框架與依賴 框架:.NET Standard 2.0 依賴:DotNetCore.NPOI https://github.com/dotnetcore/NPOI http://www.cnblogs.com/savorboard/p/netcore npoi.html Excel導入(ExcelIm ...
框架與依賴
- 框架:.NET Standard 2.0
- 依賴:DotNetCore.NPOI
- https://github.com/dotnetcore/NPOI
- http://www.cnblogs.com/savorboard/p/netcore-npoi.html
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)
- 根據模板生成Word
- 插入文本
- 插入圖片
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