简体   繁体   中英

Export DataGrid to Excel C# winform

I'm looking for how to export a datagrid (not datagridview) to Excel. I am working on VS 2003 Winform. I'm looking on the net, but without result, and I find that solutions are just about 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

Also this:

Associate a cell in the grid to a cell in Excel eg A1, D7, etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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