空 Word 文档 ASP.Net MVC Spire.Doc

Empty Word Document ASP.Net MVC Spire.Doc

我正在尝试使用 Spire.Doc 生成 Word 文档并下载它。我可以下载文档,但它是空的。有谁知道为什么会这样并且可以帮助我吗?这是我的代码:

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");

这是一个使用 .NET Core 3.1 和 FreeSpire.Doc v9.9.7

的工作示例
[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;
}

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

using System.IO;
using Spire.Doc;