package com.smtscript.lib.jsexcel; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.mozilla.javascript.NativeArray; import org.mozilla.javascript.NativeObject; import com.smtscript.utils.SMTStatic; public class JSPOIExcelSheetReader { private Sheet _sheet; public JSPOIExcelSheetReader(Sheet sheet) { _sheet = sheet; } public int getRowCount() { return _sheet.getLastRowNum(); } public NativeArray readRowValues(int row) throws Exception { if(row < _sheet.getFirstRowNum() || row > _sheet.getLastRowNum()) return null; Row excelRow = _sheet.getRow(row); int colStart = excelRow.getFirstCellNum(); int colCount = excelRow.getLastCellNum(); List colNames = new ArrayList<>(); for(int i = 0; i < colStart; i ++) colNames.add(""); for(int i = colStart; i < colCount; i ++) { String value = readString(row, i); colNames.add(value); } return new NativeArray(colNames.toArray(new Object[colNames.size()])); } public NativeObject readRowMap(int row, NativeArray colNames) throws Exception { NativeObject nvValues = new NativeObject(); for(int i = 0; i < colNames.size(); i ++) { String name = (String) SMTStatic.unwrapObject(colNames.get(i)); if(SMTStatic.isNullOrEmpty(name)) continue; SMTStatic.putJSNotNullValue(nvValues, name, readString(row, i)); } return nvValues; } public NativeArray readUpperColNames(int row) throws Exception { return new NativeArray(__readColNameList__(row, true)); } public NativeArray readColNames(int row) throws Exception { return new NativeArray(__readColNameList__(row, false)); } public Object[] __readColNameList__(int row, boolean toUpper) throws Exception { if(row < _sheet.getFirstRowNum() || row > _sheet.getLastRowNum()) return null; Row excelRow = _sheet.getRow(row); int colStart = excelRow.getFirstCellNum(); int colCount = excelRow.getLastCellNum(); List colNames = new ArrayList<>(); for(int i = 0; i < colStart; i ++) colNames.add(""); for(int i = colStart; i < colCount; i ++) { String name = readString(row, i).trim(); name = name.replace("\r", "").replace("\n", "").replace(" ", "").replace("\t", "").trim(); if(toUpper) name = name.toUpperCase(); colNames.add(name); } return colNames.toArray(new Object[colNames.size()]); } public String readString(int row, int col) throws Exception { if(row < _sheet.getFirstRowNum() || row > _sheet.getLastRowNum()) return ""; Row excelRow = _sheet.getRow(row); if(excelRow == null) return ""; int colStart = excelRow.getFirstCellNum(); int colCount = excelRow.getLastCellNum(); if(col < colStart || col >= colCount) return ""; Cell excelCell = excelRow.getCell(col); if(excelCell == null) return ""; CellType type = excelCell.getCellType(); if(type == CellType.FORMULA) type = excelCell.getCachedFormulaResultType(); String value = ""; if(type == CellType.NUMERIC) { if(DateUtil.isCellDateFormatted(excelCell)) value = SMTStatic.toString(excelCell.getDateCellValue()); else value = SMTStatic.toString(excelCell.getNumericCellValue()); } else if(type == CellType.BLANK) { value = ""; } else if(type == CellType.BOOLEAN) { value = excelCell.getBooleanCellValue() ? "TRUE" : "FALSE"; } else { value = excelCell.getStringCellValue(); } return value; } }