c# - 如何在C#中使用EPPLUS的一个对象多次写入excel

标签 c# epplus

引用一些EPPLUS的示例代码,就是为一个activity创建一个epplus对象。

using (ExcelPackage package = new ExcelPackage(newFile))
{...
// activity
}

表示activity结束后,object自动释放。 接下来,将再次创建对象以再次进行事件。 而且我只想为多次事件创建一个 EPPLUS 对象,我想创建一个可以多次使用的 EPPLUS 对象,而不是使用“使用”语句。

这是我的代码

public partial class FMain : Form
    {
        ...
        ExcelPackage pack; 
        FileInfo InfoPathFile;
        public StringPathFile = ""      
        ...
    public FMain()
        {
        ...
        }
    private void NewDialog_FileOk(object sender, CancelEventArgs e)
        {
            if(pack != null)
                pack.Dispose();

            StringPathFile = NewDialog.FileName;

            InfoPathFile = new FileInfo(StringPathFile);
            pack = new ExcelPackage(InfoPathFile);
            ...
        }
    private void SaveData(float[] Sens, string tt, string dd)
        {
            var ExSheet = pack.Workbook.Worksheets["Data"];

            ExSheet.Cells["A" + rowExcel].Value = Numb;
            ExSheet.Cells["B" + rowExcel].Value = Sens[0];
            ExSheet.Cells["C" + rowExcel].Value = Sens[1];
            ExSheet.Cells["D" + rowExcel].Value = Sens[2];
            ExSheet.Cells["E" + rowExcel].Value = Sens[3];
            ExSheet.Cells["F" + rowExcel].Value = tt;
            ExSheet.Cells["G" + rowExcel].Value = dd;


            //pack.SaveAs(InfoPathFile);
            pack.Save();
        }

我想多次写入 excel,只使用一个 EPPLUS 对象,我不想每次进行事件时都创建 epplus 对象。使用我的代码,我只能写入 excel 文件一次,第二次写入过程失败。 我可以这样做吗?

最佳答案

您遇到的问题是调用 Save() 会自动关闭包,因此在您下次写入时会产生错误。 EPPlus 并不是真的要像那样进行“增量”保存 - 它更适合放在服务器上,让客户端告诉它一次生成一个文件,然后将其发送给客户端。

我认为最好的办法是在内存中保留一份副本,然后逐步写入文件。您可以通过 MemoryStream 执行类似的操作。因此,创建类级 MemoryStreamvar 并使用它来保存正在进行的 Excel 包。这有望证明这一概念:

[TestMethod]
public void Multi_Save_Test()
{
    //http://stackoverflow.com/questions/28007087/how-to-write-to-excel-many-times-using-one-object-of-epplus-in-c-sharp
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    //Use memstream and create the package but WITHOUT the FI so it is a memory stream as well
    //Avoid using and call manual dispose
    var holdingstream = new MemoryStream();
    var pack = new ExcelPackage();
    var ExSheet = pack.Workbook.Worksheets.Add("Data");
    ExSheet.Cells["A1"].Value = "wer";
    ExSheet.Cells["B1"].Value = "sdf";

    //Do an incremental save to the file and copy the stream before closing - ORDER COUNTS!
    pack.SaveAs(existingFile);

    holdingstream.SetLength(0);
    pack.Stream.Position = 0;
    pack.Stream.CopyTo(holdingstream);

    //*********************************************************
    //reopen the holding stream, make a change, and resave it
    pack.Load(holdingstream);
    ExSheet = pack.Workbook.Worksheets["Data"];
    ExSheet.Cells["A2"].Value = "wer";
    ExSheet.Cells["B2"].Value = "sdf";

    //Another incremental change
    pack.SaveAs(existingFile);

    holdingstream.SetLength(0);
    pack.Stream.Position = 0;
    pack.Stream.CopyTo(holdingstream);

    //*********************************************************
    //reopen the holding stream, make a change, and resave it
    pack.Load(holdingstream);
    ExSheet = pack.Workbook.Worksheets["Data"];
    ExSheet.Cells["A3"].Value = "wer";
    ExSheet.Cells["B3"].Value = "sdf";

    //Another incremental change
    pack.SaveAs(existingFile);

    holdingstream.SetLength(0);
    pack.Stream.Position = 0;
    pack.Stream.CopyTo(holdingstream);

    //*********************************************************
    //reopen the holding stream, make a change, and do a FINAL save
    pack.Load(holdingstream);
    ExSheet = pack.Workbook.Worksheets["Data"];
    ExSheet.Cells["A4"].Value = "wer";
    ExSheet.Cells["B4"].Value = "sdf";

    //All done so only need to save it to the file
    pack.SaveAs(existingFile);

    //cleanup
    pack.Dispose();
    holdingstream.Dispose();

}

关于c# - 如何在C#中使用EPPLUS的一个对象多次写入excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28007087/

相关文章:

c# - 如何在 MVC 中使用 EPPlus 从服务器下载 .xlsx 文件

c# - 找不到类型或命名空间名称 'MembershipUser'

asp.net-mvc - MVC : Return . xlsx 文件保存为下载而不是在 Excel 中打开

c# - Epplus - LoadFromCollection 和列顺序

c# - 删除 ToolStripControlHost 周围的空白

c# - 使用 Parallel.For 和 EPPlus 创建 Excel 工作表

c# - 如何取消合并单元格 EPPlus?

c# - 无法加载类型 'NHibernate.ByteCode.CaSTLe.ProxyFactoryFactory, NHibernate.ByteCode.CaSTLe'

c# - 生成一组(相对)短的 "invitation codes"的好方法是什么

c# - EntityFramework 中多个 dbcontext 中每个请求的事务