将datagrid视图导出到Excel工作表时出错

时间:2011-03-03 10:05:05

标签: c# excel datagridview

我在寡妇应用程序中有数据gridview,我添加了按钮将数据导出到excel表但是这些出现了

Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Excel._Worksheet'. An explicit conversion exists (are you missing a cast?) here ( worksheet = workbook.Sheets["Sheet1"];)

这个错误已经过了(方法没有超载' SaveAs'需要' 11'参数)

workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

并且此错误已经过了

我的代码:

Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD)
private void button1_Click(object sender, EventArgs e)
{
    // creating Excel Application 
    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
    // 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 
    app.Visible = true;
    // get the reference of first sheet. By default its name is Sheet1. 
    // store its reference to worksheet 
    worksheet = workbook.Sheets["Sheet1"];
    worksheet = workbook.ActiveSheet;
    // changing the name of active sheet 
    worksheet.Name = "Exported from gridview";

    // 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 

    workbook.SaveAs("c:\\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    // Exit from the application 
    app.Quit();
    // 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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

不要忘记施放,如workbook.Sheets []和workbook.ActiveSheet返回对象类型:

worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;

你的SaveAs方法需要一个额外的'缺失':

workbook.SaveAs("c:\\output.xls", 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);

对于“旧格式或无效类型库”错误,请参阅this Microsoft article了解解决方法。

相关问题