首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >空Word文档ASP.Net MVC Spire.Doc

空Word文档ASP.Net MVC Spire.Doc
EN

Stack Overflow用户
提问于 2022-01-11 09:14:26
回答 1查看 379关注 0票数 0

我正在尝试使用Spire.Doc生成一个Word文档并下载它。我可以下载这份文件,但它是空的。有谁知道这是为什么,也能帮我吗?这是我的密码:

代码语言:javascript
复制
Document doc = new Document();
        Paragraph paragraph = doc.AddSection().AddParagraph();
        paragraph.AppendText("Hello World!");
        doc.SaveToFile("Example.doc", FileFormat.Doc);
        //Saves the Word document to  MemoryStream
        MemoryStream stream = new MemoryStream();
        stream.Position = 0;


        //Download Word document in the browser
        return File(stream, "application/msword", "Example.doc");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-11 10:39:18

下面是一个使用.NET Core3.1和FreeSpire.Doc v9.9.7的工作示例

代码语言:javascript
复制
[HttpGet]
public IActionResult DownloadWordDoc()
{
    //Create the document object;
    Document doc = new Document();

    //Create a paragraph and add some text
    var paragraph = doc.AddSection().AddParagraph();
    paragraph.AppendText("Hello World!");

    //Create and save the document to a memory stream
    var ms = new MemoryStream();
    doc.SaveToStream(ms, FileFormat.Docx);
    ms.Seek(0, SeekOrigin.Begin);

    //You may want to make this MIME type string into a constant
    var fsr = new FileStreamResult(ms, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    fsr.FileDownloadName = "Example.docx";

    return fsr;
}

您可能还需要以下使用语句,但我假设您已经有了它们。

代码语言:javascript
复制
using System.IO;
using Spire.Doc;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70664213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档