繁体   English   中英

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

[英]error when export datagrid view to excel sheet

我在寡妇应用程序中有数据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();
        }
    }
}

不要忘了强制转换,因为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);

至于“旧格式或无效类型库”错误,请参阅此Microsoft文章以获取解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM