网上能搜到许多DataTable导出EXCEL的文章,但实施起来,可行者不多也!本人认真调试了一番,问题得以解决,现整理与大家分享:
一、实现目标:
由一个内存表DataTable,导出字段名及其内容的完整EXCEL表格
二、实施步骤:
1、添加引用:
这是非常生要的一步,很多人调试不成都是因为这步没做好:
需要在你的解决方案中添加COM引用,选择 "Microsoft EXCEL ...."(根据版本有所不同),这是为下面的
EXCEL相关命名空间的引用做铺垫的;
我用的EXCEL
2007,添加COM引用:
interop.Microsoft.Office.Core.dll
interop.Microsoft.Office.interop.Excel.dll
增加了两个引用文件!
2、命名空间引用部分:
增加下面的引用内容:
using
Microsoft.Office.Interop.Excel;
3、定义函数:
public static void DataTabletoExcel(System.Data.DataTable
tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
Application xlApp = new ApplicationClass();
xlApp.DefaultFilePath = "";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Workbook xlBook = xlApp.Workbooks.Add(true);
//将DataTable的列名导入Excel表第一行
foreach (DataColumn dc in tmpDataTable.Columns)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
rowIndex++;
columnIndex = 0;
for (int j = 0; j < columnNum; j++)
{
columnIndex++;
xlApp.Cells[rowIndex, columnIndex] =
tmpDataTable.Rows[i][j].ToString();
}
}
//xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName,
System.Text.Encoding.UTF8));
xlBook.SaveCopyAs(strFileName);
}
}
4、 使用实例:
System.Data.DataTable dt =
……;
//准备好你的DataTable
DataTabletoExcel(dt,
"C:\\中国.XLS");
//调用自定义的函数,当然输出文件你可以随便写
三、测试环境:
VS2008,EXCEL 2007
导出excel(无组件法)
#region 输出Excel
public static void CreateExcelOther(Page page, DataSet ds, string
colHeaders, string typeid, string FileName)
{
HttpResponse resp;
resp = page.Response;
resp.ContentEncoding =
System.Text.Encoding.GetEncoding("GB2312");
//resp.ContentType = "application/Vnd.MS-Excel";
resp.AppendHeader("Content-Disposition", "attachment;filename=" +
FileName);
//定义表对象与行对像,同时用DataSet对其值进行初始化
System.Data.DataTable dt = ds.Tables[0];
string bodyData = ExportTable(colHeaders,ds);
// typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
if (typeid == "1")
{
//向HTTP输出流中写入取得的数据信息
//resp.Write(colHeaders);
resp.Write(bodyData);
}
else
{
if (typeid == "2")
{
//从DataSet中直接导出XML数据并且写到HTTP输出流中
resp.Write(ds.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();
}
#endregion
#region 表格数据
public static string ExportTable(string colHeaders, DataSet
ds)
{
StringBuilder sb = new StringBuilder();
//data = ds.DataSetName + "\n";
int count = 0;
foreach (DataTable tb in ds.Tables)
{
//data += tb.TableName + "\n";
sb.AppendLine("<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=gb2312\">");
sb.AppendLine("<table cellspacing=\"0\"
cellpadding=\"5\" rules=\"all\"
border=\"1\">");
////写出列名
sb.AppendLine("<tr style=\"font-weight: bold;
white-space: nowrap;\">");
foreach (string aStr in colHeaders.Split('\t'))
{
sb.AppendLine("<td>" + aStr +
"</td>");
}
sb.AppendLine("</tr>");
//写出数据
foreach (DataRow row in tb.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in tb.Columns)
{
if (column.ColumnName.Equals("IDCard") ||
column.ColumnName.Equals("IDCard"))
sb.Append("<td
style=\"vnd.ms-excel.numberformat:@\">" +
row[column].ToString() +
"</td>");
else
sb.Append("<td>" +
row[column].ToString() +
"</td>");
}
sb.AppendLine("</tr>");