MiniWord .NET Word介绍MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081029171979262988252.png)
imageGetting Started安装• nuget link : https://www.nuget.org/packages/MiniWord快速入门模板遵循“所见即所得”的设计,模板和标签的样式会被完全保留
varvalue = new Dictionary(){["title"] = "Hello MiniWord"};MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath,
value);
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081029171979262934411.png)
image输入、输出• 输入系统支持模版路径或是Byte[]• 输出支持文件路径、Byte[]、StreamSaveAsByTemplate(string path, string templatePath, Dictionary<
string, object> value)SaveAsByTemplate(string path, byte[] templateBytes, Dictionary
value)SaveAsByTemplate(this Stream stream, string templatePath, Dictionary value)SaveAsByTemplate(
this Stream stream, byte[] templateBytes, Dictionary value)标签MiniWord 使用类似 Vue, React 的模版字串
{{tag}},只需要确保 tag 与 value 参数的 key 一样(大小写敏感),系统会自动替换字串文本{{tag}}代码例子varvalue = new Dictionary
>(){ ["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);模版
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081030171979263032034.png)
image导出
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081030171979263059211.png)
image图片标签值为 MiniWordPicture 类别代码例子varvalue = new Dictionary(){ ["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile(
"DemoLogo.png"), Width= 180, Height= 180 }};MiniWord.SaveAsByTemplate(path, templatePath, value);模版
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081030171979263055512.png)
image导出
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081030171979263032271.png)
image列表标签值为 string[] 或是 IList类别代码例子varvalue = new Dictionary(){ ["managers"
] = new[] { "Jack" ,"Alan"}, ["employees"] = new[] { "Mike" ,"Henry"},};MiniWord.SaveAsByTemplate(path, templatePath,
value);模版
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081031171979263135557.png)
image导出
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081031171979263150077.png)
image表格标签值为 IEmerable类别代码例子varvalue = new Dictionary(){ [
"TripHs"] = new List {new Dictionary { {
"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 {
{ "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);模版
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081031171979263149556.png)
image导出
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081031171979263154448.png)
image二级列表Tag 是 IEnumerable 类别. 使用方式{{foreach 和 endforeach}}.Examplevarvalue = new Dictionary<
string, object>(){ ["TripHs"] = new List {new Dictionary
> { { "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() { {"Text", "Air"},
{"Value", "Airplane"} }, Separator =
" | " },new MiniWordForeach() { Value =
new Dictionary() { {"Text", "Parking"
}, {"Value", "Car"} }, Separator =
" / " } } } } }};MiniWord.SaveAsByTemplate(path, templatePath,
value);Template
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081032171979263252520.png)
before_foreachResult
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081032171979263278565.png)
after_foreach条件判断@if 和 @endif tags .Examplevarvalue = 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.SaveAsByTemplate(path, templatePath,
value);Template
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081032171979263267342.png)
before_ifResult
![](http://ldjg88.com/zb_users/upload/2024/07/20240701081033171979263353129.png)
after_if多彩字体代码例子varvalue = 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 parametervarvalue = new { title = "Hello MiniWord"
};MiniWord.SaveAsByTemplate(outputPath, templatePath, value);字体FontColor和HighlightColorvarvalue = 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 目标路径• 文字:超链接文字varvalue = 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推荐阅读• Nginx负载均衡+转发策略• C#在PC上面控制安卓设备,太牛掰了欢迎关注我的公众号“
Net分享”,技术文章第一时间推送,随缘更新 , 分享一些你可能注意不到的细节。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。