ASP.NET:頁面儲存為WORD出現的問題!

來源:互聯網
上載者:User

通常項目開發需要把頁面上指定內容儲存為WORD/EXCEL等檔案形式,網上採用比較簡單的方法如下:

Code
protected void btnPageToWord_Click(object sender, ImageClickEventArgs e)
{
    System.IO.StringWriter sw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
    this.jjqqhb_container.Page.EnableViewState = false;
    this.jjqqhb_container.RenderControl(htw);
    Response.Buffer = true;
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/ms-word";
    Response.Charset = "utf-8";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
    Response.AddHeader("Content-Disposition", "attachment;filename=1.doc");
    Response.Write(sw.ToString());
    Response.Flush();
    Response.Close(); 
}

其中jjqqhb_container包含的HTML代碼就是需要轉換為檔案的內容

但是轉換過程出現了3個小問題

(1)儲存的WORD文檔上出現"表單頂端","表單底端"等多餘資訊

解決辦法:因為控制項在<form></form>中,故有表單資訊,你可以刪掉它,把控制項放body裡編輯

 (2)  ........會發生只能在執行 Render() 的過程中調用 RegisterForEventValidation的錯誤提示

解決辦法:this.jjqqhb_container.Page.EnableViewState = false;或者直接修改頁面
<%@ Page Language="C" EnableEventValidation = "false" AutoEventWireup="true" .....>

(3) 類型“XX”的控制項“XX”必須放在具有 runat=server 的表單標記內

解決辦法:在後台檔案中重載VerifyRenderingInServerForm方法,如下所示。

Code
public override void VerifyRenderingInServerForm(Control control) 

    //base.VerifyRenderingInServerForm(control); 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.