将DataTable数据导出Excel
2010-11-26 10:09
411 查看
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=gb2312'>"); strb.Append(" <mce: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(" --></mce:style><style mce_bogus="1">"); 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>Sheet1 </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" mce_style="border-collapse:collapse;table-layout:fixed"> <tr>"); if (ds.Tables.Count > 0) { //写列标题 int columncount = ds.Tables[0].Columns.Count; for (int columi = 0; columi < columncount; columi++) { strb.Append(" <td> <b>" + ds.Tables[0].Columns[columi] + " </b> </td>"); } strb.Append(" </tr>"); //写数据 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { strb.Append(" <tr>"); for (int j = 0; j < ds.Tables[0].Columns.Count; j++) { strb.Append(" <td class='xl26'>" + ds.Tables[0].Rows[i][j].ToString() + " </td>"); } strb.Append(" </tr>"); } } strb.Append(" </body> </html>"); newPage.Response.Clear(); newPage.Response.Buffer = true; newPage.Response.Charset = "GB2312"; string strTemp = string.Format("attachment;filename={0}", newPage.Server.UrlEncode(fileName)); newPage.Response.AppendHeader("Content-Disposition", strTemp); newPage.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 newPage.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 newPage.EnableViewState = false; newPage.Response.Write(strb); newPage.Response.End();
相关文章推荐
- wpf 异步导出 DataGrid 数据到 excel(包括转换 List 到 DataTable)
- DataSet数据导出为Excel文档(每个DataTable为一个Sheet)
- c#使用aspose.cells 从datatable导出数据到excel
- c#将dataTable中的数据导出到Excel表中
- DataTable导出到Excel数据
- asp.net 快速简单的导出数据 DataTable导出到Excel、GSV
- DataTable中的数据导出Excel文件
- c#使用aspose.cells 从datatable导出数据到excel
- DataTable中的数据导出Excel文件
- DataSet、DataTable中的数据导出为Excel的解决方法
- C# 将内存中的datatable数据导出为Excel(方法二,创建Excel对象导出)
- asp.net实现导出DataTable数据到Word或者Excel的方法
- C# 将DataTable里面的数据导出到excel
- 将DataTable中的数据导出成Excel
- DataTable 更改在有数据列的类型方法+DataTable 导出excel功能
- DataTable数据表导出Excel (传统方法)
- C# 获取DataTable数据导出到Excel
- [ASP.NET-C#]将DataSet、DataTable中的数据导出为Excel的解决方法
- 将DataTable数据导出到Excel文件中(xls)
- C# 导出DataTable数据到Excel文件中