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

C# 如何在C中使用EPPLUS的一个对象多次写入excel#,c#,epplus,C#,Epplus,参考一些EPPLUS示例代码,这里只为一个活动创建一个EPPLUS对象 前 这意味着活动完成后,对象将自动释放。 接下来,将再次创建对象,以便再次执行活动。 我想为多次活动创建一个EPPLUS对象,我想创建一个可以多次使用的EPPLUS对象,而不是使用“using”语句 这是我的密码 public partial class FMain : Form { ... ExcelPackage pack; FileInfo InfoPathFi

参考一些EPPLUS示例代码,这里只为一个活动创建一个EPPLUS对象

这意味着活动完成后,对象将自动释放。 接下来,将再次创建对象,以便再次执行活动。 我想为多次活动创建一个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,只使用一个ePlus对象,我不想每次执行活动时都创建ePlus对象。使用我的代码,我只需向excel文件写入一次,第二次写入过程失败。
我可以这样做吗?

您遇到的问题是调用
Save()
将自动关闭该包,以便下次写入时它将生成错误。EPPlus并不是真的要做这样的“增量”保存——它更适合于坐在服务器上,让客户端告诉它一次生成一个文件,然后发送给客户端

我认为最好的办法是在内存中保留它的一个副本,并以增量方式写入文件。您可以通过
MemoryStream
执行类似操作。因此,创建类级别的
MemoryStream
var,并使用它来保存正在进行的工作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();

}

非常感谢先生,但是您的代码使用了“using”语句,我认为如果在打包后另存为,则“using”块中的对象将被销毁。我希望不使用块,因为我想在代码中手动销毁该对象。示例:我使用计时器倒计时,从60秒开始,每10秒我想使用一个epplus对象(只有一个对象)。直到计时器0秒,该对象将被销毁。我能做什么?我认为这种方法比每10秒创建和销毁一次对象要好,你认为创建一次对象更好吗?@AntiMafia没问题,请参阅我修改的代码。我使用usings只是为了方便-可以很容易地进行手动处理。因此,在您的代码中,您可以按任何方式执行。
[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();

}