- protected void btnExport_Click(object sender, EventArgs e)
- {
- //if (Session["zhiwu"].ToString().Trim() == "2")
- //{
- // Alert.Show("<b>出现错误啦!</b>即日起,业务员不允许自行导出流向啦。请向直属经理申请,谢谢。", MessageBoxIcon.Error);
- // return;
- //}
- //else
- //{
- //因为Grid1默认只导出当前数据,所以首先设置为每页显示10000条,等有好的解决方案后解决
- Grid1.PageSize = 10000;
- BindGrid();
- Response.ClearContent();
- Response.AddHeader("content-disposition", "attachment; filename=Liuxiang.xls");
- Response.ContentType = "application/excel";
- Response.Charset = "UTF-8";
- Response.ContentEncoding = System.Text.Encoding.UTF8;
- Response.Write(GetGridTableHtml(Grid1));
- Response.End();
- //}
-
- }
- private string GetGridTableHtml(Grid grid)
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">");
- sb.Append("<tr>");
- foreach (GridColumn column in grid.Columns)
- {
- sb.AppendFormat("<td>{0}</td>", column.HeaderText);
- }
- sb.Append("</tr>");
- foreach (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 = Request.Url.AbsoluteUri.Replace(Request.Url.AbsolutePath, "");
- html = html.Replace("src="", "src="" + prefix);
- }
- sb.AppendFormat("<td>{0}</td>", html);
- }
- sb.Append("</tr>");
- }
- sb.Append("</table>");
- return sb.ToString();
- }
复制代码 |