小弟最近在遇到了一个问题,在写网站页面时,想通过点击导出按钮导出固定区域的table表格,比如以下代码中控件span中使用innerhtml方法插入的表格,我自己写了一个方法,但点击导出时一直无法成功,具体前后台代码如下,环境是VS2010,
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="web._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
     <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    <asp:Label id="lable1" runat="server" Text=""></asp:Label>
    <div>
        <span id="spaInfo" runat="server"></span>
    </div>
    </form>
</body>
</html>
后台
        protected void Page_Load(object sender, EventArgs e)
        {
            GetInfo();
        }
        public void GetInfo()
        {
            StringBuilder bt = new StringBuilder();
            //表头
            bt.Append("<table cellspacing=\"0\" align=\"Center\" border=\"1\" bordercolor=\"Gray\" style=\"background-color: WhiteSmoke;width: 100%; border-collapse: collapse;\">");
            bt.Append(" <tr align=\"center\" style=\"color: Black; background-color: #C7E0FE; height: 26px;\">");
            bt.Append(" <td height='30' width='10%'> 序号 </td>");
            bt.Append(" <td height='30' width='30%'> 流程guid </td>");
            bt.Append(" <td height='30' width='20%'> 申请人 </td>");
            bt.Append(" <td height='30' width='20%'> 申请人所在部门 </td>");
            bt.Append("</tr>");
            bt.Append(" <tr align=\"center\" >");
            bt.Append(" <td height='30' width='10%'> 1 </td>");
            bt.Append(" <td height='30' width='30%'> 123 </td>");
            bt.Append(" <td height='30' width='20%'> FDH </td>");
            bt.Append(" <td height='30' width='20%'> k </td>");
            bt.Append("</tr>");
            bt.Append(" <tr align=\"center\" >");
            bt.Append(" <td height='30' width='10%'> 2 </td>");
            bt.Append(" <td height='30' width='30%'> 234 </td>");
            bt.Append(" <td height='30' width='20%'> FDH </td>");
            bt.Append(" <td height='30' width='20%'> e </td>");
            bt.Append("</tr>");
            bt.Append("</table>");
            spaInfo.InnerHtml = bt.ToString();
        }
        public static string PrepareDataForExportToOfficeDocument(string Header, string SecondHeader, string Tail, Control DataCtl)
        {
            StringWriter writer = new StringWriter();
            HtmlTextWriter writer2 = new HtmlTextWriter(writer);
            if (Header != "")
            {
                writer2.AddAttribute("align", "center");
                writer2.RenderBeginTag(HtmlTextWriterTag.H1);
                writer2.Write(Header);
                writer2.RenderEndTag();
            }
            if (SecondHeader != "")
            {
                writer2.Write(SecondHeader);
            }
            DataCtl.RenderControl(writer2);
            if (Tail != "")
            {
                writer2.Write(Tail);
            }
            return writer.ToString();
        }        public static void ExportAsOfficeDocument(OfficeDocType ApplicationName, string Data, string FileName, Control DataCtl)
        {
            HttpContext.Current.Session["OfficeExport"] = Data;
            string str = "";
            switch (ApplicationName)
            {
                case OfficeDocType.WORD:
                    str = "doc";
                    break;                case OfficeDocType.EXCEL:
                    str = "xls";
                    break;                case OfficeDocType.XML:
                    str = "xml";
                    break;                default:
                    return;
            }
            string applicationPath = HttpContext.Current.Request.ApplicationPath;
            ScriptManager.RegisterStartupScript(DataCtl.Page, DataCtl.Page.GetType(), Guid.NewGuid().ToString(), "window.open('" + (applicationPath + "officeDownload.aspx?FileType=" + str + "&FileName=" + FileName) + "');", true);
        }
         protected void Button1_Click(object sender, EventArgs e)
        {
            string DateStr = PrepareDataForExportToOfficeDocument("比较", "", "", spaInfo).Replace("border=0", "border=1").Replace("href='#'", "").Replace("color='red'", "");
            ExportAsOfficeDocument(OfficeDocType.EXCEL, DateStr, "比较.xls", spaInfo);
            GetInfo();
        }
以上方法调用了一个officeDownload.aspx页面,页面代码如下
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="officeDownload.aspx.cs" Inherits="web.officeDownload" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
后台
 protected void Page_Load(object sender, EventArgs e)
        {
            base.Response.Clear();
            string str = base.Request["FileType"].ToLower();
            string str2 = base.Request["FileName"].ToString();
            string s = this.Session["OfficeExport"].ToString();
            this.Session.Remove("OfficeExport");
            string str4 = str;
            if (str4 != null)
            {
                if (!(str4 == "doc"))
                {
                    if (str4 != "xls")
                    {
                        if (str4 != "xml")
                        {
                            return;
                        }
                        base.Response.ContentType = "text/XML";
                        base.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(str2));
                    }
                    else
                    {
                        base.Response.ContentType = "application/vnd.ms-excel";
                        base.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(str2));
                    }
                }
                else
                {
                    base.Response.ContentType = "application/vnd.ms-word";
                    base.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(str2) + ".doc;");
                }
                if (str != "xml")
                {
                    base.Response.ContentEncoding = Encoding.GetEncoding("gb2312");
                    base.Response.Charset = "gb2312";
                    base.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\">");
                }
                base.Response.Write(s);
                base.Response.End();
            }
        }
请各位大侠指点一下,最好给出具体的代码,谢谢了。
ASPExcel导出C#