日期:2014-05-17  浏览次数:20448 次

.NET web页面导出word文档问题
 protected void Button1_Click(object sender, EventArgs e)
    {
     
        //ExpertControl(table1, DocumentType.Word);

        Response.Clear();
        Response.BufferOutput = true;
        //设定输出的字符集
        Response.Charset = "GB2312";
        //假定导出的文件名为FileName.doc
        Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.doc");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        ////设置导出文件的格式
        Response.ContentType = "application/ms-word";
        //关闭ViewState
        table1.EnableViewState = false;
        System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
        System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
        table1.RenderControl(textWriter);
        ////把HTML写回浏览器
        Response.Write(stringWriter.ToString());
        Response.End();

    }


        <table cellpadding="0" cellspacing="0" class="style1" runat="server" id="table1">
            <tr>
前台控件
--------------------------------------------

类型“Button”的控件“Button1”必须放在具有 runat=server 的窗体标记内。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Web.HttpException: 类型“Button”的控件“Button1”必须放在具有 runat=server 的窗体标记内。

源错误: 


行 108:        System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
行 109:        System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
行 110:        table1.RenderControl(textWriter);
行 111:        ////把HTML写回浏览器
行 112:        Response.Write(stringWriter.ToString());
 
怎么解决呢就求救
------最佳解决方案--------------------
/// <summary>
        /// 将Web控件导出
        /// </summary>
        /// <param name="source">控件实例</param>
      &n