2
votes

I am using EPPlus to edit an existing Excel file. It is working well but here goes the funny thing: When I save this file using EPPlus, the file's size is 51.0 KB. If I open the same file after editing it and save it manually, the size of the file is increased to 59.1 KB.

Anyone knows why does it happen?
Is there a way to avoid this behavior?
I mean, make EPPlus save this file on the real size of it (59.1 KB).
What is the difference between saving this file manually or using EPPlus?

Another observation that I've made is that when I open this file after editing it using EPPlus it takes more time to load the file then if I do it after manually saving it.

Here is my code:

        FileInfo file = new FileInfo(@"C:\Desktop\MyExcelFile.xlsx");
        using (var package = new ExcelPackage(file))
        {
            ExcelWorkbook workBook = package.Workbook;
            ExcelWorksheet currentWorksheet = workBook.Worksheets.SingleOrDefault(w => w.Name == "MyWorkSheet");
            currentWorksheet.Cells[2, 2].Value = "Value1";
            currentWorksheet.Cells[3, 2].Value = "Value2";
            currentWorksheet.Cells[2, 1].Value = "Value3";
            currentWorksheet.Cells[3, 1].Value = "Value4";
            package.Save();

        }
1
Were you ever able to find a solution for this? I have the same issue. Is there a way to insert the XML exactly how Excel is making it after you save it from Excel?MatthewD
@MatthewD Hi Matthew, I was able to solve this by using the Excel Interop library. You can try itVinicius Seganfredo
Thanks for getting back to me. I have been tasked with changing a few apps to not use the interop libraries. They want to remove Office from the workstations. Thanks though.MatthewD
I currently have the same problem and I'm looking for an alternative way of doing this. I'm trying to use the OpenXML SDK. I'll let you know the results.Vinicius Seganfredo
The best workaround I have found is to have a template workbook that I copy. Then I can open the copy and write to it and save. After closing and opening I don't have to save again.MatthewD

1 Answers

1
votes

Those xlsx files are just zipped directories full of xml. If you want to see the difference between them, just unzip them (7zip will work), and use a diff tool to compare the two directories. Size differences will be due to things like excel saving your last location in a workbook and EEPlus not, or how the xml is formatted. In general, you should not expect EEPlus to produce exactly the same output as Excel.