ClosedXML / ClosedXML.Report

ClosedXML.Report is a tool for report generation with which you can easily export any data from your .NET classes to Excel using a XLSX-template.
https://closedxml.github.io/ClosedXML.Report/
MIT License
544 stars 111 forks source link

Pivot table: Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded. #200

Open Aduthraes opened 3 years ago

Aduthraes commented 3 years ago

Hi,

i was trying to create an example with pivot tables and i've used your template tPivot1.xlsx When trying to open the generated excel file with Office 2016 and 2019, i'm getting the following error:

"Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded. Removed Part: /xl/pivotTables/pivotTable.xml part with XML error. (PivotTable view) HRESULT 0x8000ffff Line 1, column 0."

I thought i was doing something wrong until i opened the same file with LibreOffice and i saw that the pivot table was correctly generated.

Am i missing something or is there really an issue with Office?

Thank you

b0bi79 commented 3 years ago

Hi. Have you changed something in the template or in the report generation code?

Aduthraes commented 3 years ago

Hi, i've changed the name of the named range to Data and that was the only change i've made in the template. Here's the class and export methods:

public class Order
{
    public string Company { get; set; }
    public int PaymentMethod { get; set; }
    public string OrderNo { get; set; }
    public DateTime ShipDate { get; set; } 
    public int ItemsTotal { get; set; }
    public int TaxRate { get; set; }
    public int AmountPaid { get; set; }
}

    public virtual byte[] Export<TEntity>(IList<TEntity> dataToExport, Guid templateId) where TEntity : class
    {
        byte[] template = GetTemplateFile(templateId);

        if (template == null) return null;

        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(template, 0, template.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return CreateExcelFromTemplate.CreateExcel(
                stream,
                new
                {
                    Data = dataToExport,
                    DynamicHeaders = DynamicColumnsInExcel,
                    Username = _userService.Fullname,
                    CreationDate = DateTime.Now.ToString("dd/MM/yyyy"),
                    CreationDateTime = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    Reference = Reference,
                    Title = Title
                });
        }
    }

public static class CreateExcelFromTemplate
{
    /// <summary>
    /// Generate excel file from template by file stream
    /// </summary>
    /// <param name="fileStream"></param>
    /// <param name="data"></param>
    /// <returns>File byte array</returns>
    public static byte[] CreateExcel(Stream fileStream, object data)
    {
        using (XLTemplate template = new XLTemplate(fileStream))
        {
            return Generate(template, data);
        }
    }

    private static byte[] Generate(XLTemplate template, object data)
    {
        template.AddVariable(data);
        template.Generate();

        using (MemoryStream outputStream = new MemoryStream())
        {
            template.SaveAs(outputStream);
            return outputStream.ToArray();
        }
    }

} Thank you for the quick response.

b0bi79 commented 3 years ago

Can you tell which version of ClosedXML you are using?

Aduthraes commented 3 years ago

v0.2.1

b0bi79 commented 3 years ago

0.2.1 it is a ClosedXML.Report version. Which version of ClosedXML?

Aduthraes commented 3 years ago

I didn't install ClosedXML, just ClosedXML.Report. Do i have to install both of them?

b0bi79 commented 3 years ago

ClosedXML should be installed automatically as a dependency on ClosedXML.Report. Most likely this is the latest compatible version, it should be 0.95.1. Tomorrow I'll check how Pivot works with ClosedXML 0.95.1.

Aduthraes commented 3 years ago

You're right, it was installed as a dependency. Version 0.95.0

Aduthraes commented 3 years ago

Hi, just un update. The problem persists in version 0.95.4

b0bi79 commented 3 years ago

Hi. Have you solved this issue? You're right, ClosedXML generates a file that Excel cannot read. The fix should be in ClosedXML.

Aduthraes commented 3 years ago

Hi. I didn't fix the issue yet. Do you know what's causing the problem? I could try to help if you give some hints on where the issue might be. Thank you.

C1rdec commented 2 years ago

Any news? I'm having the same issue with (0.95.4)

b0bi79 commented 2 years ago

The fix should be in ClosedXML. Not fixed yet.

C1rdec commented 2 years ago

Can I help in any way to speed things up?

StepanHusa commented 1 year ago

I think, I found the problem... I described it here: https://github.com/ClosedXML/ClosedXML/discussions/2135