将 DataGrid 导出到 Excel C# winform

本文关键字:Excel winform DataGrid | 更新日期: 2023-09-27 18:37:13

我正在寻找如何将数据网格(不是数据网格视图)导出到Excel。我正在研究VS 2003 Winform。我在网上寻找,但没有结果,我发现解决方案只是关于Datagridview VS 2010(asp)。

这是我到目前为止所拥有的:

lblMessage.Text = ""; 
// Export all the details 
try 
{ 
    // Get the datatable to export 
    DataTable dtEmployee = dsEmployee.Tables["Employee"].Copy(); 
    // Export all the details to Excel 
    RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Win");
    objExport.ExportDetails(dtEmployee, Export.ExportFormat.Excel, "C:''EmployeesInfo.xls");
    lblMessage.Text = "Successfully exported to C:''EmployeesInfo.xls";
}
catch(Exception Ex)
{ 
    lblMessage.Text = Ex.Message;
}

将 DataGrid 导出到 Excel C# winform

阅读网格(伪代码):

 // Here Interop to Excel Application, save empty workbook (xls file)
 // Here connect to the create workbook using ACE
 for (int i = 0; i < grid.rows.count; i++)
 {
     for (int j = 0; j < grid.columns.count; j++)
     {
         // here use one of the methods described in this link to write data 
         // http://support.microsoft.com/kb/306023
         //   ----->  grid.Item(i, j) = excel cell  <--------
     }
 }

看到这个:

使用 OleDb 书写以超越

还有这个:

将网格中的单元格与Excel中的单元格相关联,例如 A1、D7 等。

http://support.microsoft.com/kb/302094