|
以上是用FineUI原先的导出,以下是重新写的,可以自定义表头。- /// <summary>
- /// DataSet导出Excel
- /// </summary>
- /// <param name="ds">要导出的DataSet</param>
- /// <param name="arrTitle">列标题,若为null,则直接取dataset列标题</param>
- /// <param name="FileName">文件名</param>
- public static void CreateExcelFineUI(DataSet ds, string[] arrTitle, string FileName)
- {
- HttpContext.Current.Response.ClearContent();
- HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
- HttpContext.Current.Response.Charset = "GB2312";
- HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
- HttpContext.Current.Response.ContentType = "application/ms-excel";
- StringBuilder sb = new StringBuilder();
- sb.Append(" <html xmlns:o="urn:schemas-microsoft-com:office:office"");
- sb.Append("xmlns:x="urn:schemas-microsoft-com:office:excel"");
- sb.Append("xmlns="http://www.w3.org/TR/REC-html40"");
- sb.Append(" <head> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>");
- sb.Append(" <style>");
- sb.Append(".xl26");
- sb.Append(" {mso-style-parent:style0;");
- sb.Append(" font-family:"Times New Roman", serif;");
- sb.Append(" mso-font-charset:0;");
- sb.Append(" mso-number-format:"@";}");
- sb.Append(" </style>");
- sb.Append(" <xml>");
- sb.Append(" <x:ExcelWorkbook>");
- sb.Append(" <x:ExcelWorksheets>");
- sb.Append(" <x:ExcelWorksheet>");
- sb.Append(" <x:Name>Sheet1 </x:Name>");
- sb.Append(" <x:WorksheetOptions>");
- sb.Append(" <x:DefaultRowHeight>285 </x:DefaultRowHeight>");
- sb.Append(" <x:Selected/>");
- sb.Append(" <x:Panes>");
- sb.Append(" <x:Pane>");
- sb.Append(" <x:Number>3 </x:Number>");
- sb.Append(" <x:ActiveCol>1 </x:ActiveCol>");
- sb.Append(" </x:Pane>");
- sb.Append(" </x:Panes>");
- sb.Append(" <x:ProtectContents>False </x:ProtectContents>");
- sb.Append(" <x:ProtectObjects>False </x:ProtectObjects>");
- sb.Append(" <x:ProtectScenarios>False </x:ProtectScenarios>");
- sb.Append(" </x:WorksheetOptions>");
- sb.Append(" </x:ExcelWorksheet>");
- sb.Append(" <x:WindowHeight>6750 </x:WindowHeight>");
- sb.Append(" <x:WindowWidth>10620 </x:WindowWidth>");
- sb.Append(" <x:WindowTopX>480 </x:WindowTopX>");
- sb.Append(" <x:WindowTopY>75 </x:WindowTopY>");
- sb.Append(" <x:ProtectStructure>False </x:ProtectStructure>");
- sb.Append(" <x:ProtectWindows>False </x:ProtectWindows>");
- sb.Append(" </x:ExcelWorkbook>");
- sb.Append(" </xml>");
- sb.Append("");
- sb.Append(" </head> <body> <table align="center" style='border-collapse:collapse;table-layout:fixed' border="1">");
- sb.Append("<tr><td colspan="" + Convert.ToString(ds.Tables[0].Columns.Count) + "" align="center"><b>" + FileName + "</b></td></tr>");
- sb.Append("<tr>");
- foreach (string strColumnTitle in arrTitle)
- {
- sb.AppendFormat("<td><center>{0}</center></td>", strColumnTitle);
- }
- sb.Append("</tr>");
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- sb.Append(" <tr>");
- for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
- {
- sb.Append(" <td><center>" + ds.Tables[0].Rows[i][j].ToString() + "</center></td>");
- }
- sb.Append(" </tr>");
- }
- sb.Append("</table>");
- HttpContext.Current.Response.Write(sb.ToString());
- HttpContext.Current.Response.End();
- }
复制代码 |
|