1

I just started using the epplus library after attempting to use oledb and then tried interop.

Below is what i'm working on

public void editExcel_VariablesEEP()
    {
        try
        {
            FileInfo newFile = new FileInfo(excelQuotePath);
            ExcelPackage pck = new ExcelPackage(newFile);
            var energyCalculation = pck.Workbook.Worksheets[4];
            var ROI = pck.Workbook.Worksheets[3];
            var NPV = pck.Workbook.Worksheets[2];
            var Summary = pck.Workbook.Worksheets[1];

            energyCalculation.Cells["B5"].Value = Convert.ToDecimal(numericUpDown1.Value);
            energyCalculation.Cells["B6"].Value = numericUpDown2.Value;
            energyCalculation.Cells["B7"].Value = numericUpDown3.Value;
            energyCalculation.Cells["B8"].Value = numericUpDown4.Value;                
            NPV.Cells["B12"].Value = (numericUpDown5.Value/100);
            NPV.Cells["B13"].Value = (numericUpDown6.Value / 100);
            NPV.Cells["B14"].Value = (numericUpDown7.Value / 100);
            energyCalculation.Cells["F40"].Value = (numericUpDown8.Value / 100);
            energyCalculation.Cells["L5"].Value = numericUpDown9.Value;
            energyCalculation.Cells["G60"].Value = (comboBox1.Text);
            Summary.Cells["K2"].Value = (quoteName.Text);

            pck.Save();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

    }

For unknown reasons it will save correctly, no errors are shown in visual studio. But when I open the xlsx file, i'm presented with "file is corrupt" prompt. I then agree to the fix, the error is shown as - Removed Part: Drawing shape. But as I review the excel all data is present and accounted for.

Please help if you are familiar with this.

Thanks.

//Update on tests Just to follow on, with what I'm doing, I copy a template xlsx file to a different folder, I run the above code I posted and it is saved. I load up the newly edited xlsx file and presented with the error. I confirm for fix then save/overwrite it. I then run the program again, but I've instead pointed it back at the file. It saves. I run, and there is no error presented. Something has happened and I haven't a clue what.

//Update2

So, what has happened so far. The error was correct in its description. It was an image that caused problems. I overlooked it completely because it was a small image. The fix I had to do was to change some settings in the "size and properties" menu from right clicking the image. Under properties tab, I checked "don't move or size with cells" , and checked the print object and locked check boxes. Finally, under the size tab, I checked "lock aspect ratio". No errors showed when loading. But observing the file sizes, it appears the image is not "saved" as each time I open the excel then close, it asks for save even though I made no changes. Confirming the save, the file size differs (an increase in size). Testing whether this 'odd save' would affect reopening the file or further editing, I tried it, and was good. No issues, reopening. but the 'odd save' persisted.

Pete
  • 683
  • 1
  • 8
  • 17
  • I experience a similar problem with a EPPlus generated excel .xlsx with only text in it. I open in excel, get the corrupt prompts, save file, and all data appears to be there. Haven't found the cause yet. All the related discussion I've found seems to be the same problem but in different circumstances: http://epplus.codeplex.com/discussions/223843?ProjectName=epplus – Mitch Wheat Mar 17 '13 at 03:11
  • Such a strange problem. I've tried all variations of assigning values to the cell, literal strings, as variables, tried putting quotes marks around it (thought they were part of the string), tried Convert.toString() . – Pete Mar 17 '13 at 03:16
  • Do you have any duplicate worksheet names? Originally I did, but the problem still exists even after I've made them unique. – Mitch Wheat Mar 17 '13 at 03:18
  • No, All worksheets are unique. – Pete Mar 17 '13 at 03:19
  • Just to follow on, with what I'm doing, I copy a template xlsx file to a different folder, i run the above code I posted, it is saved. I load up the newly edited xlsx file and presented with the error. I confirm for fix, save/overwrite it. I then run the program again, but I've instead pointed it back at the file. It saves. I run, and there is no error presented. Something has happened and I haven't a clue what. – Pete Mar 17 '13 at 04:19

0 Answers0