Error exporting datatable to excel C#

0

I'm currently having as issue exporting my DataTable to Excel using Microsoft.Office.Interlope.

The error that comes up is "winform Additional information: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))."

I've googled around and tried all the resolutions I could find, most being changing the DCOM security permissions on "Microsoft Excel Application" in component services.

The error on excel generates "Cannot use object linking"

This is the code I'm currently using

 try {
            DataTable table = new DataTable();
            table = dataset(table);

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = false;
            excelApp.DisplayAlerts = false;

            Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Add(Type.Missing);

            Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkBook.ActiveSheet;
            excelWorkSheet.Name = table.TableName;

            excelWorkSheet.Cells[1, 1] = "Sample test data";
            excelWorkSheet.Cells[1, 2] = "Date : " + DateTime.Now.ToShortDateString();

            excelWorkBook.SaveAs(@"C:/testfolder/test.xlsx");
            excelWorkBook.Close();
            excelApp.Quit();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Is anyone familiar with exporting to Excel? Have you come across this error and is there a better way of doing this?

c#
excel
winforms
com
office-interop
asked on Stack Overflow Dec 30, 2015 by Raaj TheTalent Brench • edited Dec 30, 2015 by Soner Gönül

1 Answer

0

NPOI is a good open source alternative to using Microsoft.Office.Interop for creating Excel spreadsheets. This SO post gives you some sample code on how to accomplish this using NPOI.

answered on Stack Overflow Dec 30, 2015 by Josh • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0