MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。 ...

打印 上一主题 下一主题

主题 868|帖子 868|积分 2604

Github / Gitee

QQ群(1群) : 813100564 / QQ群(2群) : 579033769

视频教学

介绍

MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。

Getting Started

安装

快速入门

模板遵循“所见即所得”的设计,模板和标签的样式会被完全保留
  1. var value = new Dictionary<string, object>(){["title"] = "Hello MiniWord"};
  2. MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
复制代码

输入、输出


  • 输入系统支持模版路径或是Byte[]
  • 输出支持文件路径、Byte[]、Stream
  1. SaveAsByTemplate(string path, string templatePath, Dictionary<string, object> value)
  2. SaveAsByTemplate(string path, byte[] templateBytes, Dictionary<string, object> value)
  3. SaveAsByTemplate(this Stream stream, string templatePath, Dictionary<string, object> value)
  4. SaveAsByTemplate(this Stream stream, byte[] templateBytes, Dictionary<string, object> value)
复制代码
标签

MiniWord 使用类似 Vue, React 的模版字串 {{tag}},只需要确保 tag 与 value 参数的 key 一样(大小写敏感),系统会自动替换字串。
文本
  1. {{tag}}
复制代码
代码例子
  1. var value = new Dictionary<string, object>()
  2. {
  3.     ["Name"] = "Jack",
  4.     ["Department"] = "IT Department",
  5.     ["Purpose"] = "Shanghai site needs a new system to control HR system.",
  6.     ["StartDate"] = DateTime.Parse("2022-09-07 08:30:00"),
  7.     ["EndDate"] = DateTime.Parse("2022-09-15 15:30:00"),
  8.     ["Approved"] = true,
  9.     ["Total_Amount"] = 123456,
  10. };
  11. MiniWord.SaveAsByTemplate(path, templatePath, value);
复制代码
模版


导出


图片

标签值为 MiniWordPicture 类别
代码例子
  1. var value = new Dictionary<string, object>()
  2. {
  3.     ["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 }
  4. };
  5. MiniWord.SaveAsByTemplate(path, templatePath, value);
复制代码
模版


导出


列表

标签值为 string[] 或是 IList类别
代码例子
  1. var value = new Dictionary<string, object>()
  2. {
  3.     ["managers"] = new[] { "Jack" ,"Alan"},
  4.     ["employees"] = new[] { "Mike" ,"Henry"},
  5. };
  6. MiniWord.SaveAsByTemplate(path, templatePath, value);
复制代码
模版


导出


表格

标签值为 IEmerable类别
代码例子
  1. var value = new Dictionary<string, object>()
  2. {
  3.     ["TripHs"] = new List<Dictionary<string, object>>
  4.     {
  5.         new Dictionary<string, object>
  6.         {
  7.             { "sDate",DateTime.Parse("2022-09-08 08:30:00")},
  8.             { "eDate",DateTime.Parse("2022-09-08 15:00:00")},
  9.             { "How","Discussion requirement part1"},
  10.             { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
  11.         },
  12.         new Dictionary<string, object>
  13.         {
  14.             { "sDate",DateTime.Parse("2022-09-09 08:30:00")},
  15.             { "eDate",DateTime.Parse("2022-09-09 17:00:00")},
  16.             { "How","Discussion requirement part2 and development"},
  17.             { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
  18.         },
  19.     }
  20. };
  21. MiniWord.SaveAsByTemplate(path, templatePath, value);
复制代码
模版


导出


其他

POCO or dynamic 参数

v0.5.0 支持 POCO 或 dynamic parameter
  1. var value = new { title = "Hello MiniWord" };
  2. MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
复制代码
字体FontColor和HighlightColor
  1. var value = new
  2. {
  3.     Company_Name = new MiniWordColorText { Text = "MiniSofteware", FontColor = "#eb70AB" },
  4.     Name = new MiniWordColorText { Text = "Jack", HighlightColor = "#eb70AB" },
  5.     CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), HighlightColor = "#eb70AB", FontColor = "#ffffff" },
  6.     VIP = true,
  7.     Points = 123,
  8.     APP = "Demo APP",
  9. };
复制代码
HyperLink

我们可以尝试使用 MiniWodrHyperLink 类,用模板测试替换为超链接。
MiniWordHyperLink 提供了两个主要参数。

  • Url: HyperLink URI 目标路径
  • 文字:超链接文字
  1. var value = new
  2. {
  3.     ["Name"] = new MiniWordHyperLink(){
  4.         Url = "https://google.com",
  5.         Text = "測試連結!!"
  6.     },
  7.     ["Company_Name"] = "MiniSofteware",
  8.     ["CreateDate"] = new DateTime(2021, 01, 01),
  9.     ["VIP"] = true,
  10.     ["Points"] = 123,
  11.     ["APP"] = "Demo APP",
  12. };
  13. MiniWord.SaveAsByTemplate(path, templatePath, value);
复制代码
例子

ASP.NET Core 3.1 API Export
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Hosting;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Net;
  10. using MiniSoftware;
  11. public class Program
  12. {
  13.     public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
  14.     public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
  15. }
  16. public class Startup
  17. {
  18.     public void ConfigureServices(IServiceCollection services) => services.AddMvc();
  19.     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  20.     {
  21.         app.UseStaticFiles();
  22.         app.UseRouting();
  23.         app.UseEndpoints(endpoints =>
  24.         {
  25.             endpoints.MapControllerRoute(
  26.                 name: "default",
  27.                 pattern: "{controller=api}/{action=Index}/{id?}");
  28.         });
  29.     }
  30. }
  31. public class ApiController : Controller
  32. {
  33.     public IActionResult Index()
  34.     {
  35.         return new ContentResult
  36.         {
  37.             ContentType = "text/html",
  38.             StatusCode = (int)HttpStatusCode.OK,
  39.             Content = @"<html><body>
  40. <a target="_blank" href='https://www.cnblogs.com/api/DownloadWordFromTemplatePath'>DownloadWordFromTemplatePath</a><br>
  41. <a target="_blank" href='https://www.cnblogs.com/api/DownloadWordFromTemplateBytes'>DownloadWordFromTemplateBytes</a><br>
  42. </body></html>"
  43.         };
  44.     }
  45.     static Dictionary<string, object> defaultValue = new Dictionary<string, object>()
  46.     {
  47.         ["title"] = "FooCompany",
  48.         ["managers"] = new List<Dictionary<string, object>> {
  49.             new Dictionary<string, object>{{"name","Jack"},{ "department", "HR" } },
  50.             new Dictionary<string, object> {{ "name", "Loan"},{ "department", "IT" } }
  51.         },
  52.         ["employees"] = new List<Dictionary<string, object>> {
  53.             new Dictionary<string, object>{{ "name", "Wade" },{ "department", "HR" } },
  54.             new Dictionary<string, object> {{ "name", "Felix" },{ "department", "HR" } },
  55.             new Dictionary<string, object>{{ "name", "Eric" },{ "department", "IT" } },
  56.             new Dictionary<string, object> {{ "name", "Keaton" },{ "department", "IT" } }
  57.         }
  58.     };
  59.     public IActionResult DownloadWordFromTemplatePath()
  60.     {
  61.         string templatePath = "TestTemplateComplex.docx";
  62.         Dictionary<string, object> value = defaultValue;
  63.         MemoryStream memoryStream = new MemoryStream();
  64.         MiniWord.SaveAsByTemplate(memoryStream, templatePath, value);
  65.         memoryStream.Seek(0, SeekOrigin.Begin);
  66.         return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  67.         {
  68.             FileDownloadName = "demo.docx"
  69.         };
  70.     }
  71.     private static Dictionary<string, Byte[]> TemplateBytesCache = new Dictionary<string, byte[]>();
  72.     static ApiController()
  73.     {
  74.         string templatePath = "TestTemplateComplex.docx";
  75.         byte[] bytes = System.IO.File.ReadAllBytes(templatePath);
  76.         TemplateBytesCache.Add(templatePath, bytes);
  77.     }
  78.     public IActionResult DownloadWordFromTemplateBytes()
  79.     {
  80.         byte[] bytes = TemplateBytesCache["TestTemplateComplex.docx"];
  81.         Dictionary<string, object> value = defaultValue;
  82.         MemoryStream memoryStream = new MemoryStream();
  83.         MiniWord.SaveAsByTemplate(memoryStream, bytes, value);
  84.         memoryStream.Seek(0, SeekOrigin.Begin);
  85.         return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
  86.         {
  87.             FileDownloadName = "demo.docx"
  88.         };
  89.     }
  90. }
复制代码
常见问题

模版字串没有生效

建议 {{tag}} 复制重新整串复制贴上,有时打字 word 在底层 {{}}会被切开变成{{Tag}} 如图片


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表