0

我正在尝试使用 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");
4

1 回答 1

0

这是一个使用 .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;
于 2022-01-11T10:39:18.493 回答