将多个gridview或数据集导出到单个或多个excel表单
即时通讯使用这种方法导出单个网格到Excel
savedialog.Filter = "Microsoft Excel Documents|*.xls"; savedialog.DefaultExt = "xls"; savedialog.FileName = "Document"; if (savedialog.ShowDialog() == DialogResult.OK) { dtTable.ExportToXls(savedialog.FileName, true); }
我需要导出4 gridview到单个Excel表或多个工作表
请帮我弄明白
你也可以使用这个。 这个函数传递你想要导出的数据表和文件path
public void CreateCSVFile(ref DataTable dt, string strFilePath) { try { // Create the CSV file to which grid data will be exported. StreamWriter sw = new StreamWriter(strFilePath, false); // First we will write the headers. //DataTable dt = m_dsProducts.Tables[0]; int iColCount = dt.Columns.Count; for (int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); // Now write all the rows. foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } sw.Close(); } catch (Exception ex) { throw ex; } }
你也可以从直接表中创build它..这个扩展方法可以被调用如下:
using System; using System.Collections.Generic; using System.Linq; using Excel = Microsoft.Office.Interop.Excel; using System.Data; using System.Data.OleDb; DataTable dt; // fill table data in dt here ... // export DataTable to excel // save excel file without ever making it visible if filepath is given // don't save excel file, just make it visible if no filepath is given dt.ExportToExcel(ExcelFilePath);
DataTable类的扩展方法:
public static class My_DataTable_Extensions { // Export DataTable into an excel file with field names in the header line // - Save excel file without ever making it visible if filepath is given // - Don't save excel file, just make it visible if no filepath is given public static void ExportToExcel(this DataTable Tbl, string ExcelFilePath = null) { try { if (Tbl == null || Tbl.Columns.Count == 0) throw new Exception("ExportToExcel: Null or empty input table!\n"); // load excel, and create a new workbook Excel.Application excelApp = new Excel.Application(); excelApp.Workbooks.Add(); // single worksheet Excel._Worksheet workSheet = excelApp.ActiveSheet; // column headings for (int i = 0; i < Tbl.Columns.Count; i++) { workSheet.Cells[1, (i+1)] = Tbl.Columns[i].ColumnName; } // rows for (int i = 0; i < Tbl.Rows.Count; i++) { // to do: format datetime values before printing for (int j = 0; j < Tbl.Columns.Count; j++) { workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j]; } } // check fielpath if (ExcelFilePath != null && ExcelFilePath != "") { try { workSheet.SaveAs(ExcelFilePath); excelApp.Quit(); MessageBox.Show("Excel file saved!"); } catch (Exception ex) { throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n" + ex.Message); } } else // no filepath is given { excelApp.Visible = true; } } catch(Exception ex) { throw new Exception("ExportToExcel: \n" + ex.Message); } } }
你必须使用Microsoft.Office.Interop.Excel
来获取完整的function,以从多个DataGridView
导出内容到excel。 这里有一篇很好的文章解释了如何去做。
http://www.codeproject.com/Articles/335589/Export-Multiple-Datasets-to-Multiple-Excel-sheets