asp.net 页面导出EXCEL
2015-12-18 15:23
567 查看
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.DataTable table = QTFactory.BL.rptReport.RptGetUserAnwser(hidID.Value);
DataTable2Excel(table);
}
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.Web.UI.WebControls.GridView dgExport = null;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
curContext.Response.Clear();
curContext.Response.BufferOutput = true;
//解决第一位字符为零时不显示的问题和条码被认定为数字问题。
//string styleText = @"<style> .text { vnd.ms-excel.numberformat:@} </style> ";
//<td style="vnd.ms-excel.numberformat:@"
//设定输出的字符集
curContext.Response.Charset = "GB2312";
//假定导出的文件名为tiaoma.xls
curContext.Response.AppendHeader("Content-Disposition", "attachment;filename= Export.xls");
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;//System.Text.Encoding.GetEncoding("GB2312");
//设置导出文件的格式
curContext.Response.ContentType = "application/ms-excel";
//关闭ViewState
//EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
strWriter = new System.IO.StringWriter(cultureInfo);
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
dgExport = new System.Web.UI.WebControls.GridView();
dgExport.RowDataBound += (GridViewFormat);
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
//GridView1
dgExport.RenderControl(htmlWriter);
//把HTML写回浏览器
curContext.Response.Write(strWriter.ToString());
//指定写入Excel为文本
//curContext.Response.Write(styleText);
curContext.Response.End();
}
}
protected static void GridViewFormat(object sender, GridViewRowEventArgs e)
{
//1) 文本:vnd.ms-excel.numberformat:@
//2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//3) 数字:vnd.ms-excel.numberformat:#,##0.00
//4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
//5) 百分比:vnd.ms-excel.numberformat: #0.00%
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}
}
{
System.Data.DataTable table = QTFactory.BL.rptReport.RptGetUserAnwser(hidID.Value);
DataTable2Excel(table);
}
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.Web.UI.WebControls.GridView dgExport = null;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
curContext.Response.Clear();
curContext.Response.BufferOutput = true;
//解决第一位字符为零时不显示的问题和条码被认定为数字问题。
//string styleText = @"<style> .text { vnd.ms-excel.numberformat:@} </style> ";
//<td style="vnd.ms-excel.numberformat:@"
//设定输出的字符集
curContext.Response.Charset = "GB2312";
//假定导出的文件名为tiaoma.xls
curContext.Response.AppendHeader("Content-Disposition", "attachment;filename= Export.xls");
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;//System.Text.Encoding.GetEncoding("GB2312");
//设置导出文件的格式
curContext.Response.ContentType = "application/ms-excel";
//关闭ViewState
//EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
strWriter = new System.IO.StringWriter(cultureInfo);
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
dgExport = new System.Web.UI.WebControls.GridView();
dgExport.RowDataBound += (GridViewFormat);
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
//GridView1
dgExport.RenderControl(htmlWriter);
//把HTML写回浏览器
curContext.Response.Write(strWriter.ToString());
//指定写入Excel为文本
//curContext.Response.Write(styleText);
curContext.Response.End();
}
}
protected static void GridViewFormat(object sender, GridViewRowEventArgs e)
{
//1) 文本:vnd.ms-excel.numberformat:@
//2) 日期:vnd.ms-excel.numberformat:yyyy/mm/dd
//3) 数字:vnd.ms-excel.numberformat:#,##0.00
//4) 货币:vnd.ms-excel.numberformat:¥#,##0.00
//5) 百分比:vnd.ms-excel.numberformat: #0.00%
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
}
}
相关文章推荐
- ASP.NET中Web.Config配置文件详解与应用
- [转]asp.net解决高并发的方案.
- 我学ASP.Net---新手(分页显示)
- ASP.NET SingalR 点对点聊天实现思路总结
- iis 7 asp.net ajax post 请求字节过大报错问题解决办法
- [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)
- Asp.Net 、Css元素定位及元素显示控制总结
- ASP.NET5
- asp.net mvc 实现博客的时间分类管理
- ASP.NET内核几大对象、ASP.NET核心知识(7)--转载
- AspJpeg组件,按宽高比例,真正生成缩略图
- asp.net string有多行文字
- asp.net5中程序根目录的获取
- asp.net 获取当月的第一天和最后一天示例
- WAF 与 RASP 的安装使用大比拼!
- WAF 与 RASP 的安装使用大比拼!
- asp.net webform中的ext.net使用
- asp.net 判断用户是否使用微信浏览器
- asp.net 验证控件
- 关于Asp.net超时,延长读取sql server数据库的超时时间!(已解决)