C# 将多个gridview或数据集导出到单个或多个excel工作表
我正在使用此方法将单个网格导出到excelC# 将多个gridview或数据集导出到单个或多个excel工作表,c#,excel,gridview,xls,worksheet,C#,Excel,Gridview,Xls,Worksheet,我正在使用此方法将单个网格导出到excel savedialog.Filter = "Microsoft Excel Documents|*.xls"; savedialog.DefaultExt = "xls"; savedialog.FileName = "Document"; if (savedialog.ShowDialog() == DialogResult.OK) {
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工作表或多个工作表
请帮我弄清楚它您也可以从direct表创建它。。 可以按如下方式调用此扩展方法:
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);
}
}
}
公共静态类My_DataTable_扩展
{
//将DataTable导出到excel文件中,并在标题行中显示字段名
//-如果给定文件路径,则保存excel文件而不使其可见
//-不要保存excel文件,如果没有提供文件路径,只需使其可见即可
公共静态void ExportToExcel(此数据表Tbl,字符串ExcelFilePath=null)
{
尝试
{
if(Tbl==null | | Tbl.Columns.Count==0)
抛出新异常(“ExportToExcel:Null或空输入表!\n”);
//加载excel,并创建新工作簿
Excel.Application excelApp=新的Excel.Application();
excelApp.Workbooks.Add();
//单一工作表
Excel.\u工作表工作表=excelApp.ActiveSheet;
//列标题
对于(int i=0;i
您也可以从direct表中创建它。。
可以按如下方式调用此扩展方法:
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);
}
}
}
公共静态类My_DataTable_扩展
{
//将DataTable导出到excel文件中,并在标题行中显示字段名
//-如果给定文件路径,则保存excel文件而不使其可见
//-不要保存excel文件,如果没有提供文件路径,只需使其可见即可
公共静态void ExportToExcel(此数据表Tbl,字符串ExcelFilePath=null)
{
尝试
{
if(Tbl==null | | Tbl.Columns.Count==0)
抛出新异常(“ExportToExcel:Null或空输入表!\n”);
//加载excel,并创建新工作簿
Excel.Application excelApp=新的Excel.Application();
excelApp.Workbooks.Add();
//单一工作表
Excel.\u工作表工作表=excelApp.ActiveSheet;
//列标题
对于(int i=0;i
您也可以从direct表中创建它。。
可以按如下方式调用此扩展方法:
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);
}
}
}
公共静态类My_DataTable_扩展
{
//将DataTable导出到excel文件中,并在标题行中显示字段名
//-如果给定文件路径,则保存excel文件而不使其可见
//-不要保存excel文件,如果没有提供文件路径,只需使其可见即可
公共静态void ExportToExcel(此数据表Tbl,字符串ExcelFilePath=null)
{
尝试
{
if(Tbl==null | | Tbl.Columns.Count==0)
抛出新异常(“ExportToExcel:Null或空输入表!\n”);
//加载excel,并创建新工作簿
Excel.Application excelApp=新的Excel.Application();
excelApp.Workbooks.Add();
//单一工作表
Excel.\u工作表工作表=excelApp.ActiveSheet;
//列标题
对于(int i=0;i