简介
MiniWord 是一个简单高效的 .NET Word 模板库。

快速开始
安装
- nuget 链接 :https://www.nuget.org/packages/MiniWord
快速上手
模板采用“所见即所得”设计,模板标签样式完全保留。
var value = new Dictionary(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);

输入,输出
- 输入支持文件路径,byte[]
- 输出支持文件路径,byte[],流
SaveAsByTemplate(string path, string templatePath, Dictionary 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}},用户只需确保标签和 value 参数的键相同,系统就会自动替换它们。
文本
{{tag}}
##### 示例var value = 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);
##### 模板
##### 结果

图片
值类型为 MiniWordPicture
##### 示例
var value = new Dictionary()
{
["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 }
};
MiniWord.SaveAsByTemplate(path, templatePath, value); ##### 模板

##### 结果

列表
tag 值为 string[] 或 IList 类型
##### 示例
var value = new Dictionary()
{
["managers"] = new[] { "Jack" ,"Alan"},
["employees"] = new[] { "Mike" ,"Henry"},
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
模板
##### 结果

表格
Tag值为 IEmerable 类型
##### 示例
var value = 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);
##### 模板
##### 结果

列表内嵌列表
标签值为 IEnumerable 类型。模板中需要添加 {{foreach 和 endforeach}} 标签。
##### 示例
var value = 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" },
{
"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);
##### 模板##### 结果
模板中的 If 语句
对于多段落,使用 @if 和 @endif 标签。
对于单段落及 foreach 内部,使用 {{if 和 endif}} 标签,模板是必需的。
##### 示例
var value = 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);
##### 多段模板##### 多段结果
##### 单段模板
##### 单段结果
彩色文字
##### 示例
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 或动态参数
v0.5.0 支持 POCO 或动态参数
var value = new { title = "Hello MiniWord" };
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
字体颜色和高亮颜色
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",
};
超链接
如果值类型为 MiniWordHyperLink,系统将用超链接替换模板字符串。
- Url:超链接的 URI 目标路径
- Text:描述
var value = new
{
["Name"] = new MiniWordHyperLink(){
Url = "https://google.com",
Text = "Test Link!!"
},
["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 导出
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());
}
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 = @"
DownloadWordFromTemplatePath
DownloadWordFromTemplateBytes
"
};
} static Dictionary defaultValue = new Dictionary()
{
["title"] = "FooCompany",
["managers"] = new List> {
new Dictionary{{"name","Jack"},{ "department", "HR" } },
new Dictionary {{ "name", "Loan"},{ "department", "IT" } }
},
["employees"] = new List> {
new Dictionary{{ "name", "Wade" },{ "department", "HR" } },
new Dictionary {{ "name", "Felix" },{ "department", "HR" } },
new Dictionary{{ "name", "Eric" },{ "department", "IT" } },
new Dictionary {{ "name", "Keaton" },{ "department", "IT" } }
}
};
public IActionResult DownloadWordFromTemplatePath()
{
string templatePath = "TestTemplateComplex.docx";
Dictionary 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 TemplateBytesCache = new Dictionary();
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 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"
};
}
}
支持 : 捐赠链接
--- Tranlated By Open Ai Tx | Last indexed: 2025-07-17 ---