public static String GetGridTableHtml(FineUIPro.Grid grid) { StringBuilder sb = new StringBuilder(); MultiHeaderTable mht = new MultiHeaderTable(); mht.ResolveMultiHeaderTable(grid.Columns); sb.Append("<meta http-equiv=\"content-type\" content=\"application/excel; charset=UTF-8\"/>"); sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">"); foreach (List <object[]> rows in mht.MultiTable) { sb.Append("<tr>"); foreach (object[] cell in rows) { int rowspan = Convert.ToInt32(cell[0]); int colspan = Convert.ToInt32(cell[1]); GridColumn column = cell[2] as GridColumn; sb.AppendFormat("<th{0}{1}{2}>{3}</th>", rowspan != 1 ? " rowspan=\"" + rowspan + "\"" : "", colspan != 1 ? " colspan=\"" + colspan + "\"" : "", colspan != 1 ? " style=\"text-align:center;\"" : "", column.HeaderText); } sb.Append("</tr>"); } foreach (GridRow row in grid.Rows) { sb.Append("<tr>"); foreach (GridColumn column in mht.Columns) { string html = row.Values[column.ColumnIndex].ToString(); if (column.ColumnID == "tfNumber") { html = (row.FindControl("spanNumber") as System.Web.UI.HtmlControls.HtmlGenericControl).InnerText; } else if (column.ColumnID == "tfNumber1") { html = (row.FindControl("spanNumber1") as System.Web.UI.HtmlControls.HtmlGenericControl).InnerText; } else if (column.ColumnID == "tfGender") { html = (row.FindControl("labGender") as FineUIPro.Label).Text; } sb.AppendFormat("<td>{0}</td>", html); } sb.Append("</tr>"); } sb.Append("</table>"); return(sb.ToString()); }
// GET: Grid/ExcelGroupField/ExportToExcel public ActionResult ExportToExcel(JArray content) { DataTable source = DataSourceUtil.GetDataTable(); //string TH_HTML = "<th>{0}</th>"; string TD_HTML = "<td>{0}</td>"; //string TD_IMAGE_HTML = "<td><img src=\"{0}\"></td>"; StringBuilder sb = new StringBuilder(); sb.Append("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel;charset=utf-8\"/>"); sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">"); MultiHeaderTable mht = new MultiHeaderTable(); mht.ResolveMultiHeaderTable(content); foreach (List <object[]> rows in mht.MultiTable) { sb.Append("<tr>"); foreach (object[] cell in rows) { int rowspan = Convert.ToInt32(cell[0]); int colspan = Convert.ToInt32(cell[1]); JObject column = cell[2] as JObject; sb.AppendFormat("<th{0}{1}{2}>{3}</th>", rowspan != 1 ? " rowspan=\"" + rowspan + "\"" : "", colspan != 1 ? " colspan=\"" + colspan + "\"" : "", colspan != 1 ? " style=\"text-align:center;\"" : "", column.Value <string>("text")); } sb.Append("</tr>"); } int rowIndex = 1; foreach (DataRow row in source.Rows) { sb.Append("<tr>"); sb.AppendFormat(TD_HTML, rowIndex++); sb.AppendFormat(TD_HTML, row["Name"]); sb.AppendFormat(TD_HTML, row["Gender"].ToString() == "1" ? "男" : "女"); sb.AppendFormat(TD_HTML, row["ChineseScore"]); sb.AppendFormat(TD_HTML, row["MathScore"]); sb.AppendFormat(TD_HTML, row["TotalScore"]); sb.AppendFormat(TD_HTML, row["Major"]); sb.AppendFormat(TD_HTML, ((DateTime)row["LogTime"]).ToString("yyyy-MM-dd")); sb.Append("</tr>"); } sb.Append("</table>"); return(File(Encoding.UTF8.GetBytes(sb.ToString()), "application/vnd.ms-excel", "myexcel.xls")); }
public static String GetGridTableHtml(FineUIPro.Grid grid, DataTable dataSource) { StringBuilder sb = new StringBuilder(); MultiHeaderTable mht = new MultiHeaderTable(); mht.ResolveMultiHeaderTable(grid.Columns); sb.Append("<meta http-equiv=\"content-type\" content=\"application/excel; charset=UTF-8\"/>"); sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">"); foreach (List <object[]> rows in mht.MultiTable) { sb.Append("<tr>"); foreach (object[] cell in rows) { int rowspan = Convert.ToInt32(cell[0]); int colspan = Convert.ToInt32(cell[1]); GridColumn column = cell[2] as GridColumn; sb.AppendFormat("<th{0}{1}{2}>{3}</th>", rowspan != 1 ? " rowspan=\"" + rowspan + "\"" : "", colspan != 1 ? " colspan=\"" + colspan + "\"" : "", colspan != 1 ? " style=\"text-align:center;\"" : "", column.HeaderText); } sb.Append("</tr>"); } foreach (DataRow row in dataSource.Rows) { sb.Append("<tr>"); for (int i = 0; i < dataSource.Columns.Count; i++) { sb.AppendFormat("<td>{0}</td>", row[i]); } sb.Append("</tr>"); } sb.Append("</table>"); return(sb.ToString()); }