# MiniWord
**Repository Path**: dotnetchina/MiniWord
## Basic Information
- **Project Name**: MiniWord
- **Description**: .NET Word(docx) 模版导出引擎不需要安装 Office COM+ 支持 Linux 和 Mac
- **Primary Language**: C#
- **License**: Apache-2.0
- **Default Branch**: main
- **Homepage**: https://github.com/mini-software/MiniWord
- **GVP Project**: No
## Statistics
- **Stars**: 595
- **Forks**: 143
- **Created**: 2022-09-12
- **Last Updated**: 2025-06-13
## Categories & Tags
**Categories**: utils
**Tags**: None
## README
---
---
---
## QQ群(1群) : [813100564](https://qm.qq.com/cgi-bin/qm/qr?k=3OkxuL14sXhJsUimWK8wx_Hf28Wl49QE&jump_from=webapi) / QQ群(2群) : [579033769](https://jq.qq.com/?_wv=1027&k=UxTdB8pR)
----
## 介绍
MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。

## Getting Started
### 安装
- nuget link : https://www.nuget.org/packages/MiniWord
### 快速入门
模板遵循“所见即所得”的设计,模板和标签的样式会被完全保留
```csharp
var value = new Dictionary(){["title"] = "Hello MiniWord"};
MiniSoftware.MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
```

### 输入、输出
- 输入系统支持模版路径或是Byte[]
- 输出支持文件路径、Byte[]、Stream
```csharp
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}}`,只需要确保 tag 与 value 参数的 key 一样`(大小写敏感)`,系统会自动替换字串。
### 文本
```csharp
{{tag}}
```
##### 代码例子
```csharp
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` 类别
##### 代码例子
```csharp
var value = new Dictionary()
{
["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("DemoLogo.png"), Width= 180, Height= 180 }
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
```
##### 模版

##### 导出

### 列表
标签值为 `string[]` 或是 `IList`类别
##### 代码例子
```csharp
var value = new Dictionary()
{
["managers"] = new[] { "Jack" ,"Alan"},
["employees"] = new[] { "Mike" ,"Henry"},
};
MiniWord.SaveAsByTemplate(path, templatePath, value);
```
##### 模版

##### 导出

### 表格
标签值为 `IEmerable>`类别
##### 代码例子
```csharp
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);
```
##### 模版

##### 导出

### 二级列表
Tag 是 `IEnumerable` 类别. 使用方式`{{foreach` 和 `endforeach}}`.
##### Example
```csharp
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);
```
##### Template

##### Result

### 条件判断
`@if` 和 `@endif` tags .
##### Example
```csharp
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);
```
##### Template

##### Result

### 循环
`@foreach` 和 `@endforeach` tags .
##### Example
```csharp
var value = new
{
LoopData = new List