旧格式或无效类型库。 (HRESULT异常:0x80028018(TYPE_E_INVDATAREAD)) [英] Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))

查看:175
本文介绍了旧格式或无效类型库。 (HRESULT异常:0x80028018(TYPE_E_INVDATAREAD))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导出数据时出现错误,在datagrid视图中显示为excel表


错误(旧格式或无效类型库(HRESULT异常: 0x80028018(TYPE_E_INVDATAREAD)))


此行:

  Microsoft.Office.Interop.Excel._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);

//创建Excel应用程序
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;


//在Excel应用程序中创建新的WorkBook
Microsoft.Office.Interop.Excel._Workbook工作簿= app.Workbooks.Add(Type.Missing);
//在工作簿中创建新的Excel表
Microsoft.Office.Interop.Excel._Worksheet工作表= null;
//查看程序后面的excel表
// Funny
app.Visible = true;
//获取第一张表的参考。默认名称为Sheet1。
//存储其对工作表的引用
try
{
//Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
工作表=(Microsoft.Office .Interop.Excel.Worksheet)workbook.Sheets [Sheet1];
工作表=(Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
//更改活动工作表的名称
worksheet.Name =从Ketoan导出;
//在Excel中存储头部分
(int i = 1; i< DGData.Columns.Count + 1; i ++)
{
工作表Cells [1, i] = DGData.Columns [i-1] .HeaderText;
}
//存储每个行和列值为excel表
for(int i = 0; i< DGData.Rows.Count - 1; i ++)
{
for(int j = 0; j< DGData.Columns.Count; j ++)
{
工作表Cells [i + 2,j + 1] = DGData.Rows [i]。细胞[j] .Value.ToString();
}
}

//保存应用程序
string fileName = String.Empty;
SaveFileDialog saveFileExcel = new SaveFileDialog();

saveFileExcel.Filter =Excel文件| * .xls |所有文件(*。*)| *。*;
saveFileExcel.FilterIndex = 2;
saveFileExcel.RestoreDirectory = true;

if(saveFileExcel.ShowDialog()== DialogResult.OK)
{
fileName = saveFileExcel.FileName;
//固定旧代码:11 para-> add 1:Type.Missing
workbook.SaveAs(fileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type。缺少,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
}
else
return;

//退出应用程序
//app.Quit();
}
catch(System.Exception ex)
{

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


解决方案

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

删除此行或移至下一行关闭excel应用程序。



它适用于我。


error apeared when export data in datagrid view to excel sheet

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

on this line:

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

Can anyone tell me how to solve this?

My full code:

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 Excelsheet 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;

Delete this line or move to under line that close excel app.

It work for me.

这篇关于旧格式或无效类型库。 (HRESULT异常:0x80028018(TYPE_E_INVDATAREAD))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆