将datagrid视图导出到Excel工作表时出错 [英] error when export datagrid view to excel sheet

查看:60
本文介绍了将datagrid视图导出到Excel工作表时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I Had data gridview in widows application and I added button to export data to excel sheet but these appeared

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'参数)

and this error apeared (No overload for method 'SaveAs' takes '11' arguments) here

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


推荐答案

别忘了将其作为workbook.Sheets []和workbook.ActiveSheet进行转换,返回对象类型:

Don't forget to cast, as workbook.Sheets[] and workbook.ActiveSheet return object types:

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

您的SaveAs方法需要一个附加的丢失:

And your SaveAs method needs one addtional 'missing':

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

关于旧格式或无效类型库错误,请参见此Microsoft文章,以解决此问题。

As for the "Old format or invalid type library" error, see this Microsoft article for a workaround.

这篇关于将datagrid视图导出到Excel工作表时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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