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

2024-01-28

参考一些EPPLUS示例代码,只为一项活动创建一个epplus对象。

ex

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

这意味着活动完成后,对象将被自动处理。 接下来,将再次创建对象以再次执行活动。 我想为多次活动创建一个 EPPLUS 对象,我想创建一个可以多次使用的 EPPLUS 对象,而不是使用“using”语句。

这是我的代码

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();

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在C#中使用EPPLUS的一个对象多次写入excel 的相关文章

随机推荐