本库进行了重写,如果需要请转移到下文查看:
框架与依赖
- 框架:.NET Standard 2.0
- 依赖:DotNetCore.NPOI
Excel导入(ExcelImportService)
- 导出模板
- 校验数据
- 正则表达式校验
- 数据库存在校验
- 数值区间
- 字符串长度
- 日期
- 重复数据
- 数据转换
Demo
public class ImportCar
{
[ColName("车牌号")]
[Regex(RegexConstant.NOT_EMPTY_REGEX,ErrorMsg ="必填")]
[DatabaseExist("cartable","carcode")]
[Regex(RegexConstant.CAR_CODE_REGEX)]
[Duplication]
public string CarCode { get; set; }
[ColName("手机号")]
[Regex(RegexEnum.国内手机号)]
public string Mobile { get; set; }
[ColName("身份证号")]
[Regex(RegexConstant.IDENTITY_NUMBER_REGEX)]
public string IdentityNumber { get; set; }
[ColName("姓名")]
[MaxLength(10)]
public string Name { get; set; }
[ColName("性别")]
[Regex(RegexConstant.GENDER_REGEX)]
public GenderEnum Gender { get; set; }
[ColName("注册日期")]
[DateTime]
public DateTime RegisterDate { get; set; }
[ColName("年龄")]
[Range(0, 150)]
public int Age { get; set; }
}
//校验Excel数据
List<ExcelDataRow> rows = ExcelImportService.Import<ImportCar>(fileUrl, delegateNotExistInDatabase);
//错误信息入库
rows.Where(r => !r.IsValid).ToList().ForEach(
r =>
{
InsertErrorLog($"第{r.RowIndex}行,{r.ErrorMsg}");
}
);
//正确数据转换为指定类型
List<ImportCar> importCars = rows.Where(r => r.IsValid).FastConvert<ImportCar>().ToList();
//正确数据入库
InsertCorrectData(importCars);
Excel导出(ExcelExportService)
- 导出Excel
- 标记样式
- 表头(字体、加粗、字号、颜色)
- 列合并单元格
- 自适应宽高
- 自动换行
Demo
[WrapText]
[Header(Color =ColorEnum.RED,FontName ="微软雅黑",FontSize =12,IsBold =true)]
public class ExportCar
{
[MergeCols]
[ColName("车牌号")]
public string CarCode { get; set; }
[ColName("姓名")]
public string Name { get; set; }
[ColName("性别")]
public GenderEnum Gender { get; set; }
[ColName("注册日期")]
public DateTime RegisterDate { get; set; }
[ColName("年龄")]
public int Age { get; set; }
}
List<ExportCar> list;
//业务操作,为list 赋值...
...
//导出
IWorkbook wk = ExcelExportService.Export(list);
//为IWorkbook提供了转换为byte数组的扩展方法
File.WriteAllBytes(@"c:\test\test.xls", wk.ToBytes());
导出效果
Word生成(WordExportService)
Demo
制作Word模板
定义类
public class WordCar
{
[PlaceHolder(PlaceHolderEnum.A)]
public string OwnerName { get; set; }
[PlaceHolder(PlaceHolderEnum.B)]
public string CarType { get; set; }
//图片占位的属性类型必须为List<string>,存放图片的绝对全地址
[PicturePlaceHolder(PlaceHolderEnum.C,"车辆照片")]
public List<string> CarPictures { get; set; }
[PicturePlaceHolder(PlaceHolderEnum.D,"车辆证件")]
public List<string> CarLicense { get; set; }
}
导出
string curDir = Environment.CurrentDirectory;
string pic1 = Path.Combine(curDir, "files", "1.jpg");
string pic2 = Path.Combine(curDir, "files", "2.jpg");
string pic3 = Path.Combine(curDir, "files", "3.jpg");
string templateurl = Path.Combine(curDir, "files", "CarWord.docx");
WordCar car = new WordCar()
{
OwnerName = "张三丰",
CarType = "豪华型宾利",
CarPictures = new List<string> { pic1, pic2 },
CarLicense = new List<string> { pic3 }
};
XWPFDocument doc = WordExportService.ExportFromTemplate(templateurl, car);
///为XWPFDocument提供了转换为byte数组的扩展方法
File.WriteAllBytes(@"c:\test\test.docx", doc.ToBytes());
导出效果
性能测试
Excel导入
- 5000条数据读取和校验(首次缓存之后,700毫秒左右,校验使用了缓存提高性能)
- 4992条有效数据的转换(80毫秒,数据转换使用了表达式树生成委托,达到了接近硬编码的性能)
github地址:
https://github.com/holdengong/Ade.OfficeService