/** * Get the value of the cell as a boolean. * <p> * For strings, numbers, and errors, we throw an exception. For blank cells we return a false. * </p> * @return the value of the cell as a boolean * @throws IllegalStateException if the cell type returned by {@link #getCellType()} * is not CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA */ public static boolean getBooleanCellValue(Cell _cell) { int cellType = getCellType(_cell); switch(cellType) { case CELL_TYPE_BLANK: return false; case CELL_TYPE_BOOLEAN: return _cell.getV()!=null && TRUE_AS_STRING.equals(_cell.getV()); case CELL_TYPE_FORMULA: //YK: should throw an exception if requesting boolean value from a non-boolean formula return _cell.getV()!=null && TRUE_AS_STRING.equals(_cell.getV()); default: throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); } } }
/** * Detect cell type based on the "t" attribute of the CTCell bean */ private static int getBaseCellType(Cell _cell, boolean blankCells) { switch (_cell.getT()) { case B: return CELL_TYPE_BOOLEAN; case N: if (_cell.getV()==null && blankCells) { // ooxml does have a separate cell type of 'blank'. A blank cell gets encoded as // (either not present or) a numeric cell with no value set. // The formula evaluator (and perhaps other clients of this interface) needs to // distinguish blank values which sometimes get translated into zero and sometimes // empty string, depending on context return CELL_TYPE_BLANK; } return CELL_TYPE_NUMERIC; case E: return CELL_TYPE_ERROR; case S: // String is in shared strings case INLINE_STR: // String is inline in cell case STR: return CELL_TYPE_STRING; default: throw new IllegalStateException("Illegal cell type: " + _cell.getT()); } }
/** * Get the value of the cell as a boolean. * <p> * For strings, numbers, and errors, we throw an exception. For blank cells we return a false. * </p> * @return the value of the cell as a boolean * @throws IllegalStateException if the cell type returned by {@link #getCellType()} * is not CELL_TYPE_BOOLEAN, CELL_TYPE_BLANK or CELL_TYPE_FORMULA */ public static boolean getBooleanCellValue(Cell _cell) { int cellType = getCellType(_cell); switch(cellType) { case CELL_TYPE_BLANK: return false; case CELL_TYPE_BOOLEAN: return _cell.getV()!=null && TRUE_AS_STRING.equals(_cell.getV()); case CELL_TYPE_FORMULA: //YK: should throw an exception if requesting boolean value from a non-boolean formula return _cell.getV()!=null && TRUE_AS_STRING.equals(_cell.getV()); default: throw typeMismatch(CELL_TYPE_BOOLEAN, cellType, false); } } }
... for (Cell c : r.getC() ) { if (c.getT().equals(STCellType.S)) { System.out.println( " " + c.getR() + " contains " + sharedStrings.getJaxbElement().getSi().get(Integer.parseInt(c.getV())).getT() ); } else { // TODO: handle other cell types System.out.println( " " + c.getR() + " contains " + c.getV() ); } } ...
/** * Detect cell type based on the "t" attribute of the CTCell bean */ private static int getBaseCellType(Cell _cell, boolean blankCells) { switch (_cell.getT()) { case B: return CELL_TYPE_BOOLEAN; case N: if (_cell.getV()==null && blankCells) { // ooxml does have a separate cell type of 'blank'. A blank cell gets encoded as // (either not present or) a numeric cell with no value set. // The formula evaluator (and perhaps other clients of this interface) needs to // distinguish blank values which sometimes get translated into zero and sometimes // empty string, depending on context return CELL_TYPE_BLANK; } return CELL_TYPE_NUMERIC; case E: return CELL_TYPE_ERROR; case S: // String is in shared strings case INLINE_STR: // String is inline in cell case STR: return CELL_TYPE_STRING; default: throw new IllegalStateException("Illegal cell type: " + _cell.getT()); } }
public String getCellValue(Cell cell) { if (cell.getV() == null) return null; if (cell.getT().equals(STCellType.S)) { CTRst ctRst = sharedStrings.getJaxbElement().getSi().get(Integer.parseInt(cell.getV())); String value = null; if (ctRst.getT() != null) { value = ctRst.getT().getValue(); } else { if (ctRst.getR() != null) { StringBuilder stringBuilder = new StringBuilder(); for (CTRElt ctrElt : ctRst.getR()) { if (ctrElt.getT() != null) { stringBuilder.append(ctrElt.getT().getValue()); } } value = stringBuilder.toString(); } } return value; } else { return cell.getV(); } }
public String getCellValue(Cell cell) { if (cell.getV() == null) return null; if (cell.getT().equals(STCellType.S)) { CTSst jaxbElement; try { jaxbElement = sharedStrings.getContents(); } catch (Docx4JException e) { throw new RuntimeException("Unable to get strings contents", e); } CTRst ctRst = jaxbElement.getSi().get(Integer.parseInt(cell.getV())); String value = null; if (ctRst.getT() != null) { value = ctRst.getT().getValue(); } else { if (ctRst.getR() != null) { StringBuilder stringBuilder = new StringBuilder(); for (CTRElt ctrElt : ctRst.getR()) { if (ctrElt.getT() != null) { stringBuilder.append(ctrElt.getT().getValue()); } } value = stringBuilder.toString(); } } return value; } else { return cell.getV(); } }
protected void saveXlsxAsCsv(Document document, OutputStream outputStream) throws IOException, Docx4JException { CSVWriter writer = new CSVWriter(new OutputStreamWriter(outputStream), ';', CSVWriter.DEFAULT_QUOTE_CHARACTER); for (Document.SheetWrapper sheetWrapper : document.getWorksheets()) { Worksheet worksheet = sheetWrapper.getWorksheet().getContents(); for (Row row : worksheet.getSheetData().getRow()) { String rows[] = new String[row.getC().size()]; List<Cell> cells = row.getC(); boolean emptyRow = true; for (int i = 0; i < cells.size(); i++) { checkThreadInterrupted(); Cell cell = cells.get(i); String value = cell.getV(); rows[i] = value; if (value != null && !value.isEmpty()) emptyRow = false; } if (!emptyRow) writer.writeNext(rows); } } writer.close(); }
protected void saveXlsxAsCsv(Document document, OutputStream outputStream) throws IOException, Docx4JException { CSVWriter writer = new CSVWriter(new OutputStreamWriter(outputStream), ';', CSVWriter.DEFAULT_QUOTE_CHARACTER); for (Document.SheetWrapper sheetWrapper : document.getWorksheets()) { Worksheet worksheet = sheetWrapper.getWorksheet().getContents(); for (Row row : worksheet.getSheetData().getRow()) { String rows[] = new String[row.getC().size()]; List<Cell> cells = row.getC(); boolean emptyRow = true; for (int i = 0; i < cells.size(); i++) { checkThreadInterrupted(); Cell cell = cells.get(i); String value = cell.getV(); rows[i] = value; if (value != null && !value.isEmpty()) emptyRow = false; } if (!emptyRow) writer.writeNext(rows); } } writer.close(); }