MiniWord .NET Word模板引擎,藉由Word模板和資料簡單、快速生成檔案。
QQ群(1群) : 813100564 / QQ群(2群) : 579033769
介紹
MiniWord .NET Word模板引擎,藉由Word模板和資料簡單、快速生成檔案。
Getting Started
安裝
- nuget link : https://www.nuget.org/packages/MiniWord
- Packge xml
<PackageReference Include="MiniWord" Version="0.4.0" />
- Or .NET CLI :
dotnet add package MiniWord --version 0.4.0
快速入門
模板遵循“所見即所得”的設計,模板和標籤的樣式會被完全保留
var value = new Dictionary<string, object>(){["title"] = "Hello MiniWord"}; MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
輸入、輸出
- 輸入系統支援模版路徑或是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);
模版
匯出
圖片
標籤值為 MiniWordPicture
類別
程式碼例子
var value = new Dictionary<string, object>() { ["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 } }; MiniWord.SaveAsByTemplate(path, templatePath, value);
模版
匯出
列表
標籤值為 string[]
或是 IList<string>
類別
程式碼例子
var value = new Dictionary<string, object>() { ["managers"] = new[] { "Jack" ,"Alan"}, ["employees"] = new[] { "Mike" ,"Henry"}, }; MiniWord.SaveAsByTemplate(path, templatePath, value);
模版
匯出
表格
標籤值為 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);
模版
匯出
其他
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);
例子
ASP.NET Core 3.1 API Export
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.IO; using System.Net; using MiniSoftware; 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<Startup>()); } 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 = @"<html><body> <a href='api/DownloadWordFromTemplatePath'>DownloadWordFromTemplatePath</a><br> <a href='api/DownloadWordFromTemplateBytes'>DownloadWordFromTemplateBytes</a><br> </body></html>" }; } static Dictionary<string, object> defaultValue = new Dictionary<string, object>() { ["title"] = "FooCompany", ["managers"] = new List<Dictionary<string, object>> { new Dictionary<string, object>{{"name","Jack"},{ "department", "HR" } }, new Dictionary<string, object> {{ "name", "Loan"},{ "department", "IT" } } }, ["employees"] = new List<Dictionary<string, 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.SaveAsByTemplate(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.SaveAsByTemplate(memoryStream, bytes, value); memoryStream.Seek(0, SeekOrigin.Begin); return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { FileDownloadName = "demo.docx" }; } }
常見問題
模版字串沒有生效
建議 {{tag}}
複製重新整串複製貼上,有時打字 word 在底層 {{}}
會被切開變成 <w:t>{</w:t><w:t>{<w:/t><w:t>Tag</w:t><w:t>}</w:t><w:t>}<w:/t>
如圖片
「其他文章」
- 執行緒池底層原理詳解與原始碼分析
- 30分鐘掌握 Webpack
- 線性迴歸大結局(嶺(Ridge)、 Lasso迴歸原理、公式推導),你想要的這裡都有
- 【前端必會】webpack loader 到底是什麼
- 中心化決議管理——雲端分析
- HashMap底層原理及jdk1.8原始碼解讀
- 詳解JS中 call 方法的實現
- 列印 Logger 日誌時,需不需要再封裝一下工具類?
- 初識設計模式 - 代理模式
- 密碼學奇妙之旅、01 CFB密文反饋模式、AES標準、Golang程式碼
- Springboot之 Mybatis 多資料來源實現
- CAS核心思想、底層實現
- 面試突擊86:SpringBoot 事務不回滾?怎麼解決?
- 基於electron vue element構建專案模板之【打包篇】
- MiniWord .NET Word模板引擎,藉由Word模板和資料簡單、快速生成檔案。
- 認識執行緒,初始併發
- 1-VSCode搭建GD32開發環境
- 初識設計模式 - 原型模式
- 執行緒安全問題的產生條件、解決方式
- 2>&1到底是什麼意思?