本文实例讲述了asp.net实现导出DataTable数据到Word或者Excel的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/// <summary>< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
/// 导出DataTable数据到Word或者Excel
/// </summary>
/// <param name="pPage">Page指令</param>
/// <param name="dt">DataTable数据表</param>
/// <param name="str_ExportTitle">导出Word或者Excel表格的名字</param>
/// <param name="str_ExportContentTitle">导出Word或者Excel表格中内容的标题</param>
/// <param name="str_ExportMan">导出Word或者Excel的人</param>
/// <param name="str_ExportType">导出类型(w:Word,e:Excel)</param>
public bool DataTableToExcel(Page pPage, DataTable dt, string str_ExportTitle, string str_ExportContentTitle, string str_ExportMan, string str_ExportType)
{
bool bl_Result = false ;
string str_ExportTypeName = "word" ; //导出类型
string str_ExportFormat = ".doc" ; //导出类型的格式
if (str_ExportType.Equals( "e" ))
{
str_ExportTypeName = "excel" ;
str_ExportFormat = ".xls" ;
}
HttpResponse response = pPage.Response;
if (dt.Rows.Count > 0)
{
response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312" );
response.ContentType = "application/ms-" + str_ExportTypeName;
response.AppendHeader( "Content-Disposition" , "attachment;filename="
+ HttpUtility.UrlEncode(str_ExportTitle, System.Text.Encoding.UTF8).ToString() //该段需加,否则会出现中文乱码
+ str_ExportFormat);
//获取DataTable的总列数
int i_ColumnCount = dt.Columns.Count;
//定义变量存储DataTable内容
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append( "<html><head>\n" );
builder.Append( "<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\n" );
builder.Append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n" );
builder.Append( "</head>\n" );
builder.Append( "<table border='1' style='width:auto;'>" );
if (! string .IsNullOrEmpty(str_ExportContentTitle))
{
builder.Append( string .Concat( new object [] { "<tr><td colspan=" , (i_ColumnCount + 1),
" style='border:1px #7f9db9 solid;font-size:18px;font-weight:bold;'>" ,
str_ExportContentTitle,
"</td></tr>" }));
}
builder.Append( "<tr><td colspan=" + (i_ColumnCount + 1) + " valign='middle' style='border:1px #7f9db9 solid;height:24px;'>" );
builder.Append( "导出人:【" + str_ExportMan + "】,导出时间:【" + DateTime.Now.ToString( "yyyy-MM-dd hh:mm:ss" ) + "】</td></tr>" );
builder.Append( "<tr>\n" );
builder.Append( "<td style='border:1px #7f9db9 solid;bgcolor:#dee7f1;font-weight:bold;width:auto;'>序号</td>\n" );
for ( int i = 0; i < i_ColumnCount; i++)
{
if (dt.Columns[i].Caption.ToString().ToLower() != "id" )
{
builder.Append( "<td style='border:1px #7f9db9 solid;bgcolor:#dee7f1;width:auto;' align='center'><b>" + dt.Columns[i].Caption.ToString() + "</b></td>\n" );
}
}
#region 此处没有在导出的数据列的最前面加一列(序号列)
//此处没有在导出的数据列的最前面加一列(序号列)
//foreach (DataRow row in dt.Rows)
//{
// builder.Append("<tr>");
// for (int j = 0; j < i_ColumnCount; j++)
// {
// if (dt.Columns[j].Caption.ToString().ToLower() != "id")
// {
// builder.Append("<td style='border:1px #7f9db9 solid;vnd.ms-excel.numberformat:@'>" + row[j].ToString() + "</td>");
// }
// }
// builder.Append("</tr>\n");
//}
#endregion
#region 在导出的数据列的最前面加了一序号列(注意:非DataTable数据的序号)
//在导出的数据列的最前面加了一序号列(注意:非DataTable数据的序号)
for ( int m = 0; m < dt.Rows.Count; m++)
{
builder.Append( "<tr>" );
for ( int j = 0; j < i_ColumnCount; j++)
{
if (dt.Columns[j].Caption.ToString().ToLower() != "id" )
{
if (j == 0)
{
builder.Append( "<td style='border:1px #7f9db9 solid;width:auto;' align='center'>" + (m + 1) + "</td>" );
}
if (j > 0)
{
builder.Append( "<td style='border:1px #7f9db9 solid;width:auto;vnd.ms-excel.numberformat:@' align='left'>" + dt.Rows[m][j - 1].ToString() + "</td>" );
}
if (j == dt.Columns.Count - 1)
{
builder.Append( "<td style='border:1px #7f9db9 solid;width:auto;vnd.ms-excel.numberformat:@' align='left'>" + dt.Rows[m][j].ToString() + "</td>" );
}
}
}
builder.Append( "</tr>\n" );
}
#endregion
builder.Append( "<tr><td colspan=" + (i_ColumnCount + 1) + " valign='middle' style='border:1px #7f9db9 solid;height:24px;' align='left'>" );
builder.Append( "合计:共【<font color='red'><b>" + dt.Rows.Count + "</b></font>】条记录</td></tr>" );
builder.Append( "<tr>\n" );
builder.Append( "</table>" );
response.Write(builder.ToString());
response.End();
bl_Result = true ;
}
return bl_Result;
}
|
希望本文所述对大家asp.net程序设计有所帮助。