Esempio n. 1
0
        public ETDWorkbook[] ReadExcelFromDir(string excelDir)
        {
            if (string.IsNullOrEmpty(excelDir) || !Directory.Exists(excelDir))
            {
                return(null);
            }

            string[] excelFields = (from file in Directory.GetFiles(excelDir, "*.*", SearchOption.AllDirectories)
                                    where IsExcel(file)
                                    select file).ToArray();

            List <ETDWorkbook> books = new List <ETDWorkbook>();

            if (excelFields != null && excelFields.Length > 0)
            {
                foreach (var f in excelFields)
                {
                    ETDWorkbook book = ReadExcelFromFile(f);
                    if (book != null)
                    {
                        books.Add(book);
                    }
                }
            }

            return(books.ToArray());
        }
Esempio n. 2
0
        public ETDWorkbook ReadExcelFromFile(string excelPath)
        {
            if (!IsExcel(excelPath))
            {
                return(null);
            }
            string      ext         = Path.GetExtension(excelPath).ToLower();
            ETDWorkbook etdWorkbook = new ETDWorkbook(excelPath);

            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.Log(LogType.Warning, LogMessage.LOG_WORKBOOK_EMPTY, excelPath);
                    return(null);
                }

                logHandler.Log(LogType.Info, LogMessage.LOG_WORKBOOK_START, excelPath);

                for (int i = 0; i < workbook.NumberOfSheets; i++)
                {
                    ISheet sheet = workbook.GetSheetAt(i);

                    string sheetName = sheet.SheetName;
                    if (string.IsNullOrEmpty(sheetName))
                    {
                        logHandler.Log(LogType.Warning, LogMessage.LOG_SHEET_NAME_NULL, i);
                        continue;
                    }
                    if (sheetName.StartsWith("#"))
                    {
                        logHandler.Log(LogType.Info, LogMessage.LOG_IGNORE_SHEET, sheetName);
                        continue;
                    }
                    if (!Regex.IsMatch(sheetName, WorkbookConst.SHEET_NAME_REGEX))
                    {
                        logHandler.Log(LogType.Error, LogMessage.LOG_SHEET_NAME_NOT_MATCH, sheetName, WorkbookConst.SHEET_NAME_REGEX);
                        continue;
                    }

                    Sheet dataSheet = ReadFromSheet(sheet);
                    etdWorkbook.AddSheet(dataSheet);
                }

                logHandler.Log(LogType.Info, LogMessage.LOG_WORKBOOK_END, excelPath);
            }
            return(etdWorkbook);
        }