方法一:带StringBuilder 方法的导出
该方法是将所有的数据通过html的形式写入到StringBuilder 中,然后通过response导出。
熟悉html格式的人可以改变成各种格式。
List<U> objList = new List<U>(); objList = BLL.GetInfo();//读取数据 StringBuilder sb = new StringBuilder(); sb.Append("<style type="text/css">"); sb.Append("<!--"); sb.Append(".text"); sb.Append("{mso-style-parent:style0;"); sb.Append("font-size:10.0pt;"); sb.Append("font-family:"Arial Unicode MS", sans-serif;"); sb.Append("mso-font-charset:0;"); sb.Append(@"mso-number-format:@;"); sb.Append("text-align:center;"); sb.Append("border:.5pt solid black;"); sb.Append("white-space:normal;}"); sb.Append("-->"); sb.Append("</style>"); sb.Append("<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">"); sb.Append("<tr align="Center" style="font-weight:bold;">"); sb.Append("<td>ID</td><td>用户名</td><td>真实姓名</td><td>省份</td><td>注册时间</td"); sb.Append("</tr>"); foreach (U item in objList) { sb.Append("<tr align="Center"><td>" + item.id + "</td><td>" + item.uName + "</td><td>" + item.rName + "</td><td>" + item.proId + "</td><td>" + item.regDate + "</td></tr>"); } sb.Append("</table>"); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Charset = "GB2312"; // System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=myU.xls"); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 // System.Web.HttpContext.Current.Response.AddHeader("Content-Length",sb.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 System.Web.HttpContext.Current.Response.Write(sb.ToString()); // 停止页面的执行 System.Web.HttpContext.Current.Response.End();
方法二:带StringWriter的导出方法
该方法的格式不容易改变 方法与上面原理相同是将数据写入到StringWriter 中 让后到导出
众所周知,Respone.Write()是输出Html流程序给用户的。考虑到一个标准的Web页面的是有多种呈现方式的,
例如:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 这是以标准网页形式输出Html流
<meta http-equiv="Content-Type" content="application/vnd.ms-excel">
<meta http-equiv="Content-Disposition" content="attachment; filename=ex.xls">这是以附件形式输出Html流,而且是将“数据”存放在ex.xls这个表格中。^_^
那么我们以编码的形式如何显示^_^(现在写VB了,给出的也是VB的事例)
1 Dim _DataStringWriter As StringWriter = New StringWriter 定义一个StringWriter对象
2 _DataStringWriter.WiteLine("FirstFieldName" + ControlChars.Tab + "SecondFieldName")给输出的Excel表格每 列加入名称
3 从数据“容器”里面将数据取出。例如
Dim dt as New DataTable
For i as Integer = 0 To dt.Rows.Count - 1 Then
_DataStringWriter.WiteLine(dt(i)(0) + ControlChars.Tab + dt(i)(1))
Next
4 Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.ContentType = "application/vnd.ms-excel"
Response.ContentEncoding = System.Text.Encoding.Unicode
5 Response.Write(_DataStringWriter) 输出Html流
Response.End()
以上已经可以实现将数据导入到Excel表格,如果需要导入Word则Response.ContentType = "application/vnd.ms-excel"中改为Response.ContentType = "application/vnd.ms-word"即可。但是注意将fileName也应随之改变,XX.xls或者XX.doc
方法三:datatable导出
该方法是response一行一行导出的,你可以根据自己的需求添加相应的行。
/// <summary> /// DataTable中的数据导出到Excel并下载 /// </summary> /// <param name="dt">要导出的DataTable</param> /// <param name="FileType">类型</param> /// <param name="FileName">Excel的文件名</param> public void CreateExcel(DataTable dt, string FileType, string FileName) { Response.Clear(); Response.Charset = "UTF-8"; Response.Buffer = true; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AppendHeader("Content-Disposition", "attachment;filename="" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls""); Response.ContentType = FileType; string colHeaders = string.Empty; string ls_item = string.Empty; DataRow[] myRow = dt.Select(); int i = 0; int cl = dt.Columns.Count; foreach (DataRow row in myRow) { for (i = 0; i < cl; i++) { if (i == (cl - 1)) {
//到头换行 ls_item += row[i].ToString() + " "; } else {
//换格 ls_item += row[i].ToString() + " "; } } //一行一行的写入 Response.Output.Write(ls_item); ls_item = string.Empty; } Response.Output.Flush(); Response.End(); }