List of usage examples for org.apache.poi.ss.usermodel Cell getSheet
Sheet getSheet();
From source file:com.asakusafw.testdriver.excel.ExcelSheetDataModelSource.java
License:Apache License
private void evaluateInCell(Cell cell) throws IOException { try {/* ww w .j a v a 2 s . co m*/ Workbook workbook = cell.getSheet().getWorkbook(); FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator(); formulaEvaluator.evaluateInCell(cell); } catch (RuntimeException e) { throw new IOException(MessageFormat.format( Messages.getString("ExcelSheetDataModelSource.errorFailedToResolveFormulaCell"), //$NON-NLS-1$ id, cell.getRowIndex() + 1, cell.getColumnIndex() + 1), e); } }
From source file:com.b2international.snowowl.datastore.server.importer.ExcelUtilities.java
License:Apache License
/** * Returns true if the cell (NOT the text inside) is set to bold. * @param cell/* w ww .j ava2 s . c om*/ * @return */ public static boolean isBold(Cell cell) { CellStyle style = cell.getCellStyle(); Workbook workBook = cell.getSheet().getWorkbook(); Font font = workBook.getFontAt(style.getFontIndex()); XSSFFont xssfFont = (XSSFFont) font; return xssfFont.getBold(); }
From source file:com.b2international.snowowl.datastore.server.importer.ExcelUtilities.java
License:Apache License
private static String extractAsString(final Cell cell, final boolean formatNumber) { String value = ""; if (cell == null) { return value; }//from ww w. j a va 2 s . co m FormulaEvaluator formulaEvaluator = cell.getSheet().getWorkbook().getCreationHelper() .createFormulaEvaluator(); int type = cell.getCellType(); switch (type) { case Cell.CELL_TYPE_NUMERIC: if (formatNumber) { value = convertToString(cell.getNumericCellValue()); } else { value = convertToStringWithoutFormat(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: CellValue cellValue = formulaEvaluator.evaluate(cell); value = cellValue.getStringValue(); //type should be checked break; case Cell.CELL_TYPE_BOOLEAN: value = Boolean.toString(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_BLANK: //do nothing, sctId is null break; default: LOGGER.log(Level.SEVERE, "Unsupported cell type:" + type + " for cell: " + cell); break; } return null == value ? "" : value; }
From source file:com.cms.utils.ExportExcell.java
private Comment getcellComment(FormatExcell item, Cell cell) { ExcellHeaderComment headerCommand = item.getHeaderCommand(); Drawing sSFPatriarch = sh.createDrawingPatriarch(); CreationHelper factory = cell.getSheet().getWorkbook().getCreationHelper(); ClientAnchor anchor = factory.createClientAnchor(); Comment comment1 = sSFPatriarch.createCellComment(anchor); anchor.setCol1(headerCommand.getRow1()); anchor.setCol2(headerCommand.getRow2()); anchor.setRow1(headerCommand.getColumn1()); anchor.setRow2(headerCommand.getColumn2()); anchor.setDx1(headerCommand.getDx1()); anchor.setDx2(headerCommand.getDx2()); anchor.setDy1(headerCommand.getDy1()); anchor.setDy2(headerCommand.getDy2()); RichTextString rtf1 = factory.createRichTextString(headerCommand.getValue()); Font font = wb.createFont();/*from w w w .j a v a 2s . c o m*/ font.setFontName("Arial"); font.setFontHeightInPoints((short) 10); font.setBoldweight(Font.BOLDWEIGHT_NORMAL); font.setColor(IndexedColors.RED.getIndex()); rtf1.applyFont(font); comment1.setString(rtf1); comment1.setAuthor("Logistics"); // comment1.setColumn(cell.getColumnIndex()); // comment1.setRow(cell.getRowIndex()); return comment1; }
From source file:com.cms.utils.ExportExcell.java
private Comment getcellComment(Sheet sh, FormatExcell item, Cell cell) { ExcellHeaderComment headerCommand = item.getHeaderCommand(); Drawing sSFPatriarch = sh.createDrawingPatriarch(); CreationHelper factory = cell.getSheet().getWorkbook().getCreationHelper(); ClientAnchor anchor = factory.createClientAnchor(); Comment comment1 = sSFPatriarch.createCellComment(anchor); anchor.setCol1(headerCommand.getRow1()); anchor.setCol2(headerCommand.getRow2()); anchor.setRow1(headerCommand.getColumn1()); anchor.setRow2(headerCommand.getColumn2()); anchor.setDx1(headerCommand.getDx1()); anchor.setDx2(headerCommand.getDx2()); anchor.setDy1(headerCommand.getDy1()); anchor.setDy2(headerCommand.getDy2()); RichTextString rtf1 = factory.createRichTextString(headerCommand.getValue()); Font font = wb.createFont();/* www . j a va2 s. c o m*/ font.setFontName("Arial"); font.setFontHeightInPoints((short) 10); font.setBoldweight(Font.BOLDWEIGHT_NORMAL); font.setColor(IndexedColors.RED.getIndex()); rtf1.applyFont(font); comment1.setString(rtf1); comment1.setAuthor("Logistics"); // comment1.setColumn(cell.getColumnIndex()); // comment1.setRow(cell.getRowIndex()); return comment1; }
From source file:com.dituiba.excel.BaseExcelService.java
License:Apache License
/** * ?/* www . j a va 2 s . co m*/ * @param cell */ public static void setErrorStyle(Cell cell) { if (cell != null) { CellStyle newstyle = cell.getSheet().getWorkbook().createCellStyle(); CellStyle style = cell.getCellStyle(); if (style != null) { newstyle.cloneStyleFrom(style); } newstyle.setFillForegroundColor(IndexedColors.RED.getIndex()); newstyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cell.setCellStyle(newstyle); } }
From source file:com.dituiba.excel.DefaultOutputAdapter.java
License:Apache License
/** * ?/* w ww . j a v a 2 s . co m*/ * * @param fieldValue * @param fieldName * @return * @throws AdapterException */ public void outputDateAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell) throws AdapterException { log.debug("in DefaultOutputAdapter:outputDateAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue); Date date = null; if (fieldValue == null) { log.debug("fieldValue is null return"); cell.setCellValue(""); return; } else if (fieldValue instanceof Date) { log.debug("fieldValue instanceof Date "); date = (Date) fieldValue; } else if (fieldValue instanceof String) { log.debug("fieldValue instanceof String "); InputDateConfig config = dataBean.getInputConfig(fieldName); try { date = DateUtil.formatToDate((String) fieldValue, config.format()); } catch (ParseException e) { throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell); } } else if (fieldValue instanceof Long) { log.debug("fieldValue instanceof Long "); date = new Date((Long) fieldValue); } else { throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell); } Workbook workbook = cell.getSheet().getWorkbook(); OutputDateConfig outputConfig = dataBean.getOutputConfig(fieldName); CellStyle cellStyle = cell.getCellStyle(); if (cellStyle == null) cellStyle = workbook.createCellStyle(); CreationHelper createHelper = workbook.getCreationHelper(); cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(outputConfig.format())); cell.setCellStyle(cellStyle); cell.setCellValue(date); }
From source file:com.dituiba.excel.DefaultOutputAdapter.java
License:Apache License
public void outputNumericAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell) throws AdapterException { log.debug("in DefaultOutputAdapter:outputNumericAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue); if (ObjectHelper.isNullOrEmptyString(fieldValue)) return;//ww w. j a v a 2 s. co m OutputNumericConfig config = dataBean.getOutputConfig(fieldName); Workbook workbook = cell.getSheet().getWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); CreationHelper createHelper = workbook.getCreationHelper(); StringBuilder format = new StringBuilder("0"); for (int i = 0; i < config.floatCount(); i++) { if (i == 0) format.append("."); format.append("0"); } cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(format.toString())); cell.setCellValue(NumberUtils.format(fieldValue, config.floatCount())); cell.setCellStyle(cellStyle); }
From source file:com.dituiba.excel.DefaultOutputAdapter.java
License:Apache License
public void outputIntAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell) throws AdapterException { log.debug("in DefaultOutputAdapter:outputIntAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue); if (ObjectHelper.isNullOrEmptyString(fieldValue)) return;//from www . j a v a 2 s . com Workbook workbook = cell.getSheet().getWorkbook(); CellStyle cellStyle = workbook.createCellStyle(); CreationHelper createHelper = workbook.getCreationHelper(); cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#")); cell.setCellValue(NumberUtils.format(fieldValue, 0)); cell.setCellStyle(cellStyle); }
From source file:com.github.crab2died.utils.Utils.java
License:Open Source License
/** * ?/* ww w .j av a 2 s . com*/ * * @param cell ??? * @return ?? ??String * @author QingMings * Email:1821063757@qq.com * date 2018-01-13 */ public static String calculationFormula(Cell cell) { CellValue cellValue = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator() .evaluate(cell); return cellValue.formatAsString(); }