解决Excel文件导出后出现的错误

本文关键字:错误 解决 文件 Excel | 更新日期: 2023-09-27 18:18:20

我有一个函数导出我的GridViews到Excel作为。xlsx。然而,我面对的Excel文件打开/保存与保存对话框的问题。当我试图打开Excel文件时发生错误:

我的当前代码:

  protected void EXPORT_BUTTON_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            // creating new WorkBook within Excel application
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            String DATA1 = "DATA 1";
            String DATA2 = "DATA 2";
            ExportToExcel(app, workbook, DATA1 , DATA_1 );
            workbook.Worksheets["Sheet1"].Delete();
            workbook.Worksheets["Sheet2"].Delete();
            workbook.Worksheets["Sheet3"].Delete();
            ExportToExcel(app, workbook, DATA2 , DATA_2);
            workbook.SaveAs(@"C:'Users'testacc'Desktop'Test'" + "Server_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            var filename = "Report_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx";
            workbook.SaveAs(Server.MapPath("~/Exports/") + filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment, filename=" + filename);
            workbook.Close();
            Response.TransmitFile(Server.MapPath("~/Exports/") + filename);
            app.Quit();
        }

解决Excel文件导出后出现的错误

您可以尝试下面的代码,如果它工作。

protected void EXPORT_BUTTON_Click(object sender, EventArgs e)
        {
            using(Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application())
            // creating new WorkBook within Excel application
             using(Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing))
             {
                String DATA1 = "DATA 1";
                String DATA2 = "DATA 2";
                ExportToExcel(app, workbook, DATA1 , DATA_1 );
                workbook.Worksheets["Sheet1"].Delete();
                workbook.Worksheets["Sheet2"].Delete();
                workbook.Worksheets["Sheet3"].Delete();
                ExportToExcel(app, workbook, DATA2 , DATA_2);
                workbook.SaveAs(@"C:'Users'testacc'Desktop'Test'" + "Server_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                var filename = "Report_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx";
                workbook.SaveAs(Server.MapPath("~/Exports/") + filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment, filename=" + filename);
                workbook.Close();
                Response.TransmitFile(Server.MapPath("~/Exports/") + filename);
                Response.End();
                app.Quit();
            }
        }