简体   繁体   中英

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. It throws me this exception: "System.Runtime.InteropServices.COMException (0x800A03EC): Exception from 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). That is a COM error you seem to be getting so it could be with the db connection or maybe the package itself. 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:

[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 (ExcelPackage xlsPackage = new ExcelPackage(Template))
{
    // Your Code
    xlPackage.SaveAs(Template);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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