c# - 旧格式或无效类型库。 (来自 HRESULT : 0x80028018 (TYPE_E_INVDATAREAD)) 的异常

标签 c# winforms excel datagridview

<分区>

将数据 GridView 中的数据导出到Excel工作表时出现错误:

error (Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)))

在这一行:

Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

我该如何解决这个问题?

我的完整代码:

private void button1_Click(object sender, EventArgs e)
{
    System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    // Creating Excel Application
    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

    // Creating new WorkBook within Excel application
    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);

    // Creating new Excel sheet in workbook
    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

    // See the Excel sheet behind the program
    //Funny
    app.Visible = true;

    // Get the reference of first sheet. By default its name is Sheet1.
    // Store its reference to worksheet
    try
    {
        // Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
        worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;

        // Changing the name of active sheet
        worksheet.Name = "Exported from Ketoan";

        // Storing header part in Excel
        for (int i = 1; i < DGData.Columns.Count + 1; i++)
        {
            worksheet.Cells[1, i] = DGData.Columns[i - 1].HeaderText;
        }

        // Storing each row and column value to Excel sheet
        for (int i = 0; i < DGData.Rows.Count - 1; i++)
        {
            for (int j = 0; j < DGData.Columns.Count; j++)
            {
                worksheet.Cells[i + 2, j + 1] = DGData.Rows[i].Cells[j].Value.ToString();
            }
        }

        // Save the application
        string fileName = String.Empty;
        SaveFileDialog saveFileExcel = new SaveFileDialog();

        saveFileExcel.Filter = "Excel files |*.xls|All files (*.*)|*.*";
        saveFileExcel.FilterIndex = 2;
        saveFileExcel.RestoreDirectory = true;

        if (saveFileExcel.ShowDialog() == DialogResult.OK)
        {
            fileName = saveFileExcel.FileName;

            //Fixed-old code: 11 para->add 1:Type.Missing
            workbook.SaveAs(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, Type.Missing);
        }
        else
            return;

        // Exit from the application
        //app.Quit();
    }
    catch (System.Exception ex)
    {

    }
    finally
    {
        app.Quit();
        workbook = null;
        app = null;
    }
}

最佳答案

考虑:

System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

删除此行或移至关闭 Excel 应用程序的行下方。

它对我有用。

关于c# - 旧格式或无效类型库。 (来自 HRESULT : 0x80028018 (TYPE_E_INVDATAREAD)) 的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5180713/

相关文章:

c# - SharePoint 使用 API 获取各个网站的大小

c# - 如何将 Func<> 属性/方法传递给 NancyFx Get() 方法?

c# - 具有单元测试和模拟的开源 C# 项目(不是 MVC 项目)

c# - 无法为此文件显示设计器,因为无法设计其中的任何类

EXCEL VBA UserInterfaceOnly := True not working

python - 使用 xlwt 将超链接添加到带有文本的单元格

c# - WebService Post 在模拟器上工作但不在设备上工作

C#: 'System.StackOverflowException' 出现在 mscorlib.dll 中

c# - WPF 应用程序 : Controls look like . NET 2.0 中的 WinForms 对话框

python - Openpyxl如何按索引从工作表中获取行