List of usage examples for org.apache.poi.ss.usermodel DateUtil isCellDateFormatted
public static boolean isCellDateFormatted(Cell cell)
From source file:tech.tablesaw.io.xlsx.XlsxReader.java
License:Apache License
private Boolean isBlank(Cell cell) { switch (cell.getCellType()) { case STRING:/*from ww w . ja v a 2s. c om*/ if (cell.getRichStringCellValue().length() > 0) { return false; } break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell) ? cell.getDateCellValue() != null : cell.getNumericCellValue() != 0) { return false; } break; case BOOLEAN: if (cell.getBooleanCellValue()) { return false; } break; case BLANK: return true; default: break; } return null; }
From source file:tech.tablesaw.io.xlsx.XlsxReader.java
License:Apache License
private ColumnType getColumnType(Cell cell) { switch (cell.getCellType()) { case STRING:/*from w ww.ja va 2 s . co m*/ return ColumnType.STRING; case NUMERIC: return DateUtil.isCellDateFormatted(cell) ? ColumnType.LOCAL_DATE_TIME : ColumnType.INTEGER; case BOOLEAN: return ColumnType.BOOLEAN; default: break; } return null; }
From source file:tech.tablesaw.io.xlsx.XlsxReader.java
License:Apache License
@SuppressWarnings("unchecked") private Column<?> appendValue(Column<?> column, Cell cell) { switch (cell.getCellType()) { case STRING://from ww w .jav a2 s . c o m column.appendCell(cell.getRichStringCellValue().getString()); return null; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); // This will return inconsistent results across time zones, but that matches Excel's behavior LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); column.appendCell(localDate.toString()); return null; } else { double num = cell.getNumericCellValue(); if (column.type() == ColumnType.INTEGER) { Column<Integer> intColumn = (Column<Integer>) column; if ((int) num == num) { intColumn.append((int) num); return null; } else if ((long) num == num) { Column<Long> altColumn = LongColumn.create(column.name(), column.size()); altColumn = intColumn.mapInto(s -> (long) s, altColumn); altColumn.append((long) num); return altColumn; } else { Column<Double> altColumn = DoubleColumn.create(column.name(), column.size()); altColumn = intColumn.mapInto(s -> (double) s, altColumn); altColumn.append(num); return altColumn; } } else if (column.type() == ColumnType.LONG) { Column<Long> longColumn = (Column<Long>) column; if ((long) num == num) { longColumn.append((long) num); return null; } else { Column<Double> altColumn = DoubleColumn.create(column.name(), column.size()); altColumn = longColumn.mapInto(s -> (double) s, altColumn); altColumn.append(num); return altColumn; } } else if (column.type() == ColumnType.DOUBLE) { Column<Double> doubleColumn = (Column<Double>) column; doubleColumn.append(num); return null; } } break; case BOOLEAN: if (column.type() == ColumnType.BOOLEAN) { Column<Boolean> booleanColumn = (Column<Boolean>) column; booleanColumn.append(cell.getBooleanCellValue()); return null; } default: break; } return null; }
From source file:Test.LeerExcelXlsx.java
public static void main(String args[]) throws IOException { FileInputStream file = new FileInputStream( new File("C:\\Users\\f2 a55m-s1\\Documents\\baseSqlLite\\Libro1.xlsx")); // Crear el objeto que tendra el libro de Excel XSSFWorkbook workbook = new XSSFWorkbook(file); /*//from w ww .j a v a 2 s.c o m * Obtenemos la primera pestaa a la que se quiera procesar indicando el indice. * Una vez obtenida la hoja excel con las filas que se quieren leer obtenemos el iterator * que nos permite recorrer cada una de las filas que contiene. */ XSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); Row row; // Recorremos todas las filas para mostrar el contenido de cada celda while (rowIterator.hasNext()) { row = rowIterator.next(); // Obtenemos el iterator que permite recorres todas las celdas de una fila Iterator<Cell> cellIterator = row.cellIterator(); Cell celda; while (cellIterator.hasNext()) { celda = cellIterator.next(); // Dependiendo del formato de la celda el valor se debe mostrar como String, Fecha, boolean, entero... switch (celda.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(celda)) { System.out.print(celda.getDateCellValue()); } else { System.out.print(celda.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: System.out.print(celda.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(celda.getBooleanCellValue()); break; } } System.out.println(""); } // cerramos el libro excel workbook.close(); }
From source file:th.co.aoe.makedev.missconsult.exam.service.impl.ReadWriteWorkbook_bk.java
License:Apache License
public static List<MissQuestion> setQuestion() { //???/* w w w .j a v a 2 s. c om*/ FileInputStream fileIn = null; // FileOutputStream fileOut = null; List<MissQuestion> missQuestions = new ArrayList<MissQuestion>(); try { try { //fileIn = new FileInputStream("/usr/local/data/Work/PROJECT/MakeDev/Exam/Question_Test/Service_Attitude.xls"); // ok //fileIn = new FileInputStream("/usr/local/data/Work/PROJECT/MakeDev/Exam/Question_Test/EPT_PLUS_THAI.xls");// ok //fileIn = new FileInputStream("/usr/local/data/Work/PROJECT/MakeDev/Exam/Question_Test/The4Factors_1.xls"); // ok // fileIn = new FileInputStream("/usr/local/data/Work/PROJECT/MakeDev/Exam/Question_Test/The4Factors_2.xls"); // ok //fileIn = new FileInputStream("/usr/local/data/Work/PROJECT/MakeDev/Exam/Question_Test/LeadershipAssessment_1.xls"); // ok fileIn = new FileInputStream( "/usr/local/data/Work/PROJECT/MakeDev/Exam/Question_Test/LeadershipAssessment_2.xls"); // ok } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } POIFSFileSystem fs = null; try { fs = new POIFSFileSystem(fileIn); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HSSFWorkbook wb = null; try { wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); for (Row row : sheet) { //System.out.println("row id="+row.getRowNum()+""); MissQuestion missQuestion = new MissQuestion(); //int rowId=row.getRowNum(); //if(rowId>0){ // ThaiCustomUser user =new ThaiCustomUser(); for (Cell cell : row) { int columnIndex = cell.getColumnIndex(); String value = ""; //System.out.println(" row id="+cell.getRowIndex()+",column id="+columnIndex+""); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: if (columnIndex == 0) { value = cell.getStringCellValue(); // System.out.println(" CELL_TYPE_STRING="+value); missQuestion.setMqId(Long.parseLong((cell.getRowIndex() + 1) + "")); missQuestion.setMqNameTh1(value); missQuestions.add(missQuestion); } break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { // System.out.println(" CELL_TYPE_NUMERIC DATE="+cell.getDateCellValue()); } else { double valuecel = cell.getNumericCellValue(); NumberFormat format = NumberFormat.getNumberInstance(); // format.setMaximumIntegerDigits(99); format.setGroupingUsed(false); // System.out.println(" CELL_TYPE_NUMERIC="+format.format(valuecel)); value = format.format(valuecel); //System.out.println(" CELL_TYPE_NUMERIC="+Double.toString(cell.getNumericCellValue())); // System.out.println(" CELL_TYPE_NUMERIC="+cell.getNumericCellValue()); /* Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()); DateFormat format2 = new SimpleDateFormat("yyyyMMdd"); System.out.println(" CELL_TYPE_NUMERIC date="+format2.format(date)); */ } break; case Cell.CELL_TYPE_BOOLEAN: // System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: // System.out.println("yy="+cell.getCellFormula()); break; default: // System.out.println(); } } } // } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } finally { if (fileIn != null) try { fileIn.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return missQuestions; }
From source file:XlsUtils.XlsComparator.java
/** * Obtiene el valor de una Cell de Excel * @param cell//from w ww. java 2 s . c o m * @return */ public static Object getCellValue(Cell cell) { Object result = null; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { result = cell.getDateCellValue(); } else { if (cell.getNumericCellValue() == (int) cell.getNumericCellValue()) { result = (int) cell.getNumericCellValue(); } else { result = cell.getNumericCellValue(); } } break; case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; case Cell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_FORMULA: case Cell.CELL_TYPE_BLANK: case Cell.CELL_TYPE_ERROR: result = null; break; } } return result; }