简体   繁体   English

如何在C#中使用EPPlus关闭ExcelPackage?

[英]How to close ExcelPackage using EPPlus in C#?

I'm using epplus to write in a file an extense quantity of information in an excel template, but then I need to close the ExcelPackage as to be usable with the Excel application. 我正在使用epplus在Excel模板中向文件中写入大量信息,但是随后我需要关闭ExcelPackage以便与Excel应用程序一起使用。 It throws me this exception: "System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC" 它引发了以下异常:“ System.Runtime.InteropServices.COMException(0x800A03EC):HRESULT异常:0x800A03EC”

    private void FillCardsSheet()
    {
        xlPackage = new ExcelPackage(Template);

        wsCards = xlPackage.Workbook.Worksheets[4];

        string command = "SELECT * FROM dbo_serial_cards WHERE type <> 'EXT' AND LEFT([device_tag], 2) <> '!!'";
        OleDbCommand cmd = new OleDbCommand(command, CON);
        OleDbDataReader reader = cmd.ExecuteReader();
        int row = 1;
        while (reader.Read())
        {
            row++;
            for (int col = 1; col <= 16; col++)
            {
                wsCards.Cells[row, col].Value = reader.GetValue(col - 1);
            }
        }

        xlPackage.SaveAs(Template);
        xlPackage.Dispose();
    }

Hard to say without more details about your connection and objects. 如果没有有关连接和对象的更多详细信息,很难说。 What is 'Template'? 什么是“模板”? Might help if you say which line is generating the error (like @mason mentioned). 如果您说哪一行会产生错误,可能会有所帮助(例如提到的@mason)。 That is a COM error you seem to be getting so it could be with the db connection or maybe the package itself. 那是您似乎正在得到的COM错误,所以它可能与数据库连接或程序包本身有关。 Since the package is being managed outside the method make sure it is not locked or closed. 由于包是在方法外部进行管理的,因此请确保未锁定或关闭它。

This worked fine for me connection to a local SQL Server db: 这对我连接到本地SQL Server数据库的工作正常:

[TestMethod]
public void SQL_Reader_Test()
{
    var Template = new FileInfo(@"c:\temp\temp.xlsx");
    if (Template.Exists)
        Template.Delete();

    var xlPackage = new ExcelPackage(Template);

    //var wsCards = xlPackage.Workbook.Worksheets[4];
    var wsCards = xlPackage.Workbook.Worksheets.Add("Cards");

    //const string constring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\temp\northwind.mdb;Persist Security Info=False;";
    const string constring = @"Provider=SQLNCLI11;Data Source=MYMACHINENAME\SQLEXPRESS;Initial Catalog=AdventureWorks;UID=AdventureWorks; Pwd=AdventureWorks";

    using (var CON = new OleDbConnection(constring))
    {
        CON.Open();

        //string command = "SELECT * FROM dbo_serial_cards WHERE type <> 'EXT' AND LEFT([device_tag], 2) <> '!!'";
        const string command = "SELECT * FROM Person.Address";
        var cmd = new OleDbCommand(command, CON);
        var reader = cmd.ExecuteReader();

        int row = 1;
        while (reader.Read())
        {
            row++;
            //for (int col = 1; col <= 16; col++)
            for (int col = 1; col <= reader.FieldCount; col++)
            {
                wsCards.Cells[row, col].Value = reader.GetValue(col - 1);
            }
        }

        xlPackage.SaveAs(Template);
        xlPackage.Dispose();
    }
} 

Try to use the ExcelPackage within an Using-Block: 尝试在Using-Block中使用ExcelPackage:

using (ExcelPackage xlsPackage = new ExcelPackage(Template))
{
    // Your Code
    xlPackage.SaveAs(Template);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM