簡體   English   中英

將HTML導出到ASP.NET中的MS WORD docx文件

[英]Export HTML to MS WORD docx file in ASP.NET

我想將我的 aspx 頁面上的 html 表導出到 MS WORD ( .docx ) 文件。 我已經有了導出.doc文件的代碼,但我想以 docx 擴展名導出。

protected void btnExport_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "application/vnd.openxmlformatsofficedocument.wordprocessingml.documet";
    Response.AddHeader("Content-Disposition", "attachment; filename=WORK_ORDER.doc");
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    Response.Charset = "";
    EnableViewState = false;
    System.IO.StringWriter writer = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
    xx.RenderControl(html);
    Response.Write(writer);
    Response.End();
}      

Response.AddHeader("Content-Disposition", "attachment; filename=WORK_ORDER.docx");

代替

Response.AddHeader("Content-Disposition", "attachment; filename=WORK_ORDER.doc");

考慮到原始代碼從來沒有創建一個真正的 Word 文件,只是在一些 HTML 上放置一個.doc擴展名來欺騙 Windows *讓 Word(或等效程序)嘗試打開文件(並希望呈現 HTML),這很可能是你需要做的是在現有filename屬性的末尾添加一個x ,您將獲得與之前完全相同的結果。


* Mac OS 查看文件頭而不是擴展名,因此這可能對 Mac 用戶從未起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM