FineUI 官方论坛
标题:
动态创建的grid,导出excel怎么没有表头
[打印本页]
作者:
周星星
时间:
2013-6-12 21:33
标题:
动态创建的grid,导出excel怎么没有表头
动态创建的grid,导出excel怎么没有表头
作者:
小豬仔→pig/抓
时间:
2013-6-15 18:28
/// <summary>
/// FineUI.Grid导出Excel
/// </summary>
/// <param name="g">ExtApNet.Grid</param>
/// <param name="FileName">文件名</param>
public static void CreateExcelFineUI(FineUI.Grid g, 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.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.Write(GetGridTableHtml(g));
HttpContext.Current.Response.End();
}
/// <summary>
/// 获取数据,在CreateExcelFineUI方法中写入数据
/// </summary>
/// <param name="grid">FineUI.Grid</param>
/// <returns></returns>
private static string GetGridTableHtml(FineUI.Grid grid)
{
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("<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">");
sb.Append("<tr>");
foreach (FineUI.GridColumn column in grid.Columns)
{
sb.AppendFormat("<td>{0}</td>", column.HeaderText);
}
sb.Append("</tr>");
foreach (FineUI.GridRow row in grid.Rows)
{
sb.Append("<tr>");
foreach (object value in row.Values)
{
string html = value.ToString();
// 处理CheckBox
if (html.Contains("box-grid-static-checkbox"))
{
if (html.Contains("box-grid-static-checkbox-uncheck"))
{
html = "×";
}
else
{
html = "√";
}
}
// 处理图片
if (html.Contains("<img"))
{
string prefix = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, "");
html = html.Replace("src="", "src="" + prefix);
}
sb.AppendFormat("<td>{0}</td>", html);
}
sb.Append("</tr>");
}
sb.Append("</table>");
return sb.ToString();
}
#endregion
复制代码
以上是用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();
}
复制代码
作者:
冬天
时间:
2013-6-16 09:17
牛人啊
欢迎光临 FineUI 官方论坛 (https://fineui.com/BBS/)
Powered by Discuz! X3.4