简体   繁体   English

将数据导出到Excel时,应用程序停止

[英]Application stop when exporting data to excel

I have a code that displays a table from an Access database, on my WinForm I have an option to export the table to excel, once the user click on it takes some time to copy all rows to excel, if the user try to close the excel before all cells get transfer to the sheet the application will stop and throw the error System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800AC472' 我有一个显示来自Access数据库的表的代码,在我的WinForm上我有一个选项可以将表导出到excel,一旦用户点击它需要一些时间将所有行复制到excel,如果用户试图关闭excel在所有单元格转移到工作表之前,应用程序将停止并抛出错误System.Runtime.InteropServices.COMException:'来自HRESULT的异常:0x800AC472'

Here is the code for the "Export to Excel" button I have in my windows form 这是我在Windows窗体中的“导出到Excel”按钮的代码

        private void Export_btn_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
        app.Visible = true;
        worksheet = workbook.Sheets["Sheet1"];
        worksheet = workbook.ActiveSheet;
        for (int i = 1; i < fviAoi_tbl.Columns.Count + 1; i++)
        {
            worksheet.Cells[1, i] = fviAoi_tbl.Columns[i - 1].HeaderText;
        }
        for (int i = 0; i < fviAoi_tbl.Rows.Count - 1; i++)
        {
            for (int j = 0; j < fviAoi_tbl.Columns.Count; j++)
            {
                if (fviAoi_tbl.Rows[i].Cells[j].Value != null)
                {
                    worksheet.Cells[i + 2, j + 1] = fviAoi_tbl.Rows[i].Cells[j].Value.ToString();
                }
                else
                {
                    worksheet.Cells[i + 2, j + 1] = "";
                }
            }
        }
    }

Any ideas why this is happening or how can I make my application to ignore that error and continue running. 任何想法为什么会发生这种情况或如何让我的应用程序忽略该错误并继续运行。

Surround the code line that emit the exception with a try... catch... 用try ... catch来围绕发出异常的代码行...

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/ https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/

In general, Null is probably not what you think it is. 一般来说, Null可能不是你想象的那样。 Null is not 0 nor empty string. Null不是0也不是空字符串。 Null is lack of data. Null缺乏数据。 What is the difference between ("") and (null) (“”)和(null)之间有什么区别

Thus, C# and .NET probably throws an error here: 因此,C#和.NET可能会在这里抛出一个错误:

if (fviAoi_tbl.Rows[i].Cells[j].Value != null) because, it does not understand how can you compare some Excel cell with Null and what should it answer. if (fviAoi_tbl.Rows[i].Cells[j].Value != null)因为,它不明白你如何比较一些Excel单元格和Null以及它应该回答什么。 Change the code to: 将代码更改为:

if (fviAoi_tbl.Rows[i].Cells[j].Value != "" or something similar. if (fviAoi_tbl.Rows[i].Cells[j].Value != ""或类似的东西。

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

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