简体   繁体   English

将DataGrid导出到Excel C#winform

[英]Export DataGrid to Excel C# winform

I'm looking for how to export a datagrid (not datagridview) to Excel. 我正在寻找如何将datagrid(而不是datagridview)导出到Excel。 I am working on VS 2003 Winform. 我正在VS 2003 Winform上工作。 I'm looking on the net, but without result, and I find that solutions are just about Datagridview VS 2010 (asp). 我正在网上寻找结果,但没有结果,我发现解决方案仅与Datagridview VS 2010(asp)有关。

This is what I have so far: 这是我到目前为止的内容:

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

Reading grid (pseudo-code): 读取网格(伪代码):

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

See this: 看到这个:

Writing to excel using OleDb 使用OleDb写入Excel

Also this: 还有这个:

Associate a cell in the grid to a cell in Excel eg A1, D7, etc. 将网格中的单元格与Excel的单元格关联,例如A1,D7等。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM