Excel 导出
2009-06-26 10:00
127 查看
最近在做Excel导出的时用户突然提出需要对文件设置页眉和页脚,这个问题很是让人头痛.在网上查找资料的时候发现都做得很是复杂,最后自己对文件进行代码分析通过设置前后的不同发现可以用样式表进行处理,真个汗呀!下面代码贴出来分享一下.
/// <summary>
/// 传入的DataTable
/// </summary>
/// <param name="dt"></param>
/// <param name="xlsName"></param>
public static void DataTableToExcel(DataTable dt, string xlsName)
{
xlsName = xlsName.Replace("查询", "");
string style = @"<style> .text { mso-number-format:/@; } ";
string styleSetup = @"@page
{mso-header-data:&C&/0022宋体/,加粗/0022&18&A;
mso-footer-data:";
styleSetup=styleSetup+'"'+" 第 &P 页,共 &N 页"+'"'+';';
string other = @"margin:.98in .75in .98in .75in;
mso-header-margin:.51in;
mso-footer-margin:.51in;
mso-page-orientation:landscape;}";
style = style + styleSetup + other + "</style>";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(xlsName, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.Charset = "utf-7";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
HttpContext.Current.Response.ContentType = "application/ms-excel";
GridView gv = new GridView();
gv.Font.Size = 11;
gv.DataSource = dt;
gv.DataBind();
gv.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gv.RenderControl(hw);
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
/// <summary>
/// 传入的DataTable
/// </summary>
/// <param name="dt"></param>
/// <param name="xlsName"></param>
public static void DataTableToExcel(DataTable dt, string xlsName)
{
xlsName = xlsName.Replace("查询", "");
string style = @"<style> .text { mso-number-format:/@; } ";
string styleSetup = @"@page
{mso-header-data:&C&/0022宋体/,加粗/0022&18&A;
mso-footer-data:";
styleSetup=styleSetup+'"'+" 第 &P 页,共 &N 页"+'"'+';';
string other = @"margin:.98in .75in .98in .75in;
mso-header-margin:.51in;
mso-footer-margin:.51in;
mso-page-orientation:landscape;}";
style = style + styleSetup + other + "</style>";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(xlsName, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.Charset = "utf-7";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
HttpContext.Current.Response.ContentType = "application/ms-excel";
GridView gv = new GridView();
gv.Font.Size = 11;
gv.DataSource = dt;
gv.DataBind();
gv.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gv.RenderControl(hw);
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
相关文章推荐
- Spring实现的动态文件下载(以Excel导出为例)
- 导出内容到word、excel的一些方法
- kettle 使用excel模板导出数据
- Jxls+Spring MVC实现Excel导出
- 【Excel】java 导出Excel 中文文件名乱码不显示问题
- POI3.8 导出大数据excel(50万左右)
- 怎样把DataGrid的数据导出到Excel以供打印(杂项)
- BS导出Excel实例
- PHPExcel导出
- Java将数据导出到Excel文件中
- 在程序中直接执行SQL语句;将DGrid中的数据导出EXCEL
- C# 操作EXCEL(C#中的数据导出EXCEL)
- ExtJS实现Excel导出
- 在SSM下使用POI实现Excel表的导入/导出
- 将ListView中的内容以Excel导出
- 导出excel(sqlserver)
- C#中导出excel文档
- GridView导出到Excel或Word文件
- java实现excel的导入导出(poi详解)
- 机房收费系统之导出Excel