2011-03-03 157 views
12

误差在数据网格视图导出数据时到Excel片出现:旧格式或无效类型库。 (从HRESULT异常:0x80028018(TYPE_E_INVDATAREAD))

误差(旧格式或无效类型库(从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; 
    } 
} 
+0

请您格式化,以便它清晰可辨。 – 2011-03-03 12:34:57

+0

你已经在这里问过这个问题:http://stackoverflow.com/questions/5179196/error-when-export-datagrid-view-to-excel-sheet/5179312#5179312 – 2011-03-03 12:47:09

+1

它是因为除了“ EN-US”。我已经在hyperneed.com上回答了这个问题。要查看答案,请访问下面的链接:http://www.hyperneed.com/ShowSearchAnswers.aspx?searchstring=&category=Programming&questionid=5afa16f5-653a-4f2e-afcb-c83dce5bc4e4 – michael 2011-03-19 13:43:50

回答

6

考虑:

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

删除这条线或线下移到关闭Excel应用程序。

它适用于我。

0

你应该输入该行:

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

做到这一点您关闭Excel应用程序之后;在添加WorkBook之前,您不应使用此行。

相关问题