C#数据导出到Excel文件
来源:互联网 发布:一个公司三家淘宝店 编辑:程序博客网 时间:2024/05/17 06:29
#region 数据导出为EXCEL //转载请注明来自 http://www.shang11.com
public static void CreateExcel(DataTable dt, string fileName)
{
StringBuilder strb = new StringBuilder();
strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
strb.Append(" <style>");
strb.Append(".xl26");
strb.Append(" {mso-style-parent:style0;");
strb.Append(" font-family:\"Times New Roman\", serif;");
strb.Append(" mso-font-charset:0;");
strb.Append(" mso-number-format:\"@\";}");
strb.Append(" </style>");
strb.Append(" <xml>");
strb.Append(" <x:ExcelWorkbook>");
strb.Append(" <x:ExcelWorksheets>");
strb.Append(" <x:ExcelWorksheet>");
strb.Append(" <x:Name>" + fileName + "</x:Name>");
strb.Append(" <x:WorksheetOptions>");
strb.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
strb.Append(" <x:Selected/>");
strb.Append(" <x:Panes>");
strb.Append(" <x:Pane>");
strb.Append(" <x:Number>3</x:Number>");
strb.Append(" <x:ActiveCol>1</x:ActiveCol>");
strb.Append(" </x:Pane>");
strb.Append(" </x:Panes>");
strb.Append(" <x:ProtectContents>False</x:ProtectContents>");
strb.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
strb.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
strb.Append(" </x:WorksheetOptions>");
strb.Append(" </x:ExcelWorksheet>");
strb.Append(" <x:WindowHeight>6750</x:WindowHeight>");
strb.Append(" <x:WindowWidth>10620</x:WindowWidth>");
strb.Append(" <x:WindowTopX>480</x:WindowTopX>");
strb.Append(" <x:WindowTopY>75</x:WindowTopY>");
strb.Append(" <x:ProtectStructure>False</x:ProtectStructure>");
strb.Append(" <x:ProtectWindows>False</x:ProtectWindows>");
strb.Append(" </x:ExcelWorkbook>");
strb.Append(" </xml>");
strb.Append("");
strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'>");
if (dt.Rows.Count > 0)
{
strb.Append("<tr>");
//写列标题
int columncount = dt.Columns.Count;
for (int columi = 0; columi < columncount; columi++)
{
strb.Append(" <td style='text-align:center;'><b>" + ColumnName(dt.Columns[columi].ToString()) + "</b></td>");
}
strb.Append(" </tr>");
//写数据
for (int i = 0; i < dt.Rows.Count; i++)
{
strb.Append(" <tr>");
for (int j = 0; j < dt.Columns.Count; j++)
{
strb.Append(" <td class='xl26'>" + dt.Rows[i][j].ToString() + "</td>");
}
strb.Append(" </tr>");
}
}
strb.Append("</table> </body> </html>");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//HttpContext.Current.p.EnableViewState = false;
HttpContext.Current.Response.Write(strb);
HttpContext.Current.Response.End();
}
#endregion
public static void CreateExcel(DataTable dt, string fileName)
{
StringBuilder strb = new StringBuilder();
strb.Append(" <html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
strb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
strb.Append("xmlns=\"http://www.w3.org/TR/REC-html40\">");
strb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
strb.Append(" <style>");
strb.Append(".xl26");
strb.Append(" {mso-style-parent:style0;");
strb.Append(" font-family:\"Times New Roman\", serif;");
strb.Append(" mso-font-charset:0;");
strb.Append(" mso-number-format:\"@\";}");
strb.Append(" </style>");
strb.Append(" <xml>");
strb.Append(" <x:ExcelWorkbook>");
strb.Append(" <x:ExcelWorksheets>");
strb.Append(" <x:ExcelWorksheet>");
strb.Append(" <x:Name>" + fileName + "</x:Name>");
strb.Append(" <x:WorksheetOptions>");
strb.Append(" <x:DefaultRowHeight>285</x:DefaultRowHeight>");
strb.Append(" <x:Selected/>");
strb.Append(" <x:Panes>");
strb.Append(" <x:Pane>");
strb.Append(" <x:Number>3</x:Number>");
strb.Append(" <x:ActiveCol>1</x:ActiveCol>");
strb.Append(" </x:Pane>");
strb.Append(" </x:Panes>");
strb.Append(" <x:ProtectContents>False</x:ProtectContents>");
strb.Append(" <x:ProtectObjects>False</x:ProtectObjects>");
strb.Append(" <x:ProtectScenarios>False</x:ProtectScenarios>");
strb.Append(" </x:WorksheetOptions>");
strb.Append(" </x:ExcelWorksheet>");
strb.Append(" <x:WindowHeight>6750</x:WindowHeight>");
strb.Append(" <x:WindowWidth>10620</x:WindowWidth>");
strb.Append(" <x:WindowTopX>480</x:WindowTopX>");
strb.Append(" <x:WindowTopY>75</x:WindowTopY>");
strb.Append(" <x:ProtectStructure>False</x:ProtectStructure>");
strb.Append(" <x:ProtectWindows>False</x:ProtectWindows>");
strb.Append(" </x:ExcelWorkbook>");
strb.Append(" </xml>");
strb.Append("");
strb.Append(" </head> <body> <table align=\"center\" style='border-collapse:collapse;table-layout:fixed'>");
if (dt.Rows.Count > 0)
{
strb.Append("<tr>");
//写列标题
int columncount = dt.Columns.Count;
for (int columi = 0; columi < columncount; columi++)
{
strb.Append(" <td style='text-align:center;'><b>" + ColumnName(dt.Columns[columi].ToString()) + "</b></td>");
}
strb.Append(" </tr>");
//写数据
for (int i = 0; i < dt.Rows.Count; i++)
{
strb.Append(" <tr>");
for (int j = 0; j < dt.Columns.Count; j++)
{
strb.Append(" <td class='xl26'>" + dt.Rows[i][j].ToString() + "</td>");
}
strb.Append(" </tr>");
}
}
strb.Append("</table> </body> </html>");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//HttpContext.Current.p.EnableViewState = false;
HttpContext.Current.Response.Write(strb);
HttpContext.Current.Response.End();
}
#endregion
0 0
- C#导出数据到Excel文件
- C#数据导出到Excel文件
- C#导出Excel|导出数据到Excel模板文件里
- c#导出数据到Excel
- C#数据导出到Excel
- C#数据导出到Excel
- C#导出数据到excel
- C# 导出数据到Excel
- C# 数据导出到Excel
- c# 导出数据到 excel
- C#导出数据到excel
- C# Asp.Net导出数据集到Excel文件
- GridView(DataSet)导出数据到Excel文件代码C#
- C# 将listview 中的数据导出到excel 文件
- VB C# listview 中的数据导出到excel 文件
- C# 导出DataTable数据到Excel文件中
- c# listview数据导出到生成的excel文件
- C#创建Excel文件并将数据导出到Excel文件
- Unity引用外部DLL库
- ES-Hadoop学习笔记-Spark交互
- chrome窗口和标签页快捷键
- 二叉树建立、存储、遍历
- python easy_install django
- C#数据导出到Excel文件
- 用 gparted 在 ubuntu 上合并分区
- Mac系统终端命令行不执行命令 总出现command not found解决方法
- RecycleView的上啦加载下拉刷新
- 海量数据处理-数据结构
- mysql数据库 主从复制的配置
- DocumentBuilderFactory.setNamespaceAware(true)慎用
- Yii2.0目录结构记录
- Java的基本程序设计