尝试做一个.NET模板填充导出Excel工具
2024-09-28 04:21:19
园友好,最近晚辈延续上篇后尝试进阶做成Excel模板填充数据生成工具 MiniExcel Template。
主要特点
- 同样以
Stream流、延迟查询
避免全部数据载入内存情况,做到1GB内存降低到只需60多MB - 简单使用,方式类似 Vue 的宣告模板
{{变数}}
- 轻量化,总包大小100多KB,不依赖 com 跟第三方套件
使用方式介绍 :
0. 安装
1. 基本填充
模板:
最终效果:
代码:
// 1. By POCO
var value = new
{
Name = "Jack",
CreateDate = new DateTime(2021, 01, 01),
VIP = true,
Points = 123
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
// 2. By Dictionary
var value = new Dictionary<string, object>()
{
["Name"] = "Jack",
["CreateDate"] = new DateTime(2021, 01, 01),
["VIP"] = true,
["Points"] = 123
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
2. IEnumerable 数据填充
Note1: 同行从左往右以第一个 IEnumerableUse 当列表来源 (不支持同列多集合)
模板:
最终效果:
代码:
//1. By POCO
var value = new
{
employees = new[] {
new {name="Jack",department="HR"},
new {name="Lisa",department="HR"},
new {name="John",department="HR"},
new {name="Mike",department="IT"},
new {name="Neo",department="IT"},
new {name="Loan",department="IT"}
}
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
//2. By Dictionary
var value = new Dictionary<string, object>()
{
["employees"] = new[] {
new {name="Jack",department="HR"},
new {name="Lisa",department="HR"},
new {name="John",department="HR"},
new {name="Mike",department="IT"},
new {name="Neo",department="IT"},
new {name="Loan",department="IT"}
}
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
3. 复杂数据填充
Note: 支持多 sheet 填充,并共用同一组参数
模板:
最终效果:
代码:
// 1. By POCO
var value = new
{
title = "FooCompany",
managers = new[] {
new {name="Jack",department="HR"},
new {name="Loan",department="IT"}
},
employees = new[] {
new {name="Wade",department="HR"},
new {name="Felix",department="HR"},
new {name="Eric",department="IT"},
new {name="Keaton",department="IT"}
}
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
// 2. By Dictionary
var value = new Dictionary<string, object>()
{
["title"] = "FooCompany",
["managers"] = new[] {
new {name="Jack",department="HR"},
new {name="Loan",department="IT"}
},
["employees"] = new[] {
new {name="Wade",department="HR"},
new {name="Felix",department="HR"},
new {name="Eric",department="IT"},
new {name="Keaton",department="IT"}
}
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
4. 大数据填充效率比较
NOTE: 在 MiniExcel 使用 IEnumerable 延迟 ( 不ToList ) 可以节省内存使用
5. Cell 值自动类别对应
模板
最终效果
类别
public class Poco
{
public string @string { get; set; }
public int? @int { get; set; }
public decimal? @decimal { get; set; }
public double? @double { get; set; }
public DateTime? datetime { get; set; }
public bool? @bool { get; set; }
public Guid? Guid { get; set; }
}
代码
var poco = new TestIEnumerableTypePoco { @string = "string", @int = 123, @decimal = decimal.Parse("123.45"), @double = (double)123.33, @datetime = new DateTime(2021, 4, 1), @bool = true, @Guid = Guid.NewGuid() };
var value = new
{
Ts = new[] {
poco,
new TestIEnumerableTypePoco{},
null,
poco
}
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
6. Example : 列出 Github 专案
模板
最终效果
代码
var projects = new[]
{
new {Name = "MiniExcel",Link="https://github.com/shps951023/MiniExcel",Star=146, CreateTime=new DateTime(2021,03,01)},
new {Name = "HtmlTableHelper",Link="https://github.com/shps951023/HtmlTableHelper",Star=16, CreateTime=new DateTime(2020,02,01)},
new {Name = "PocoClassGenerator",Link="https://github.com/shps951023/PocoClassGenerator",Star=16, CreateTime=new DateTime(2019,03,17)}
};
var value = new
{
User = "ITWeiHan",
Projects = projects,
TotalStar = projects.Sum(s => s.Star)
};
MiniExcel.SaveAsByTemplate(path, templatePath, value);
广告时间
MiniExcel最近加入dotNET China组织
喜欢MiniExcel/dotnet china的朋友求帮点个 Star
Gitee / Github
最后宣传前天刚创的QQ群,方便大家做讨论 : 813100564
最新文章
- 【BZOJ】3526: [Poi2014]Card
- 导航效果css
- Java简单的系统登陆
- C#项目打开/保存文件夹/指定类型文件,获取路径
- 关于使用Css设置Canvas画布大小的问题
- Install GDAL in OpenSUSE 12.3 Linux
- linux虚拟主机wdcp系列教程之四
- NOIP2008 双栈队列
- TCP/IP(三)数据链路层~1
- Python中os与sys两模块的区别
- 求方差分析与两样本T检验 区别
- Taro 代码及功能,需要注意的地方
- DotNetty项目基本了解和介绍
- 获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
- ffmpeg按比例缩放--";width / height not divisible by 2"; 解决方法
- MQTT-SN协议乱翻之消息格式
- SSM框架——实现分页和搜索分页
- jvisualvm 远程连接jboss
- Robot FrameWork基础学习(二)
- deb包制作
热门文章
- NGK公链:夯实基础设施 实现产业大规模应用
- MySQL修改表中字段的字符集
- 推荐一款好用的免费远程控制软件——ToDesk
- 使用sun.net.ftp.FtpClient进行上传功能开发,在jdk1.7上不适用问题的解决
- Python和JavaScript在使用上有什么区别?
- SpringCloud(三):SpringCloud快速开发入门
- MVC base64加密的文件,前端下载
- celery 与 flask 实现异步任务调度
- C# 应用 - 多线程 1) 多线程的知识图谱
- HDU_3949 XOR 【线性基】