需要支持使用StringBuilder生成word文档

本文关键字:生成 word 文档 StringBuilder 支持 | 更新日期: 2023-09-27 18:28:06

我正在尝试使用StringBuilder生成一个word文档,如下

    [WebMethod]
    public static void ExportToWord(string HTMLContent)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/msword";
        string strFileName = "GenerateDocument" + ".doc";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
        System.Text.StringBuilder strHTMLContent = new System.Text.StringBuilder();
        strHTMLContent.Append(HTMLContent);

        HttpContext.Current.Response.Write(strHTMLContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();         
    }

问题是,当我看到下载的文档时,它的剩余页面内容添加了上述<div>内容

需要支持使用StringBuilder生成word文档

我在开头缺少Response.Clear,在结尾缺少Response.End()