1.简介
工作中,遇到需要导入导出 execl,导出 word 需求,还有各种各样的样式,于是有了 word 模板导出。这类工作不涉及到核心业务,但又往往不可缺少
查询了大量资料,有以下框架/库可以用:
名称 | 是否收费 | 说明 |
---|---|---|
NPOI | 开源免费 | 不需要服务器安装 Office,功能强大 |
Docx | 开源免费 | 不需要服务器安装 Office,功能强大 |
Aspose | 收费 | 不需要服务器安装 Office,功能很强大 |
OpenXml | 开源免费 | 不需要服务器安装 Office,微软提供的组件 |
Spire | 收费 | 不需要服务器安装 Office,官方有提供免费版本,不过有些限制 |
最后决定采用 Docx
操作 word,EPPlus
操作 execl
由于工作需要,我封装了 Alizhou.Office
,目的是最大化节省导入导出这种非核心功能开发时间,专注于业务实现,并且业务端与底层基础组件完全解耦,即业务端完全不需要知道底层使用的是什么基础库,使得重构代价大大降低。
Alizhou.Office
底层库目前使用 Docx,因此是完全免费的
Alizhou.Office
目前提供了
- Word 根据模板生成:支持使用文本/图片/表格替换,占位符只需定义模板类,制作 Word 模板,一行代码导出 docx 文档;
- 根据实体对 execl 导入导出;
2.如何使用
2.1 安装 Alizhou.Office
nuget 搜索并安装 Alizhou.Office
2.2 依赖注入
// 注入Office基础服务 services.AddAlizhouOffice();
2.3 IWordExportService - Word 通用导出服务
2.3.1 定义模板类,模板类需实现 IWordExportTemplate 空接口
public class WordUserTemplate : IWordExportTemplate { /// <summary> /// 默认占位符为{PropertyName} /// </summary> public string UserName { get; set; } /// <summary> /// 默认 /// </summary> [Placeholder("{电话}")] public string Phone { set; get; } /// <summary> /// 表格 /// </summary> public AlizhouTable Table { get; set; } /// <summary> /// 图片 /// </summary> public IEnumerable<AlizhouPicture> Pictures { get; set; } }
2.3.2 制作模板
2.3.3 根据模板生成 word
string basePath = Environment.CurrentDirectory; string templateUrl = @$"{basePath}/template/word/TemplateWrod.docx"; IWordExportService wordExportService = new WordExportService(new WordExportProvider()); WordUserTemplate userTemplate = new WordUserTemplate { UserName = "小周黎", Phone = "175626565656", Pictures = new List<AlizhouPicture>() { new Office.Model.AlizhouPicture { PictureUrl = "D://图片1.png", Width=540, Height=405, }, new Office.Model.AlizhouPicture { PictureUrl = "D://图片2.png", Width=540, Height=405, } }, Table = new Office.Model.AlizhouTable(3, 2) { Rows = new System.Collections.Generic.List<Office.Model.AlizhouTableRow>() { new Office.Model.AlizhouTableRow{ Height=200, Cells=new System.Collections.Generic.List<Office.Model.AlizhouTableCell>(){ new Office.Model.AlizhouTableCell{ Width=100, Paragraphs=new System.Collections.Generic.List<Office.Model.AlizhouParagraph>(){ new Office.Model.AlizhouParagraph{ Run=new Office.Model.AlizhouRun{ Text="姓名", IsBold=true } } } }, new Office.Model.AlizhouTableCell{ Width=100, Paragraphs=new System.Collections.Generic.List<Office.Model.AlizhouParagraph>(){ new Office.Model.AlizhouParagraph{ Run=new Office.Model.AlizhouRun{ Text="年龄" } } } } }, }, new Office.Model.AlizhouTableRow{ Height=200, Cells=new System.Collections.Generic.List<Office.Model.AlizhouTableCell>(){ new Office.Model.AlizhouTableCell{ Width=100, Paragraphs=new System.Collections.Generic.List<Office.Model.AlizhouParagraph>(){ new Office.Model.AlizhouParagraph{ Run=new Office.Model.AlizhouRun{ Text="周黎" } } } }, new Office.Model.AlizhouTableCell{ Width=100, Paragraphs=new System.Collections.Generic.List<Office.Model.AlizhouParagraph>(){ new Office.Model.AlizhouParagraph{ Run=new Office.Model.AlizhouRun{ Text="18" } } } } }, }, new Office.Model.AlizhouTableRow{ Height=200, Cells=new System.Collections.Generic.List<Office.Model.AlizhouTableCell>(){ new Office.Model.AlizhouTableCell{ Width=100, Paragraphs=new System.Collections.Generic.List<Office.Model.AlizhouParagraph>(){ new Office.Model.AlizhouParagraph{ Run=new Office.Model.AlizhouRun{ Text="张三", IsBold=true, Pictures=new System.Collections.Generic.List<Office.Model.AlizhouPicture>() { new Office.Model.AlizhouPicture{ PictureUrl="D://191cb437-2bc8-4fe9-9c5c-b7536eae1883.jpg",Width=30,Height=30}, new Office.Model.AlizhouPicture{ PictureUrl="D://renwu-mayun1.jpg",Width=30,Height=30} } } } } }, new Office.Model.AlizhouTableCell{ Width=100, Paragraphs=new System.Collections.Generic.List<Office.Model.AlizhouParagraph>(){ new Office.Model.AlizhouParagraph{ Run=new Office.Model.AlizhouRun{ Text="19", Color=Color.Red, FontFamily="微软雅黑", FontSize=12, IsBold=true, } } } } }, } } } }; var word = wordExportService.TemplateCreateWord(templateUrl, userTemplate); File.WriteAllBytes($"{basePath}/{DateTime.Now.ToString("yyyyMMddHHmmss")}测试生成word.docx", word.WordBytes); Console.ReadKey();
2.3.3 生成效果
2.4 IExeclImportExportService- execl 通用导入导出服务
2.4.1 定义 execl 模板类
public class PersonDto
{
[ExcelTableColumn("姓名")]
[Required(ErrorMessage = "姓名必填")]
public string Name { get; set; }
[ExcelTableColumn("年龄")]
[Range(1, 25, ErrorMessage = "年龄超过25岁的MM不要")]//范围判断
public int Age { get; set; }
[ExcelTableColumn(3)]//根据索引取
public string Adress { get; set; }
[ExcelTableColumn("电话")]
[MaxLength(11, ErrorMessage = "手机号 长度不能超过11")]
public string Phone { set; get; }
public override string ToString()
{
return $"姓名:{Name};年龄:{Age};地址:{Adress};电话:{Phone}";
}
}
2.4.2 导入导出 execl
string basePath = Environment.CurrentDirectory;
var list = new List<PersonDto>();
for (int i = 0; i < 100; i++)
{
list.Add(new PersonDto
{
Name = $"张三{i}",
Age = 18,
Adress = $"地址{i}",
Phone = $"17783042962"
});
}
IExeclImportExportService execlImportExport = new ExeclImportExportService(new ExeclImportExportProvider());
var alizhouExecl = execlImportExport.Export(list);
string path = $@"{basePath}..\..\..\..\OutPut\execl\ExeclImportExportTest.xlsx";
File.WriteAllBytes(path, alizhouExecl.WordBytes);
var data = execlImportExport.Import<PersonDto>(File.OpenRead(path));
foreach (var item in data)
{
Console.WriteLine(item.ToString());
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于