public async Task <ExcelData> ValidateAsync <Template>(ImportOption importOption) where Template : class, new() { ExcelData excelData = this.GetExcelData <Template>(importOption); AndFilter andFilter = new AndFilter { Filters = FiltersFactory.CreateFilters <Template>() }; FilterContext filterContext = new FilterContext { TypeFilter = TypeFilterInfoFactory.CreateInstance <Template>() }; excelData.ExcelDataRows = andFilter.Filter(excelData.ExcelDataRows, filterContext, importOption); return(await Task.FromResult(excelData)); }
//public DataTable ToTable(string fileUrl, int sheetIndex = 0, int headerRowIndex = 0, int dataRowStartIndex = 1, int dataRowCount = -1) //{ // IWorkbook workbook; // using (FileStream stream = new FileStream(fileUrl, FileMode.Open, FileAccess.Read)) // { // workbook = new HSSFWorkbook(stream); // } // ISheet sheet = workbook.GetSheetAt(sheetIndex); // DataTable dt = new DataTable(sheet.SheetName); // // 处理表头 // IRow headerRow = sheet.GetRow(headerRowIndex); // foreach (ICell headerCell in headerRow) // { // dt.Columns.Add(headerCell.ToString()); // } // //默认转换所有数据 // var dataRowStopIndex = sheet.PhysicalNumberOfRows - 1; // //只转换指定数量数据 // if (dataRowCount > -1) // { // dataRowStopIndex = dataRowStartIndex + dataRowCount; // } // //转换 // for (int i = dataRowStartIndex; i < dataRowStopIndex; i++) // { // var row = sheet.GetRow(i); // if (row != null) // { // DataRow dataRow = dt.NewRow(); // dataRow.ItemArray = row.Cells.Select(c => c.ToString()).ToArray(); // dt.Rows.Add(dataRow); // } // } // return dt; //} /// <summary> /// 导入 /// </summary> /// <typeparam name="T">模板类</typeparam> /// <param name="fileUrl">Excel文件绝对地址</param> /// <returns></returns> public Task <List <ExcelDataRow> > ValidateAsync <T>(ImportOption importOption) where T : class, new() { var excelData = GetExcelData <T>(importOption); if (importOption.MappingDictionary != null) { excelData.Data = MappingHeaderDictionary(excelData.Data, importOption.MappingDictionary); } AndFilter andFilter = new AndFilter() { filters = FiltersFactory.CreateFilters <T>(excelData.Header) }; FilterContext context = new FilterContext() { TypeFilterInfo = TypeFilterInfoFactory.CreateInstance(typeof(T), excelData.Header) }; excelData.Data = andFilter.Filter(excelData.Data, context, importOption); return(Task.FromResult(excelData.Data)); }