public static void process_read_employee_data( IObjectSpace objectSpace, string fileSavePath, string configFilePath, out List <Employee> readResult) { readResult = new List <Employee>(); using (var stream = File.Open(fileSavePath, FileMode.Open, FileAccess.Read)) { using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) { var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true } }); if (dataSet.Tables.Count > 0) { SheetTemplateImport templateImport = new SheetTemplateImport(configFilePath); DataTable table = dataSet.Tables[templateImport.SheetName]; foreach (var column in templateImport.Columns) { if (!string.IsNullOrWhiteSpace(column.excelHeader)) { table.Columns[column.excelHeader].ColumnName = column.propertyName; } } /// Map employee data process_map_employee_data(objectSpace, table, out List <Employee> mapResult); readResult = mapResult; } } } }
public static void process_read_health_insurrance_register_place_data( IObjectSpace objectSpace, string fileSavePath, string configFilePath) { using (var stream = File.Open(fileSavePath, FileMode.Open, FileAccess.Read)) { using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) { var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true } }); if (dataSet.Tables.Count > 0) { SheetTemplateImport templateImport = new SheetTemplateImport(configFilePath); DataTable table = dataSet.Tables[templateImport.SheetName]; foreach (var column in templateImport.Columns) { if (!string.IsNullOrWhiteSpace(column.excelHeader)) { table.Columns[column.excelHeader].ColumnName = column.propertyName; } } ///Map data from excel to bussiness object process_map_health_insurrance_register_place_data( objectSpace, table, out List <HealthInsuranceRegisterPlace> mapResult); } } } }
public ActionResult <CommonResponeModel> CheckFileEmployees([FromForm] IFormFile file) { if (file == null || string.IsNullOrWhiteSpace(file.FileName)) { Result = new ErrorResult(ActionType.CheckFileExcel, CommonMessageGlobal.Require("File Excel")); return(GetCommonRespone()); } try { string path = Path.Combine(AppGlobal.ExcelImportDestFolder, DateTime.Now.ToString("yyyyMMddHHmmss") + file.FileName); string pathXmlCheck = Path.Combine(AppGlobal.XmlTemplateImportFolder, "Employee.xml"); //save file to server file.SaveTo(path); //read file to check System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); using (var stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read)) { using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream)) { var dataSet = reader.AsDataSet(new ExcelDataSetConfiguration { ConfigureDataTable = _ => new ExcelDataTableConfiguration { UseHeaderRow = true // Use first row is ColumnName here } }); if (dataSet.Tables.Count > 0) { SheetTemplateImport templateImport = new SheetTemplateImport(pathXmlCheck); DataTable table = dataSet.Tables[templateImport.SheetName]; foreach (var column in templateImport.Columns) { if (!string.IsNullOrWhiteSpace(column.excelHeader)) { table.Columns[column.excelHeader].ColumnName = column.propertyName; } } //validate data var validator = new ImportEmployeeExcelFileValidator(categoryRepository); List <EmployeeImportDataTransfer> employees = table.ToList <EmployeeImportDataTransfer>(true); foreach (var employee in employees) { ValidationResult results = validator.Validate(employee); if (!results.IsValid) { foreach (var failure in results.Errors) { employee.IsError = true; employee.ErrorMessage.Add(failure.ErrorMessage); } } } Data = employees; } } } Result = new SuccessResultFactory().Factory(ActionType.CheckFileExcel); } catch (Exception) { Result = new ErrorResultFactory().Factory(ActionType.CheckFileExcel); } return(GetCommonRespone()); }