List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getBooleanCellValue
@Override public boolean getBooleanCellValue()
From source file:gda.hrpd.data.ExcelWorkbook.java
License:Open Source License
/** * gets value from the specified cell and return it as String. * //w w w . j a va 2 s .c o m * @param cell * @return value from cell as a String */ public String getCellValue(HSSFCell cell) { // If the cell is null return an empty string if (cell == null) { return ""; } String value = null; if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { value = cell.getRichStringCellValue().toString(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { value = String.valueOf(cell.getNumericCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { value = " "; } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { value = String.valueOf(cell.getBooleanCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_ERROR) { value = String.valueOf(cell.getErrorCellValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { value = cell.getCellFormula(); } return value; }
From source file:gov.nih.nci.ncicb.cadsr.bulkloader.util.excel.ExcelUtility.java
License:BSD License
public static Boolean getBoolean(HSSFSheet sheet, int row, short col) { HSSFRow hssfRow = getRow(sheet, row); if (hssfRow == null) { return null; }//from www .ja v a 2 s.co m HSSFCell cell = getRow(sheet, row).getCell(col); if (isNull(cell)) { return null; } return new Boolean(cell.getBooleanCellValue()); }
From source file:gov.nih.nci.ncicb.cadsr.bulkloader.util.excel.ExcelUtility.java
License:BSD License
public static Object getObject(HSSFSheet sheet, int row, short col) { HSSFRow hssfRow = getRow(sheet, row); if (hssfRow == null) { return null; }//w ww . ja v a 2 s .c o m HSSFCell cell = getRow(sheet, row).getCell(col); if (cell == null) { return null; } try { String val = cell.getStringCellValue(); if (val != null && val.equalsIgnoreCase("(null)")) { return null; } } catch (Exception t) { } int type = cell.getCellType(); switch (type) { case HSSFCell.CELL_TYPE_BLANK: return ""; case HSSFCell.CELL_TYPE_BOOLEAN: return new Boolean(cell.getBooleanCellValue()); case HSSFCell.CELL_TYPE_ERROR: return new Byte(cell.getErrorCellValue()); case HSSFCell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case HSSFCell.CELL_TYPE_NUMERIC: return new Double(cell.getNumericCellValue()); case HSSFCell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return null; } }
From source file:Import.Utils.XSSFConvert.java
/** * @param oldCell/*w w w.ja va 2s .co m*/ * @param newCell * @param styleMap */ public static void copyCell(HSSFCell oldCell, XSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) { if (styleMap != null) { int stHashCode = oldCell.getCellStyle().hashCode(); HSSFCellStyle sourceCellStyle = styleMap.get(stHashCode); XSSFCellStyle destnCellStyle = newCell.getCellStyle(); if (sourceCellStyle == null) { sourceCellStyle = oldCell.getSheet().getWorkbook().createCellStyle(); } destnCellStyle.cloneStyleFrom(oldCell.getCellStyle()); styleMap.put(stHashCode, sourceCellStyle); newCell.setCellStyle(destnCellStyle); } 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:ispyb.common.util.upload.UploadShipmentUtils.java
License:Open Source License
/** * Converts from Excel Cell contents to a String * //from w ww. ja v a 2 s . c o m * @param cell * The Cell to convert * @return A String value of the contents of the cell */ public static String cellToString(HSSFCell cell) { String retVal = ""; if (cell == null) { return retVal; } if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { retVal = cell.getStringCellValue(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { retVal = String.valueOf(new Double(cell.getNumericCellValue()).intValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { if (new Boolean(cell.getBooleanCellValue()) == Boolean.TRUE) { retVal = "true"; } else { retVal = "false"; } } return retVal; }
From source file:ispyb.common.util.upload.XLSParser.java
License:Open Source License
/** * Converts from Excel Cell contents to a String * /*from w w w. ja v a 2 s . com*/ * @param cell * The Cell to convert * @return A String value of the contents of the cell */ public String cellToString(HSSFCell cell) { String retVal = ""; if (cell == null) { return retVal; } if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { retVal = cell.getStringCellValue(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { retVal = String.valueOf(new Double(cell.getNumericCellValue()).intValue()); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { if (new Boolean(cell.getBooleanCellValue()) == Boolean.TRUE) { retVal = "true"; } else { retVal = "false"; } } return retVal; }
From source file:ispyb.common.util.upload.XLSParser.java
License:Open Source License
/** * Converts from Excel Cell contents to a boolean * // w w w . ja va 2 s . c o m * @param cell * The Cell to convert * @return The boolean value contained within the Cell or false if the Cell is not the correct type or is undefined */ public boolean cellToBoolean(HSSFCell cell) { Boolean retVal = new Boolean(false); if (cell == null) { return retVal.booleanValue(); } if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { retVal = new Boolean(cell.getBooleanCellValue()); } return retVal.booleanValue(); }
From source file:net.openchrom.xxd.processor.supplier.rscripting.ui.jobs.LoadExcelJob.java
License:Open Source License
private void loadExcel(final String file) { final File fil = new File(file); if (fil.exists()) { canRead = true;/*w w w . jav a2 s . co m*/ if (grid != null) { try { InputStream inp = new FileInputStream(file); try { wb = new HSSFWorkbook(inp); } catch (Exception e) { MsgDialog.message("Wrong format!\nOnly Excel 97-2007 is supported!"); canRead = false; } } catch (IOException ex) { ex.printStackTrace(); } if (canRead) { for (s = 0; s < wb.getNumberOfSheets(); s++) { Display display = PlatformUI.getWorkbench().getDisplay(); display.syncExec(new Runnable() { public void run() { String name = fil.getName(); grid = new Spread().spread(SampleView.getTabFolder(), 0, 0, name); SampleView.setGrid(grid); HSSFSheet sheet = wb.getSheetAt(s); int colCount = grid.getColumnCount(); int rowCount = grid.getItemCount(); int exelRow = endOfRow(sheet); int exelColumn = endOfColumn(sheet); // System.out.println(exelRow + " " + exelColumn // + "---" + sheet.getPhysicalNumberOfRows() + // " " + // sheet.getRow(0).getPhysicalNumberOfCells()); if (colCount < exelColumn) { int diff = exelColumn - colCount; for (int i = 0; i < diff; i++) { GridColumn column = new GridColumn(grid, SWT.NONE); column.setText("C " + (i + 1 + colCount)); column.setWidth(50); } } if (rowCount < exelRow) { int diff = exelRow - rowCount; for (int i = 0; i < diff; i++) { new GridItem(grid, SWT.NONE).setHeight(16); } } // Iterate over each row in the sheet int rows = sheet.getPhysicalNumberOfRows(); for (int i = 0; i < exelRow; i++) { HSSFRow row = sheet.getRow(i); if (row == null) { for (int u = 0; u < exelColumn; u++) { grid.getItem(i).setText(u, " "); } } else { for (int u = 0; u < exelColumn; u++) { HSSFCell cell = row.getCell(u); if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_NUMERIC: String val = String.valueOf(cell.getNumericCellValue()); grid.getItem(i).setText(u, val); break; case HSSFCell.CELL_TYPE_STRING: HSSFRichTextString st = cell.getRichStringCellValue(); String val2 = st.getString(); grid.getItem(i).setText(u, val2); break; case HSSFCell.CELL_TYPE_FORMULA: try { String val3 = String.valueOf(cell.getNumericCellValue()); grid.getItem(i).setText(u, val3); } catch (Exception e) { String s2 = cell.getCellFormula(); grid.getItem(i).setText(u, s2); } break; case HSSFCell.CELL_TYPE_BLANK: grid.getItem(i).setText(u, " "); break; case HSSFCell.CELL_TYPE_BOOLEAN: boolean s4 = cell.getBooleanCellValue(); if (s4) { grid.getItem(i).setText(u, "TRUE"); } else { grid.getItem(i).setText(u, "FALSE"); } break; default: break; } } else { grid.getItem(i).setText(u, " "); } } } } } }); } wb = null; } } } else { MsgDialog.message("File not found!"); } }
From source file:net.sourceforge.jaulp.export.excel.poi.ExportExcelUtils.java
License:Apache License
/** * Exportiert die bergebene excel-Datei in eine Liste mit zweidimensionalen Arrays fr jeweils * ein sheet in der excel-Datei.//w w w. j a va 2s . c o m * * @param excelSheet * Die excel-Datei. * @return Gibt eine Liste mit zweidimensionalen Arrays fr jeweils ein sheet in der excel-Datei * zurck. * @throws IOException * Fals ein Fehler beim Lesen aufgetreten ist. * @throws FileNotFoundException * Fals die excel-Datei nicht gefunden wurde. */ public static List<String[][]> exportWorkbook(File excelSheet) throws IOException, FileNotFoundException { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelSheet)); HSSFWorkbook wb = new HSSFWorkbook(fs); int numberOfSheets = wb.getNumberOfSheets(); List<String[][]> sheetList = new ArrayList<>(); for (int sheetNumber = 0; sheetNumber < numberOfSheets; sheetNumber++) { HSSFSheet sheet = null; sheet = wb.getSheetAt(sheetNumber); int rows = sheet.getLastRowNum(); int columns = sheet.getRow(0).getLastCellNum(); String[][] excelSheetInTDArray = null; excelSheetInTDArray = new String[rows][columns]; for (int i = 0; i < rows; i++) { HSSFRow row = sheet.getRow(i); if (null != row) { for (int j = 0; j < columns; j++) { HSSFCell cell = row.getCell(j); if (null == cell) { excelSheetInTDArray[i][j] = ""; } else { int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_BLANK) { excelSheetInTDArray[i][j] = ""; } else if (cellType == Cell.CELL_TYPE_BOOLEAN) { excelSheetInTDArray[i][j] = Boolean.toString(cell.getBooleanCellValue()); } else if (cellType == Cell.CELL_TYPE_ERROR) { excelSheetInTDArray[i][j] = ""; } else if (cellType == Cell.CELL_TYPE_FORMULA) { excelSheetInTDArray[i][j] = cell.getCellFormula(); } else if (cellType == Cell.CELL_TYPE_NUMERIC) { excelSheetInTDArray[i][j] = Double.toString(cell.getNumericCellValue()); } else if (cellType == Cell.CELL_TYPE_STRING) { excelSheetInTDArray[i][j] = cell.getRichStringCellValue().getString(); } } } } } sheetList.add(excelSheetInTDArray); } return sheetList; }
From source file:net.sourceforge.jaulp.export.excel.poi.ExportExcelUtils.java
License:Apache License
/** * Exportiert die bergebene excel-Datei in eine geschachtelte Liste mit Listen von sheets und * Listen von den Zeilen der sheets von der excel-Datei. * * @param excelSheet/*www. java2 s .com*/ * Die excel-Datei. * @return Gibt eine Liste mit Listen von den sheets in der excel-Datei zurck. Die Listen mit * den sheets beinhalten weitere Listen mit String die jeweils eine Zeile * reprsentieren. * @throws IOException * Fals ein Fehler beim Lesen aufgetreten ist. * @throws FileNotFoundException * Fals die excel-Datei nicht gefunden wurde. */ public static List<List<List<String>>> exportWorkbookAsStringList(File excelSheet) throws IOException, FileNotFoundException { POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelSheet)); HSSFWorkbook wb = new HSSFWorkbook(fs); int numberOfSheets = wb.getNumberOfSheets(); List<List<List<String>>> sl = new ArrayList<>(); for (int sheetNumber = 0; sheetNumber < numberOfSheets; sheetNumber++) { HSSFSheet sheet = null; sheet = wb.getSheetAt(sheetNumber); int rows = sheet.getLastRowNum(); int columns = sheet.getRow(0).getLastCellNum(); List<List<String>> excelSheetList = new ArrayList<>(); for (int i = 0; i < rows; i++) { HSSFRow row = sheet.getRow(i); if (null != row) { List<String> reihe = new ArrayList<>(); for (int j = 0; j < columns; j++) { HSSFCell cell = row.getCell(j); if (null == cell) { reihe.add(""); } else { int cellType = cell.getCellType(); if (cellType == Cell.CELL_TYPE_BLANK) { reihe.add(""); } else if (cellType == Cell.CELL_TYPE_BOOLEAN) { reihe.add(Boolean.toString(cell.getBooleanCellValue())); } else if (cellType == Cell.CELL_TYPE_ERROR) { reihe.add(""); } else if (cellType == Cell.CELL_TYPE_FORMULA) { reihe.add(cell.getCellFormula()); } else if (cellType == Cell.CELL_TYPE_NUMERIC) { reihe.add(Double.toString(cell.getNumericCellValue())); } else if (cellType == Cell.CELL_TYPE_STRING) { reihe.add(cell.getRichStringCellValue().getString()); } } } excelSheetList.add(reihe); } } sl.add(excelSheetList); } return sl; }