public static WDBSheet[] ReadFromDirectory(string excelDir, WDBExcelStyle readerStyle = null) { if (string.IsNullOrEmpty(excelDir) || !Directory.Exists(excelDir)) { logHandler?.Invoke(LogType.Error, string.Format(LogMessage.ERROR_DIRECTOR_NOT_EXIST, excelDir)); return(null); } string[] excelFields = (from file in Directory.GetFiles(excelDir, "*.*", SearchOption.AllDirectories) where IsExcel(file) select file).ToArray(); List <WDBSheet> sheetList = new List <WDBSheet>(); if (excelFields != null && excelFields.Length > 0) { foreach (var f in excelFields) { WDBSheet[] sheets = ReadFromFile(f, readerStyle); if (sheets != null && sheets.Length > 0) { sheetList.AddRange(sheets); } } } return(sheetList.ToArray()); }
public static WDBSheet[] ReadFromFile(string excelPath, WDBExcelStyle readerStyle = null) { readerExcelStyle = readerStyle; if (readerExcelStyle == null) { readerExcelStyle = WDBExcelStyle.DefaultStyle; } if (!IsExcel(excelPath)) { logHandler?.Invoke(LogType.Error, string.Format(LogMessage.ERROR_FILE_NOT_EXCEL, excelPath)); return(null); } List <WDBSheet> sheetList = new List <WDBSheet>(); string ext = Path.GetExtension(excelPath).ToLower(); try { using (FileStream fs = new FileStream(excelPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { IWorkbook workbook = null; if (ext == ".xlsx") { workbook = new XSSFWorkbook(fs); } else { workbook = new HSSFWorkbook(fs); } if (workbook == null || workbook.NumberOfSheets == 0) { logHandler?.Invoke(LogType.Error, string.Format(LogMessage.ERROR_WORKBOOK_EMPTY, excelPath)); return(null); } logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_START_READ_WORKBOOK, excelPath)); for (int i = 0; i < workbook.NumberOfSheets; i++) { ISheet sheet = workbook.GetSheetAt(i); string sheetName = sheet.SheetName; if (string.IsNullOrEmpty(sheetName)) { logHandler?.Invoke(LogType.Warning, LogMessage.WARN_SHEET_NAME_EMPTY); continue; } if (sheetName.StartsWith("#")) { logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_SHEET_IGNORE, sheetName)); continue; } WDBSheet wdbSheet = ReadFromSheet(sheet); sheetList.Add(wdbSheet); } logHandler?.Invoke(LogType.Info, string.Format(LogMessage.INFO_END_READ_WORKBOOK, excelPath)); } } catch (Exception e) { logHandler?.Invoke(LogType.Error, e.Message); } readerExcelStyle = null; return(sheetList.ToArray()); }