黑马程序员技术交流社区
标题:
数据绑定控件的数据导出到Excel的样式问题
[打印本页]
作者:
宋兴征
时间:
2013-5-10 15:41
标题:
数据绑定控件的数据导出到Excel的样式问题
本帖最后由 宋兴征 于 2013-5-14 14:49 编辑
用的这段导出到Excel的代码:
/// <summary>
/// 将结果集导出到Excel
/// </summary>
/// <param name="dsControl">数据源控件</param>
/// <param name="strFileName">导出的文件名</param>
private void ExportExcel(ref System.Web.UI.WebControls.ListView dsControl, 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 = "utf-8";
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);
dsControl.RenderControl(oHtmlTextWriter);
dsControl = null;
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();
}
复制代码
我是想将ListView里边的内容导出来,终于导出来Excel了,结果打开的时候提示:
导出Excel样式问题1.png
(21.26 KB, 下载次数: 0)
下载附件
2013-5-10 15:39 上传
请问是怎么回事?
然后打开发现,把按钮、css样式等也给导出来了
导出Excel样式问题.png
(6.46 KB, 下载次数: 0)
下载附件
2013-5-10 15:39 上传
我想去除css样式,比如表头的“姓名”的背景色,
还有 “编辑”、“删除”等按钮。
请问该怎么写?
作者:
许庭洲
时间:
2013-5-10 21:29
本帖最后由 许庭洲 于 2013-5-10 21:31 编辑
protected void ExportExcel(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = false;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlEncode(DropListReport.SelectedValue, System.Text.Encoding.UTF8).ToString());
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
this.EnableViewState = false;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridViewResule.AllowPaging = false;
ShowData((Template)Session["TemplateFile"]);
GridViewResule.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
GridViewResule.AllowPaging = true;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2