Question

I'm using EPPlus to create and save an Excel file.

The method looks like this:

public static void CreateAnnuityExcelSheet(List<Calculation> cList, FormCollection form, int DTCyear, string path)
    {
        var StartValue = cList[0].StartValue;
        var radio = form["advanceOrArrears"];
        string fileName = cList[0].CalculationName;
        FileInfo info = new FileInfo(path);
        using (ExcelPackage package = new ExcelPackage(info))
        {
            ExcelWorksheet ws = package.Workbook.Worksheets.Add(fileName);
            //Styles for the sheet

            package.Save();
        }
    }

I have also tried using .SaveAs(info) but the result is the same for me.

Well, I now have the file saved under C:\ExcelFiles\ and let's say it's about 6kb, but when I open it in Excel and try to close it Excel want's me to save it. When I do, the size doubles (~12kb).

I did try open the unsaved, and the saved file, in notepad++ and I can see there is some difference in the files.

They really don't make much sense buy looking at them in notepad++, but here is how it looks:

The picture above is from the unsaved version. The saved one is looking pretty much the same (unreadable) just a lot bigger.

I now wonder if anyone could identify the problem? Have you had the same problem before?

It's not making a big trouble for me but it would be nice if the file was created "as it should" right away. Because when I try to upload and read the file, it has to be the saved version or everything crashes...

Was it helpful?

Solution

If you save the file in Excel, a lot of styles and Excel-specific stuff is added automatically.

You can verify that by

  • unzipping the file before opening it in Excel into a directory (several files and folders)
  • open the file in Excel and save it
  • unzipping the new (larger) file into a different directory
  • do a directory-compare of the two unzipped files.

You should see the additional styles that were added by Excel.

UPDATE: If the file getting larger is still an issue, you could try is to open a file created in Excel and add your content instead of creating a new document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top