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.