将DataGrid导出到Excel C#winform

时间:2013-10-29 12:34:42

标签: c# winforms excel datagrid

我正在寻找如何将数据网格(而非数据网格视图)导出到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;
}

1 个答案:

答案 0 :(得分:0)

读取网格(伪代码):

 // 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  <--------
     }
 }

见:

Writing to excel using OleDb

还有:

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

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