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 编辑过