asp.net Excel导出及数据格式化处理(摘)
2012-10-25 09:05
579 查看
public void ToExcel(System.Web.UI.Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");//这里是将要导出到execl里的第一列格式化为字符类型。
//e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");//这里是格式化为货币类型。
e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
//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%
}
}
{
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");//这里是将要导出到execl里的第一列格式化为字符类型。
//e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:¥#,###.00");//这里是格式化为货币类型。
e.Row.Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
//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%
}
}
相关文章推荐
- ASP.NET大批量数据处理,97-2003Excel导入、导出
- ASP.NET Export to Excel(导出数据到Excel) & C#导出Excel,某单元格内容长度超过255的处理方法
- asp.net导出GridView数据到Excel
- Asp.Net中如何将数据导出到excel
- asp.net npoi 导出excel VS2010 framework 4.0 handler 一般处理程序
- 将从SQL数据库中查询出来的数据导出为Excel文档ASP.NET(VB)?
- [转]asp.net导出数据到Excel的三种方法
- ASP.net 把 DataGrid 数据导出到 Excel .
- Asp.Net导出GridView数据到Excel
- 黑马程序员之ASP.NET学习笔记:将数据导出到Word或Excel
- Asp.Net 导出 Excel 数据的9种方案
- asp.net导出数据到Excel的几种方法(1/3)
- asp.net导出数据到Excel的三种方法
- 关于ASP.NET 将数据导出成Excel 的总结[补充]
- asp.net Linq把数据导出到Excel的代码
- asp.net 通过 Handler 导出数据至excel (让用户下载)
- Asp.net导出Excel/Csv文本格式数据
- 从数据库导出数据到word、excel、.txt _asp.net技巧
- asp.net从数据库导出数据到word、excel、txt文本文件
- ASP.NET数据列表导出为Excel