如何编写使用EPPLUS的一个对象在C#中脱颖而出多次 [英] How to write to excel many times using one object of EPPLUS in C#

查看:293
本文介绍了如何编写使用EPPLUS的一个对象在C#中脱颖而出多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

指一些EPPLUS示例代码,还有刚刚制作一个epplus对象一个活动。

Refer to some EPPLUS sample code, there are just creating one epplus object for one activity.

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

这意味着活动结束后,对象会自动释放。
和为下一个,对象将再次为再次做活动来创建。
,我想创建活动的多次只是一个EPPLUS对象,我想创建一个EPPLUS对象可以多次使用,不使用使用的声明。

it means that after activity is finished, object is disposed automatically. and for the next, object will be created again for doing activity again. And i want to create just one EPPLUS object for many times of activity, i want to create one EPPLUS object can be used many times, not using "using" statement.

这是我的代码

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



我要写信给练成了很多次,只使用一个EPPLUS对象,我不希望创建epplus对象每次我做了一个活动时间。使用我的代码,我可以只写一次Excel文件,第二写作过程中失败。
我能做到吗?

I want to write to excel many times, using just one EPPLUS object, i dont want to create epplus object every time i do an activity. Using my code, i can just write once to excel file, and second writing process is failed. Can i do that?

推荐答案

您有被调用保存(问题)将自动关闭的包,所以你写它下一次就会产生一个错误。 EPPlus心不是真正的意思做增量可以节省这样的 - 它更多的设计坐在一台服务器上,让客户端告诉它生成一个文件全部一次,并将其发送给客户端

The problem you are having is calling the Save() will automatically close the package so the next time you write to it it will generate an error. EPPlus isnt really meant to do "incremental" saves like that - its more designed to sit on a server, have the client tell it to generate a file all at once, and send it to the client.

我认为最好的选择将是保持它的一个副本在内存中,并逐步写入文件。你可以通过的MemoryStream 做这样的事情。因此,创建类级别的MemoryStream var和用它来保持工作正在进行Excel的包。这希望证明的概念:

I think the best bet would be to keep a copy of it in memory and incrementally write the file. You could do something like this via MemoryStream. So create class-level MemoryStreamvar and use that to hold the work-in-progress Excel Package. This hopefully demonstrates that concept:

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

}

这篇关于如何编写使用EPPLUS的一个对象在C#中脱颖而出多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆