error when export datagrid view to excel sheet

前端 未结 1 1150
无人及你
无人及你 2021-01-26 16:16

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 \'Mic         


        
相关标签:
1条回答
  • 2021-01-26 16:38

    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.

    0 讨论(0)
提交回复
热议问题