开发者

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

This question already has an answer here: Microsoft.Office.Interop.Excel doesn't work on 64 bit (1 answer) Closed 8 years ago.

The error appeared when exporting data in a datagrid view to an 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);

How do I fix this problem?

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


Consider:

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

Delete this line or move to under the line that closes the Excel application.

It works for me.


The real reason is the regional settings configured for the user that launches the thread. It is documented as a bug.

http://support.microsoft.com/default.aspx?scid=kb;en-us;320369


Your code works fine on Office 2007 + VS 2010. What versions are you using? Mayby you have choosen wrong version of interop refernece: Office 2007 = 12.0.0.0, Office 2010 = 14.0.0.0

You can look at http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 it might solve your problem.


I had the same problem and I solved it:
Look at this:
Microsoft.Office.Interop.Excel doesn't work on 64 bit


You should type this line:

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

Do this after you close your Excel application; before adding WorkBook you should not use this line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜