List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getCellFormula
public String getCellFormula()
From source file:ro.nextreports.engine.exporter.util.XlsUtil.java
License:Apache License
/** * Copy a cell to another cell/*w ww. j av a 2s . c o m*/ * * @param oldCell cell to be copied * @param newCell cell to be created * @param styleMap style map */ public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) { if (styleMap != null) { if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) { newCell.setCellStyle(oldCell.getCellStyle()); } else { int stHashCode = oldCell.getCellStyle().hashCode(); HSSFCellStyle newCellStyle = styleMap.get(stHashCode); if (newCellStyle == null) { newCellStyle = newCell.getSheet().getWorkbook().createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); styleMap.put(stHashCode, newCellStyle); } newCell.setCellStyle(newCellStyle); } } switch (oldCell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_BLANK: newCell.setCellType(HSSFCell.CELL_TYPE_BLANK); break; case HSSFCell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; default: break; } }
From source file:shouQiSystem.user.ReadDriverEXL.java
private String getValue(HSSFCell hssfCell) { DecimalFormat df = new DecimalFormat("#"); switch (hssfCell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(hssfCell)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(sdf.format(HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue())).toString()); return sdf.format(HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue())).toString(); }/*from ww w . java 2 s. com*/ System.out.println(df.format(hssfCell.getNumericCellValue()).toString()); return df.format(hssfCell.getNumericCellValue()).toString(); case HSSFCell.CELL_TYPE_STRING: System.out.println(hssfCell.getStringCellValue()); return hssfCell.getStringCellValue(); case HSSFCell.CELL_TYPE_FORMULA: return hssfCell.getCellFormula(); case HSSFCell.CELL_TYPE_BLANK: return ""; case HSSFCell.CELL_TYPE_BOOLEAN: return hssfCell.getBooleanCellValue() + ""; case HSSFCell.CELL_TYPE_ERROR: return hssfCell.getErrorCellValue() + ""; } return ""; }
From source file:swift.selenium.TransactionMapping.java
License:Open Source License
@SuppressWarnings("null") public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex, HashMap<String, Object> inputHashTable) throws IOException { HSSFCell reqCell = null; Object actualvalue = null;//from ww w.j ava2s. c o m String req = ""; DataFormatter fmt = new DataFormatter(); if (inputHashTable.isEmpty() == true) { inputHashTable = getValueFromHashMap(reqSheet); } HSSFRow rowActual = reqSheet.getRow(rowIndex); if (inputHashTable.get(reqValue) == null) { report.setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet"); pauseFun("Column " + reqValue + " not Found. Please Check input Sheet"); } else { actualvalue = inputHashTable.get(reqValue);// rowHeader.getCell(colIndex).toString(); if (actualvalue != null) { int colIndex = Integer.parseInt(actualvalue.toString()); reqCell = rowActual.getCell(colIndex); if (reqCell == null) { System.out.println(reqValue + " is Null"); } else { int type = reqCell.getCellType(); switch (type) { case HSSFCell.CELL_TYPE_BLANK: req = ""; break; case HSSFCell.CELL_TYPE_NUMERIC: req = fmt.formatCellValue(reqCell); break; case HSSFCell.CELL_TYPE_STRING: req = reqCell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: req = Boolean.toString(reqCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: req = "error"; break; case HSSFCell.CELL_TYPE_FORMULA: req = reqCell.getCellFormula(); break; } } } else { req = reqCell.getStringCellValue(); System.out.println("null"); } } return req; }
From source file:SwiftSeleniumWeb.WebHelper.java
License:Open Source License
@SuppressWarnings("null") public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex, HashMap<String, Object> inputHashTable) throws IOException { HSSFCell reqCell = null; Object actualvalue = null;/* w w w. ja v a 2 s. co m*/ String req = ""; DataFormatter fmt = new DataFormatter(); if (inputHashTable.isEmpty() == true) { inputHashTable = getValueFromHashMap(reqSheet); } HSSFRow rowActual = reqSheet.getRow(rowIndex); if (inputHashTable.get(reqValue) == null) { SwiftSeleniumWeb.WebDriver.report .setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet"); MainController.pauseFun("Column " + reqValue + " not Found. Please Check input Sheet"); } else { actualvalue = inputHashTable.get(reqValue);//rowHeader.getCell(colIndex).toString(); if (actualvalue != null) { int colIndex = Integer.parseInt(actualvalue.toString()); reqCell = rowActual.getCell(colIndex); if (reqCell == null) { System.out.println(reqValue + " is Null"); } else { int type = reqCell.getCellType(); switch (type) { case HSSFCell.CELL_TYPE_BLANK: req = ""; break; case HSSFCell.CELL_TYPE_NUMERIC: req = fmt.formatCellValue(reqCell); break; case HSSFCell.CELL_TYPE_STRING: req = reqCell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: req = Boolean.toString(reqCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: req = "error"; break; case HSSFCell.CELL_TYPE_FORMULA: req = reqCell.getCellFormula(); break; } } } else { req = reqCell.getStringCellValue(); System.out.println("null"); } } return req; }
From source file:ubic.basecode.io.excel.ExcelUtil.java
License:Apache License
/** * @param sheet// w w w . j av a 2 s .c o m * @param row * @param col * @return */ public static String getValue(HSSFSheet sheet, int row, int col) { if (col > 255) { throw new RuntimeException("Column position is over 255"); } if (sheet.getRow(row) == null) return null; HSSFCell cell = sheet.getRow(row).getCell(col); if (cell == null) { return null; } if (cell.getCellType() == Cell.CELL_TYPE_STRING) return cell.getRichStringCellValue().getString(); if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { // WARNING not ideal for numbers. return Double.toString(cell.getNumericCellValue()); } if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) return cell.getCellFormula(); return ""; }
From source file:uk.ac.ebi.mnb.parser.ExcelXLSHelper.java
License:Open Source License
public String getCellString(HSSFCell cell) { if (cell == null) { return ""; }/* ww w. ja v a 2 s . c om*/ int type = cell.getCellType(); if (type == HSSFCell.CELL_TYPE_NUMERIC) { return Double.toString(cell.getNumericCellValue()); } else if (type == HSSFCell.CELL_TYPE_STRING) { return cell.getStringCellValue().trim(); } else if (type == HSSFCell.CELL_TYPE_BLANK) { return ""; } else if (type == HSSFCell.CELL_TYPE_ERROR) { return "ERROR!"; } else if (type == HSSFCell.CELL_TYPE_FORMULA) { return cell.getCellFormula(); } else { LOGGER.info("Unhandled cell type: " + cell.getCellType()); return ""; } }
From source file:vone.HSSFReadWrite.java
License:Apache License
/** * Method main//from w w w .j ava2s . com * * Given 1 argument takes that as the filename, inputs it and dumps the cell * values/types out to sys.out.<br/> * * given 2 arguments where the second argument is the word "write" and the * first is the filename - writes out a sample (test) spreadsheet see * {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/> * * given 2 arguments where the first is an input filename and the second an * output filename (not write), attempts to fully read in the spreadsheet * and fully write it out.<br/> * * given 3 arguments where the first is an input filename and the second an * output filename (not write) and the third is "modify1", attempts to read * in the spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col * 3 to "MODIFIED CELL" then writes it out. Hence this is "modify test 1". * If you take the output from the write test, you'll have a valid scenario. */ @SuppressWarnings("deprecation") public static void main(String[] args) { if (args.length < 1) { System.err.println("At least one argument expected"); return; } String fileName = args[0]; try { if (args.length < 2) { HSSFWorkbook wb = HSSFReadWrite.readFile(fileName); System.out.println("Data dump:\n"); for (int k = 0; k < wb.getNumberOfSheets(); k++) { HSSFSheet sheet = wb.getSheetAt(k); int rows = sheet.getPhysicalNumberOfRows(); System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows + " row(s)."); for (int r = 0; r < rows; r++) { HSSFRow row = sheet.getRow(r); if (row == null) { continue; } // int cells = row.getPhysicalNumberOfCells(); int cells = row.getLastCellNum(); System.out.println("\nROW " + row.getRowNum() + " has " + cells + " cell(s)."); for (int c = 0; c < cells; c++) { HSSFCell cell = row.getCell(c); String value = null; if (cell == null) { System.out.println("CELL col=" + c + " VALUE= Empty"); continue; } switch (cell.getCellTypeEnum()) { case FORMULA: value = "FORMULA value=" + cell.getCellFormula(); break; case NUMERIC: value = "NUMERIC value=" + cell.getNumericCellValue(); break; case STRING: value = "STRING value=" + cell.getStringCellValue(); break; default: } System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE=" + value); } } } wb.close(); } } catch (Exception e) { e.printStackTrace(); } }