error when export datagrid view to excel sheet

0

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

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)

and this error apeared

My code:

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();
        }
    }
}
c#
excel
datagridview
asked on Stack Overflow Mar 3, 2011 by Myworld • edited Mar 3, 2011 by Myworld

1 Answer

1

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;

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

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

answered on Stack Overflow Mar 3, 2011 by Edwin de Koning • edited Mar 3, 2011 by Edwin de Koning

User contributions licensed under CC BY-SA 3.0