programing

형식이 오래되었거나 형식 라이브러리가 잘못되었습니다. (HRESULT 예외: 0x80028018 (TYPE_E_INVDATAREAD)

telebox 2023. 9. 19. 21:01
반응형

형식이 오래되었거나 형식 라이브러리가 잘못되었습니다. (HRESULT 예외: 0x80028018 (TYPE_E_INVDATAREAD)

데이터 그리드 보기의 데이터를 Excel 시트로 내보낼 때 오류가 발생했습니다.

오류(이전 형식 또는 잘못된 형식 라이브러리)(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 응용 프로그램을 닫는 줄 아래로 이동합니다.

저한테는 효과가 있어요.

실제 이유는 스레드를 실행하는 사용자를 위해 구성된 지역 설정 때문입니다.그것은 버그로 기록되어있습니다.

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

Office 2007 + VS 2010에서 코드가 정상적으로 작동합니다.어떤 버전을 사용하고 계십니까?Mayby 당신이 잘못된 버전의 상호 참조를 선택했습니다: Office 2007 = 12.0.0.0, Office 2010 = 14.0.0.0.0.0

http://support.microsoft.com/default.aspx?scid=kb;en-us;320369 보면 문제가 해결될 수도 있습니다.

저도 같은 문제가 있어서 해결했습니다.
이것 좀 보세요.
마이크로소프트.사무실.인터럽트.Excel은 64비트에서 작동하지 않습니다.

다음 줄을 입력해야 합니다.

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

Excel 응용프로그램을 닫은 후에 이 작업을 수행하십시오. WorkBook을 추가하기 전에는 이 줄을 사용해서는 안 됩니다.

언급URL : https://stackoverflow.com/questions/5180713/old-format-or-invalid-type-library-exception-from-hresult-0x80028018-type-e

반응형