【愚公系列】2023年01月 .net core工具案例-.net core使用miniword-爱代码爱编程
前言
MiniWord模板引擎的主要功能是根据模板,生成对应的Word文档。支持跨平台,项目采用类似Vue、React模板方式,在模板定义相应的变量,再结合数据,快速生成Word文件。
MiniWord官网:https://github.com/mini-software/MiniWord
一、.NET Core使用MiniWord
1.安装包
MiniWord
2.基本使用
1、定义模板
2、运行程序
var value = new Dictionary<string, object>(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
3、效果
3.文本插入
1、定义模板
2、运行程序
var value = new Dictionary<string, object>()
{
["Name"] = "愚公搬代码",
["Department"] = "软件部",
["Purpose"] = "Shanghai site needs a new system to control HR system.",
["StartDate"] = DateTime.Parse("2023-01-20 08:30:00"),
["EndDate"] = DateTime.Parse("2023-01-20 15:30:00"),
["Approved"] = true,
["Total_Amount"] = 123456,
};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
3、效果
4.图片插入
1、定义模板
2、运行程序
using MiniSoftware;
var value = new Dictionary<string, object>()
{
["Logo"] = new MiniWordPicture() { Path = PathHelper.GetFile("DemoLogo.png"), Width = 180, Height = 180 }
};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
3、效果
5.列表插入
1、定义模板
2、运行程序
var value = new Dictionary<string, object>()
{
["managers"] = new[] { "愚公1", "愚公粉丝1" },
["employees"] = new[] { "愚公2", "愚公粉丝2" },
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
3、效果
6.表格插入
1、定义模板
2、运行程序
var value = new Dictionary<string, object>()
{
["TripHs"] = new List<Dictionary<string, 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.SaveAsByTemplate(path, templatePath, value);
3、效果