MiniWord
推薦一個 .NET 開源的免費 Word 處理神器 MiniWord。這是一個非常簡單有效的 .NET Word 模板庫,甚至可以使用一行代碼處理 Word,非常方便。并且不需要安裝微軟 Word,無需 COM+ 和互操作支持 Linux 和 Mac,輕量級 Word 處理神器。

快速使用
模板遵循“所見即所得”設計,模板標簽樣式完整保留。
var value = new Dictionary<string, object>(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);

MiniWord 模板格式字符串如 Vue、React {{tag}} ,用戶只需確保 tag 和 value 參數 key 相同,系統會自動替換它們。
var value = new Dictionary<string, object>()
{
["Name"] = "Jack",
["Department"] = "IT Department",
["Purpose"] = "Shanghai site needs a new system to control HR system.",
["StartDate"] = DateTime.Parse("2022-09-07 08:30:00"),
["EndDate"] = DateTime.Parse("2022-09-15 15:30:00"),
["Approved"] = true,
["Total_Amount"] = 123456,
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
模板

結果

.NET Core 使用
public IActionResult DownloadWordFromTemplatePath()
{
string templatePath = "TestTemplateComplex.docx";
Dictionary<string, object> value = defaultValue;
MemoryStream memoryStream = new MemoryStream();
MiniWord.SaveAsByTemplate(memoryStream, templatePath, value);
memoryStream.Seek(0, SeekOrigin.Begin);
returnnew FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
FileDownloadName = "demo.docx"
};
}
public IActionResult DownloadWordFromTemplateBytes()
{
byte[] bytes = TemplateBytesCache["TestTemplateComplex.docx"];
Dictionary<string, object> value = defaultValue;
MemoryStream memoryStream = new MemoryStream();
MiniWord.SaveAsByTemplate(memoryStream, bytes, value);
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
{
FileDownloadName = "demo.docx"
};
}
MiniWord 的更多功能,等待您去發現!
- EOF -
閱讀原文:原文鏈接
該文章在 2025/5/26 12:29:10 編輯過