【问题标题】:Creation of Word Document file创建 Word 文档文件
【发布时间】:2010-05-11 06:02:47
【问题描述】:

如何使用 ASP.Net 和 C# (2008) 创建扩展名为 .doc 和 .docx 的 Word 文档文件?

【问题讨论】:

标签: c# asp.net


【解决方案1】:

我更喜欢避免使用 com 路由,而是在响应流中生成文档。这对我来说非常有效。希望对您有所帮助。

        Response.ContentType = "application/msword";
        Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
        Response.Charset = "UTF-8";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + "mydoc.doc");

        Response.Write("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>");
        Response.Write("<head>");
        Response.Write("<!--[if gte mso 9]> <xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>100</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml> <![endif]-->");
        Response.Write("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"\"text/html; charset=UTF-8\"\">");
        Response.Write("<meta name=ProgId content=Word.Document>");
        Response.Write("<meta name=Generator content=\"Microsoft Word 9\">");
        Response.Write("<meta name=Originator content=\"Microsoft Word 9\">");
        Response.Write("</head>");
        Response.Write("<body>");
        Response.Write("<div class=Section2>");

        // write some content here

        Response.Write("</body>");
        Response.Write("</html>");
        HttpContext.Current.Response.Flush();

【讨论】:

  • 谢谢。我会检查一下。那么在脚本任务编码部分,我为什么不能维护我添加供参考的文件?每次我去编辑脚本时,我都必须添加文件以供参考以恢复编码。是否有任何其他设置来维护添加的文件以供参考?
  • 获取“响应”的命名空间是什么?我没有得到 System.Web.HttpResponse !!!!!!!
  • 我不关注你的第一条评论。也许您可以进一步解释,有人可以插话。至于响应,如果您从网络表单中调用它,您应该可以按原样使用。奇怪的。您可能想尝试 Page.Response 或 Context.Response 或 System.Web.HttpContext.Current.Response。
  • 没有。我没有同时获得 Page.Response 或 Context.Response 或 System.Web.HttpContext.Current.Response。我在 SSIS 中使用脚本任务编辑器。
  • 我不熟悉脚本任务编辑器。我建议更新这篇文章的标签,让一些人熟悉这里。
【解决方案2】:
【解决方案3】:

OfficeWriter 是针对 ASP.NET 使用而优化的 DOC 和 DOCX API。您可以使用它创建两种文件格式:

http://www.officewriter.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多