MiniWord .NET Word
介绍
MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。
![null 74988cf369aad7b71a00cf90983dfb54.png](https://i-blog.csdnimg.cn/blog_migrate/960d864784c77dbdb793aa0fa85dd442.png)
Getting Started
安装
• nuget link : https://www.nuget.org/packages/MiniWord
快速入门
模板遵循“所见即所得”的设计,模板和标签的样式会被完全保留
var value = new Dictionary<string, object>(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
![null a7081600b1847c378885fa01d9fa9a58.png](https://i-blog.csdnimg.cn/blog_migrate/a7bb021feb9fda2d99b913161b97ff96.png)
输入、输出
• 输入系统支持模版路径或是Byte[]
• 输出支持文件路径、Byte[]、Stream
SaveAsByTemplate(string path, string templatePath, Dictionary<string, object> value)
SaveAsByTemplate(string path, byte[] templateBytes, Dictionary<string, object> value)
SaveAsByTemplate(this Stream stream, string templatePath, Dictionary<string, object> value)
SaveAsByTemplate(this Stream stream, byte[] templateBytes, Dictionary<string, object> value)
标签
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.SaveAsByTemplate(path, templatePath, value);
模版
![null 4147ec6dd9fdb0c94951dbedf2c3df94.png](https://i-blog.csdnimg.cn/blog_migrate/cbe91d3bd0681852f33b918e45e5fdfa.png)
导出
![null 8a5123910102751539e29d7f6a225b50.png](https://i-blog.csdnimg.cn/blog_migrate/f0e579d9cbae5dc235a5997d111ee071.png)
图片
标签值为 MiniWordPicture
类别
代码例子
var value = new Dictionary<string, object>()
{
["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 }
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
模版
![null be20ef4a8e77ae654dc8a983b1797eeb.png](https://i-blog.csdnimg.cn/blog_migrate/b4886490040313373d862c98bda10fe6.png)
导出
![null a385fd6448be6d34a8facaeeda1c9d33.png](https://i-blog.csdnimg.cn/blog_migrate/11b9b26e5a95e819f6f1b06dfb41f777.png)
列表
标签值为 string[]
或是 IList<string>
类别
代码例子
var value = new Dictionary<string, object>()
{
["managers"] = new[] { "Jack" ,"Alan"},
["employees"] = new[] { "Mike" ,"Henry"},
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
模版
![null 1ab2968a6ad760fcf8fe3460daa0866c.png](https://i-blog.csdnimg.cn/blog_migrate/9e76404320da30eb217a5faf642b71d3.png)
导出
![null c4001e2a8777e4fb38840995d795c8bd.png](https://i-blog.csdnimg.cn/blog_migrate/3ca80e57d69324786b2914d45b909679.png)
表格
标签值为 IEmerable<Dictionary<string,object>>
类别
代码例子
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);
模版
![null 625bc719971c8db74173974dfaa3f617.png](https://i-blog.csdnimg.cn/blog_migrate/c106d3d29c1e01c6e90777ef89448dc9.png)
导出
![null 3e08690f0acd6e24751308f2177e0c38.png](https://i-blog.csdnimg.cn/blog_migrate/e35e3659dc87de15b2641fee609fa0db.png)
二级列表
Tag 是 IEnumerable<MiniWordForeach>
类别. 使用方式{{foreach
和 endforeach}}
.
Example
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" },
{
"Details", new List<MiniWordForeach>()
{
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.SaveAsByTemplate(path, templatePath, value);
Template
![null 6bce27328d24f9a0e9ac9fcdd28d47bd.png](https://i-blog.csdnimg.cn/blog_migrate/9a48c4b98b6dbe952f58f792ef74f728.png)
Result
![null 81a703842c1de962f4f27b68df8888ca.png](https://i-blog.csdnimg.cn/blog_migrate/547aa9438fe93469226d73ab1f1e80df.png)
条件判断
@if
和 @endif
tags .
Example
var value = new Dictionary<string, object>()
{
["Name"] = new List<MiniWordHyperLink>(){
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.SaveAsByTemplate(path, templatePath, value);
Template
![null 8f1357a959897b4d347c0e3a77074ce3.png](https://i-blog.csdnimg.cn/blog_migrate/cf8649a965460c203b935abe00e3aec3.png)
Result
![null bdfce2c936861515cf98f0edc39dc91c.png](https://i-blog.csdnimg.cn/blog_migrate/1a59679cf0b0c5858f725957014960a7.png)
多彩字体
代码例子
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.SaveAsByTemplate(path, templatePath, value);
其他
POCO or dynamic 参数
v0.5.0 支持 POCO 或 dynamic parameter
var value = new { title = "Hello MiniWord" };
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
字体FontColor和HighlightColor
var value = new
{
Company_Name = new MiniWordColorText { Text = "MiniSofteware", FontColor = "#eb70AB" },
Name = new MiniWordColorText { Text = "Jack", HighlightColor = "#eb70AB" },
CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), HighlightColor = "#eb70AB", FontColor = "#ffffff" },
VIP = true,
Points = 123,
APP = "Demo APP",
};
HyperLink
我们可以尝试使用 MiniWodrHyperLink
类,用模板测试替换为超链接。
MiniWordHyperLink
提供了两个主要参数。
• Url:HyperLink URI 目标路径
• 文字:超链接文字
var value = new
{
["Name"] = new MiniWordHyperLink(){
Url = "https://google.com",
Text = "測試連結!!"
},
["Company_Name"] = "MiniSofteware",
["CreateDate"] = new DateTime(2021, 01, 01),
["VIP"] = true,
["Points"] = 123,
["APP"] = "Demo APP",
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
项目地址
文档地址:https://github.com/mini-software/MiniWord
推荐阅读
欢迎关注我的公众号“Net分享”,技术文章第一时间推送,随缘更新 , 分享一些你可能注意不到的细节。
![6ee09a778e13688cfb72449d9f8575dd.jpeg](https://i-blog.csdnimg.cn/blog_migrate/1be189441589a4281dd70114c5bf7305.png)