我正在嘗試使用 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");
uj5u.com熱心網友回復:
這是一個使用 .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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408230.html
標籤:
