asp.net 页面导出EXCEL
来源:互联网 发布:eclipse怎么连接数据库 编辑:程序博客网 时间:2024/05/29 11:02
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:@");
}
}
}
0 0
- asp.net 页面导出EXCEL
- ASP.NET页面内容导出为Excel
- .NET页面导出Excel
- .NET页面导出Excel
- asp.net导出excel
- asp.net导出Excel
- asp.net导出excel
- asp.net导出Excel
- Asp.net 导出Excel
- asp.net导出excel
- asp.net 导出excel
- asp.net 导出Excel
- asp.net 导出 excel
- asp.net导出Excel
- ASP.NET Excel导出
- asp.net导出EXCEL
- ASP.NET Excel导出
- asp.net 导出Excel
- Android apk动态加载机制的研究
- mac的逻辑文件存储结构和windows的区别
- 不能使用sudo命令
- 黑马程序员 - OC语言 - 面向对象三大特性,封装、继承、多态
- 算法与设计实验一
- asp.net 页面导出EXCEL
- 获取2个经纬度坐标之间的距离
- Windows下强制删除文件的批处理文件
- 博弈之Nim浅谈
- label基本使用 ----》更高级使用请参考 attributeString
- actionbar基本使用
- [研发与团队]和平典范的例子
- 第三章 一元和二元操作符
- 【总结】EditText的使用