public static void OutputExcel(Nandasoft.WebControls.NDGridView grid, string excelFileName) { Page page = (Page)HttpContext.Current.Handler; string[] flag = new string[grid.Columns.Count]; for (int i = 0; i < grid.Columns.Count; i++) { DataControlField col = grid.Columns[i]; //只有可见的邦定列和选择列才输出 if (col.Visible && (col.GetType() == typeof(BoundField) || col.GetType() == typeof(CheckBoxField))) { flag[i] = col.HeaderText; } else { flag[i] = ""; } } page.Response.Clear(); string fileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(excelFileName)); page.Response.AddHeader("Content-Disposition", "filename=" + fileName + ".xls"); page.Response.ContentType = "application/vnd.ms-excel"; page.Response.Charset = "utf-8"; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<HTML><HEAD><TITLE>" + fileName + "</TITLE><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"); sb.Append("<table border=1>"); sb.Append("<tr><b>"); for (int i = 0; i < grid.Columns.Count; i++) { if (flag[i] != "") { sb.Append("<td>" + grid.Columns[i].HeaderText + "</td>"); } } sb.Append("</b></tr>"); for (int i = 0; i < grid.Rows.Count; i++) { if (((CheckBox)grid.Rows[i].Cells[1].FindControl("CheckBoxSelect")).Checked == true) { sb.AppendLine("<tr>"); for (int j = 0; j < grid.Columns.Count; j++) { if (flag[j] != "") { if (grid.Rows[i].Cells[j].Controls.Count > 0) { if (grid.Rows[i].Cells[j].Controls[0].GetType().ToString().Trim() == "System.Web.UI.WebControls.CheckBox") { if (((CheckBox)grid.Rows[i].Cells[j].Controls[0]).Checked) { sb.Append("<td>是</td>"); } else { sb.Append("<td>否</td>"); } } else { sb.Append("<td>" + grid.Rows[i].Cells[j].Text + "</td>"); } } else { sb.Append("<td>" + grid.Rows[i].Cells[j].Text + "</td>"); } } } sb.AppendLine("</tr>"); } } sb.Append("</table>"); sb.Append("</body></html>"); page.Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(sb.ToString())); page.Response.End(); }
public static void OutputExcel(Nandasoft.WebControls.NDGridView gird, DataTable dt, string excelFileName) { Page page = (Page)HttpContext.Current.Handler; page.Response.Clear(); string fileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(excelFileName)); page.Response.AddHeader("Content-Disposition", "filename=" + fileName + ".xls"); page.Response.ContentType = "application/vnd.ms-excel"; page.Response.Charset = "utf-8"; StringBuilder s = new StringBuilder(); s.Append("<HTML><HEAD><TITLE>" + fileName + "</TITLE><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"); string FildContent = ""; foreach (DataControlField field in gird.Columns) { if (field.GetType() == typeof(BoundField) || field.GetType() == typeof(CheckBoxField)) { BoundField col = (BoundField)field; if (col.Visible) { FildContent += col.DataField.ToString() + "," + col.HeaderText + "*"; } } } DataTable temp = new DataTable(); string[] arr = FildContent.Split('*'); for (int n = 0; n < arr.Length - 1; n++) { string[] data = arr[n].Split(','); temp.Columns.Add(data[0].ToString()); } DataRow drr = temp.NewRow(); DataRow drrr = temp.NewRow(); for (int m = 0; m < arr.Length - 1; m++) { string[] da = arr[m].Split(','); drr[da[0].ToString()] = da[0].ToString(); drrr[da[0].ToString()] = da[1].ToString(); } temp.Rows.Add(drrr); temp.Rows.Add(drr); foreach (DataRow dr in dt.Rows) { DataRow drTemp = temp.NewRow(); for (int a = 0; a < temp.Columns.Count; a++) { drTemp[temp.Rows[1][a].ToString()] = dr[temp.Rows[1][a].ToString()].ToString(); } temp.Rows.Add(drTemp); } temp.Rows.RemoveAt(1); foreach (DataRow dr in temp.Rows) { for (int i = 0; i < temp.Columns.Count; i++) { if (dr[i].ToString().ToLower().Trim() == "false") { dr[i] = "否"; } if (dr[i].ToString().ToLower().Trim() == "true") { dr[i] = "是"; } } } s.Append("<table>"); int count = temp.Columns.Count; foreach (DataRow dr in temp.Rows) { s.AppendLine("<tr>"); for (int n = 0; n < count; n++) { s.Append("<td>" + dr[n].ToString() + "</td>"); } s.AppendLine("</tr>"); } s.Append("</table>"); s.Append("</body></html>"); page.Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(s.ToString())); page.Response.End(); }