原文: http://topic.csdn.net/u/20081102/13/c07a4eb4-0f24-45cd-bc7e-e3fbeeff857e.html
void ExportExcel( System.Web.UI.WebControls.DataList dl, string strFileName)
{
strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.Charset = "gb2312";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "online; filename=" + strFileName + ".xls");
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
dl.RenderControl(oHtmlTextWriter);
System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString().Replace("<td", "<td STYLE='MSO-NUMBER-FORMAT:\\@'"));
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.End();
}
1Response.Clear()
2 Response.BufferOutput = True
3 Response.Charset = "GB2312"
4 Response.AppendHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode("学员建议--" & Me.courseName, System.Text.Encoding.UTF8) & ".xls")
5 Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
6 Response.ContentType = "application/ms-excel"
7 dlstGetAndSuggest.EnableViewState = False
8 Dim myCItrad As New System.Globalization.CultureInfo("ZH-CN", True)
9 Dim oStringWriter As New System.IO.StringWriter(myCItrad)
10 Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
11 Me.dlstGetAndSuggest.RenderControl(oHtmlTextWriter)
12 Response.Write(oStringWriter.ToString())
13 Response.End()说明:
第4行,如果把文件名直接写作字符串格式,在导出时文件名会出现乱码,用HttpUtility.UrlEncode("学员建议--" & Me.courseName, System.Text.Encoding.UTF8),可以防止文件名为乱码。
第3行和第5行,防止原文中的内容导出到Excel文件时出现乱码。
posted on 2008-12-21 12:44
Documents 阅读(612)
评论(0) 编辑 收藏 所属分类:
Web 、
ASP.NET