EPPlus不保存对工作表的更改

我有一个批处理作业,通过电子表格逐行处理每个logging。 根据结果​​,我想在每条logging的最后一列留下评论。

我可以通过在程序运行时监视这个单元格中的值来告诉适当的变化正在到达他们需要的地方。 然而,在运行.Save()函数之后,我打开电子表格,每一列看起来与程序运行之前一样。

看起来他们build立EPPlus是相当简单的保存您的更改,所以我不知道我做错了什么。

这是我如何打开电子表格

 if (File.Exists(path)) { try { //Connect to spreadsheet FileStream _stream = File.Open(path, FileMode.Open, FileAccess.ReadWrite); FileInfo file = new FileInfo(path); } catch (IOException) { SendEmail.OkMail("Spreadsheet is currently in use. Please close and try again."); //Exit program Environment.Exit(0); } //Pull data into EPPlus object _package = new ExcelPackage(); _package.Load(_stream); _sheet = _package.Workbook.Worksheets.First(); } else { //Notify user and exit program SendEmail.OkMail("Cannot find file in requested directory. Please check and try again."); //Exit program Environment.Exit(0); } 

此代码进行适当的更改。 在逐步完成这部分代码时,我可以看到正确的值被存储在名为_sheetExcelWorkSheet对象的正确单元中

 //Figure out what type of update occurred if (ignore == (_noUpdates - 1)) { _sheet.Cells[row, col + 11].Value = "Ignore"; } else if (add == (_adds - 1)) { _sheet.Cells[row, col + 11].Value = "Valid Add"; } else if (update == (_updates - 1)) { _sheet.Cells[row, col + 11].Value = "Valid Update"; } else if (autoterm == (_autotermed - 1)) { _sheet.Cells[row, col + 11].Value = "Valid Autoterm"; } 

一旦我完成了电子表格的循环,我就调用了.Save()函数。 尽pipe这里没有发生。 更改没有被保存。

 //Save changes to spreadsheet. _package.Save(); 

Load(Stream)方法不存储文件的path,并且不保存传入的stream以在保存时重用。 当您调用Save()方法时,程序包不知道保存文件的位置,只是closures程序包。

(这对我来说似乎是一个糟糕的devise;在这种情况下,它可能会引发exception。)

尝试改变你的代码来使用带FileInfo参数的构造函数:

 FileInfo file = new FileInfo(path); if (file.Exists) { try { _package = new ExcelPackage(file); _sheet = _package.Workbook.Worksheets.First(); } catch (IOException) { SendEmail.OkMail("Spreadsheet is currently in use. Please close and try again."); //Exit program Environment.Exit(0); } } else { //Notify user and exit program SendEmail.OkMail("Cannot find file in requested directory. Please check and try again."); //Exit program Environment.Exit(0); }