位置:首页 > C# > MiniWord:一款适合 .NET 项目的免费开源 Word 模板库

MiniWord:一款适合 .NET 项目的免费开源 Word 模板库

时间:2026-08-25  |  作者:夜鞌不睡  |  阅读:0

前言

给大家推荐一款.NET 免费开源 Word 处理解决方案——MiniWord。只需几行代码即可实现对 Word 文档的强大控制力。

无论您使用的是Windows、Linux还是Mac系统,MiniWord都能为您带来稳定且一致的文档处理体验,无需再依赖Microsoft Office套件或其他复杂组件。接下来,本文将为您详细介绍MiniWord的核心功能,以及它在实际应用中所具备的显著优势。

项目介绍

MiniWord 是一款简单而高效的 .NET Word 模板库。仅用一行代码即可轻松处理 Word 文档,极大地提升了开发效率。

不依赖于 Microsoft Word 的安装,也不需要 COM+ 或互操作组件,同时支持在 Linux 和 Mac 平台上运行,是一款跨平台的轻量级 Word 处理解决方案。

项目使用

1、安装 NuGet

www.nuget.org/packages/Mi…

2、快速使用

模板遵循"所见即所得"的设计,模板和标签的样式会被完全保留。

var value = new Dictionary<string, object>(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.Sa veAsByTemplate(outputPath, templatePath, value);

3、输入、输出

输入系统支持模版路径或是Byte[]

输出支持文件路径、Byte[]、Stream

Sa veAsByTemplate(string path, string templatePath, Dictionary<string, object> value)
Sa veAsByTemplate(string path, byte[] templateBytes, Dictionary<string, object> value)
Sa veAsByTemplate(this Stream stream, string templatePath, Dictionary<string, object> value)
Sa veAsByTemplate(this Stream stream, byte[] templateBytes, Dictionary<string, object> value)

4、标签

MiniWord 使用类似 Vue, React 的模版字串 {{tag}},只需要确保 tag 与 value 参数的 key 一样(大小写敏感),系统会自动替换字串。

文本

{{tag}}

示例如下

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.Sa veAsByTemplate(path, templatePath, value);

模版

结果

5、表格

标签值为IEmerable>类别

示例代码

var value = new Dictionary<string, object>()
{
    ["TripHs"] = new Liststring, object>>
    {
        new Dictionary<string, object>
        {
            { "sDate",DateTime.Parse("2022-09-08 08:30:00")},
            { "eDate",DateTime.Parse("2022-09-08 15:00:00")},
            { "How","Discussion requirement part1"},
            { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
        },
        new Dictionary<string, object>
        {
            { "sDate",DateTime.Parse("2022-09-09 08:30:00")},
            { "eDate",DateTime.Parse("2022-09-09 17:00:00")},
            { "How","Discussion requirement part2 and development"},
            { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
        },
    }
};
MiniWord.Sa veAsByTemplate(path, templatePath, value);

模版

结果

6、二级列表

Tag 是 IEnumerable< MiniWordForeach> 类别. 使用方式{{foreach 和 endforeach}}.

示例代码

var value = new Dictionary<string, object>()
{
    ["TripHs"] = new Liststring, object>>
    {
        new Dictionary<string, object>
        {
            { "sDate", DateTime.Parse("2022-09-08 08:30:00") },
            { "eDate", DateTime.Parse("2022-09-08 15:00:00") },
            { "How", "Discussion requirement part1" },
            {
                "Details", new List()
                {
                    new MiniWordForeach()
                    {
                        Value = new Dictionary<string, object>()
                        {
                            {"Text", "Air"},
                            {"Value", "Airplane"}
                        },
                        Separator = " | "
                    },
                    new MiniWordForeach()
                    {
                        Value = new Dictionary<string, object>()
                        {
                            {"Text", "Parking"},
                            {"Value", "Car"}
                        },
                        Separator = " / "
                    }
                }
            }
        }
    }
};
MiniWord.Sa veAsByTemplate(path, templatePath, value);

模板

输出

7、条件判断

@if 和 @endif tags

示例代码

var value = new Dictionary()
{
    ["Name"] = new List(){
        new MiniWordHyperLink(){
            Url = "https://google.com",
            Text = "測試連結22!!"
        },
        new MiniWordHyperLink(){
            Url = "https://google1.com",
            Text = "測試連結11!!"
        }
    },
    ["Company_Name"] = "MiniSofteware",
    ["CreateDate"] = new DateTime(2021, 01, 01),
    ["VIP"] = true,
    ["Points"] = 123,
    ["APP"] = "Demo APP",
};
MiniWord.Sa veAsByTemplate(path, templatePath, value);

模版

结果

8、多彩字体

代码示例

var value = new
{
    Company_Name = new MiniWordColorText { Text = "MiniSofteware", FontColor = "#eb70AB", },
    Name = new[] {
        new MiniWordColorText { Text = "Ja", HighlightColor = "#eb70AB" },
        new MiniWordColorText { Text = "ck", HighlightColor = "#a56abe" }
    },
    CreateDate = new MiniWordColorText
    {
        Text = new DateTime(2021, 01, 01).ToString(),
        HighlightColor = "#eb70AB",
        FontColor = "#ffffff",
    },
    VIP = true,
    Points = 123,
    APP = "Demo APP",
};
MiniWord.Sa veAsByTemplate(path, templatePath, value);

使用示例

ASP.NET Core 3.1 API Export

public class Program
{
    public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();

    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup());
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services) => services.AddMvc();
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseStaticFiles();
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=api}/{action=Index}/{id}");
        });
    }
}

public class ApiController : Controller
{
    public IActionResult Index()
    {
        return new ContentResult
        {
            ContentType = "text/html",
            StatusCode = (int)HttpStatusCode.OK,
            Content = @"
DownloadWordFromTemplatePath
DownloadWordFromTemplateBytes
"
}; } static Dictionary<string, object> defaultValue = new Dictionary<string, object>() { ["title"] = "FooCompany", ["managers"] = new Liststring, object>> { new Dictionary<string, object>{{"name","Jack"},{ "department", "HR" } }, new Dictionary<string, object> {{ "name", "Loan"},{ "department", "IT" } } }, ["employees"] = new Liststring, object>> { new Dictionary<string, object>{{ "name", "Wade" },{ "department", "HR" } }, new Dictionary<string, object> {{ "name", "Felix" },{ "department", "HR" } }, new Dictionary<string, object>{{ "name", "Eric" },{ "department", "IT" } }, new Dictionary<string, object> {{ "name", "Keaton" },{ "department", "IT" } } } }; public IActionResult DownloadWordFromTemplatePath() { string templatePath = "TestTemplateComplex.docx"; Dictionary<string, object> value = defaultValue; MemoryStream memoryStream = new MemoryStream(); MiniWord.Sa veAsByTemplate(memoryStream, templatePath, value); memoryStream.Seek(0, SeekOrigin.Begin); return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { FileDownloadName = "demo.docx" }; } private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>(); static ApiController() { string templatePath = "TestTemplateComplex.docx"; byte[] bytes = System.IO.File.ReadAllBytes(templatePath); TemplateBytesCache.Add(templatePath, bytes); } public IActionResult DownloadWordFromTemplateBytes() { byte[] bytes = TemplateBytesCache["TestTemplateComplex.docx"]; Dictionary<string, object> value = defaultValue; MemoryStream memoryStream = new MemoryStream(); MiniWord.Sa veAsByTemplate(memoryStream, bytes, value); memoryStream.Seek(0, SeekOrigin.Begin); return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { FileDownloadName = "demo.docx" }; } }

项目地址

GitHub:github.com/mini-softwa…

项目总结

本文仅展示了部分功能和内容,若您有进一步需求,可访问项目地址获取详细信息。希望本文能为您在Word处理开发方面提供一些有益的帮助。期待大家在评论区留言交流,分享您的宝贵经验和建议。

最后

如果你觉得这篇文章对你有帮助,不妨点个赞支持一下!你的支持是我继续分享知识的动力。如果有任何疑问或需要进一步的帮助,欢迎随时留言。

也可以加入微信公众号 [DotNet技术匠] 社区,与其他热爱技术的同行一起交流心得,共同成长!

优秀是一种习惯,欢迎大家留言学习!

《夸克》非常好用的免费AI浏览器

下载APP查看

免责声明:文中图文均来自网络,如有侵权请联系删除,心愿游戏发布此文仅为传递信息,不代表心愿游戏认同其观点或证实其描述。

相关文章

更多

精选合集

更多

大家都在玩

热门话题

大家都在看

更多