c# (WinForms-App) export DataSet to Excel

后端 未结 8 2103
野趣味
野趣味 2020-12-04 14:46

I need a solution to export a dataset to an excel file without any asp code (HttpResonpsne...) but i did not find a good example to do this...

Best thanks in advance

8条回答
  •  感动是毒
    2020-12-04 15:33

    Microsoft has a built in solution for Importing/Exporting Excel files. It's not the most straightforward library but it generally works better than the others listed above.

    The library required to do this is included in Office and can be found under the list of Framework assemblies at Microsoft.Office.Interop.Excel.

    Here is some example code:

    using Excel = Microsoft.Office.Interop.Excel;
    
    Excel.Application app = new Excel.Application();
    
    //Open existing workbook
    //Excel.Workbook workbook = xlApp.Workbooks.Open(fileName);
    
    //Create new workbook
    Excel.Workbook workbook = app.Workbooks.Add();
    
    Excel.Worksheet worksheet = workbook.ActiveSheet;
    
    worksheet.Cells[1,1] = "Hello world!"; // Indexes start at 1, because Excel
    workbook.SaveAs("C:\\MyWorkbook.xlsx");
    workbook.Close();
    app.Quit();
    

提交回复
热议问题