
prerna.poi.main.helper.excel.ExcelParsing Maven / Gradle / Ivy
The newest version!
package prerna.poi.main.helper.excel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
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 prerna.algorithm.api.SemossDataType;
import prerna.date.SemossDate;
import prerna.poi.main.helper.FileHelperUtil;
public class ExcelParsing {
private static final int NUM_ROWS_TO_PREDICT_TYPES = 500;
private ExcelParsing() {
}
public static boolean isExcelFile(String filePath) {
String file = filePath.toLowerCase();
if(file.endsWith(".xlsx") || file.endsWith("xlsm") || file.endsWith("xls")) {
return true;
}
return false;
}
public static boolean isEmptyCell(Cell thisCell) {
if (thisCell == null
|| thisCell.getCellType() == CellType.BLANK
|| thisCell.toString().trim().isEmpty()) {
return true;
}
return false;
}
/**
* Get the cell values
*
* @param thisCell
* @return
*/
public static Object getCell(Cell thisCell) {
if (thisCell == null) {
return null;
}
CellType type = thisCell.getCellType();
if (type == CellType.BLANK) {
return "";
}
if (type == CellType.STRING) {
return thisCell.getStringCellValue();
} else if (type == CellType.NUMERIC) {
if (DateUtil.isCellDateFormatted(thisCell)) {
return new SemossDate(thisCell.getDateCellValue(), thisCell.getCellStyle().getDataFormatString());
}
return thisCell.getNumericCellValue();
} else if (type == CellType.BOOLEAN) {
return thisCell.getBooleanCellValue();
} else if (type == CellType.FORMULA) {
// do the same for the formula value
CellType formulatype = thisCell.getCachedFormulaResultType();
if (formulatype == CellType.BLANK) {
return "";
}
if (formulatype == CellType.STRING) {
return thisCell.getStringCellValue();
} else if (formulatype == CellType.NUMERIC) {
if (DateUtil.isCellDateFormatted(thisCell)) {
return new SemossDate(thisCell.getDateCellValue(), thisCell.getCellStyle().getDataFormatString());
}
return thisCell.getNumericCellValue();
} else if (formulatype == CellType.BOOLEAN) {
return thisCell.getBooleanCellValue();
}
}
return null;
}
/*
* Methods around predicting types
*/
public static Object[][] predictTypes(Sheet sheet, String range) {
// for a given sheet
// loop through and determine the types
// based on a block in a given range
// range index is start col, start row, end col, end row
int[] rangeIndex = ExcelRange.getSheetRangeIndex(range);
int numCols = rangeIndex[2] - rangeIndex[0] + 1;
Object[][] predictedTypes = new Object[numCols][3];
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy