List of usage examples for org.apache.poi.xssf.usermodel XSSFCell getCellType
@Override
public CellType getCellType()
From source file:FenetrePrincipal.java
private static String cellToString(XSSFCell cell) { // TODO Auto-generated method stub int type;/*from w w w . j a v a 2s .co m*/ Object result; if (cell == null) return (String) (result = ""); type = cell.getCellType(); switch (type) { case 0: result = cell.getNumericCellValue(); break; case 1: result = cell.getStringCellValue(); break; default: throw new RuntimeException("There are no support for this type of cell "); } return result.toString(); }
From source file:CDatos.Excel.java
/** * Lee una hoja de un excel y devuelve una matriz con los datos * @return Matriz con los datos del excel *//*from w w w . j av a 2s . c om*/ public ArrayList getDatosHoja() { ArrayList<ArrayList> filas = new ArrayList(); XSSFSheet sheet = workbook.getSheetAt(hoja); int numColumnas = -1; // Recorremos fila a fila for (int r = 0; r <= sheet.getLastRowNum(); r++) { ArrayList<String> celdas = new ArrayList(); XSSFRow row = sheet.getRow(r); if (row == null) break; else { // En la primera fila se leen las cabeceras, por lo que aprovechamos para // guardar el nmero de columnas porque cuando una fila tiene celdas vacas el tamao // de la lista es menor if (numColumnas == -1) numColumnas = row.getLastCellNum(); // Recorremos celda a celda for (int c = 0; c < numColumnas; c++) { XSSFCell cell = row.getCell(c); String cellValue = ""; if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd"); //cellValue = cell.getDateCellValue().toString(); cellValue = formateador.format(cell.getDateCellValue()); } else { cellValue = (Integer.toString((int) cell.getNumericCellValue())); } break; case Cell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; } } celdas.add(cellValue); } filas.add(celdas); } } return filas; }
From source file:com.appdynamics.jrbronet.projectplan.ExcelManager.java
private static void copyRow(XSSFWorkbook workbook, XSSFSheet sourceWorksheet, int sourceRowNum, XSSFSheet destinationWorksheet, int destinationRowNum) { // Get the source / new row XSSFRow newRow = destinationWorksheet.getRow(destinationRowNum); XSSFRow sourceRow = sourceWorksheet.getRow(sourceRowNum); // If the row exist in destination, push down all rows by 1 else create a new row if (newRow != null) { destinationWorksheet.shiftRows(destinationRowNum, destinationWorksheet.getLastRowNum(), 1); } else {//from w w w. j a va 2 s .com newRow = destinationWorksheet.createRow(destinationRowNum); } // Loop through source columns to add to new row for (int i = 0; i < sourceRow.getLastCellNum(); i++) { // Grab a copy of the old/new cell XSSFCell oldCell = sourceRow.getCell(i); XSSFCell newCell = newRow.createCell(i); // If the old cell is null jump to next cell if (oldCell == null) { newCell = null; continue; } // Copy style from old cell and apply to new cell XSSFCellStyle newCellStyle = workbook.createCellStyle(); newCellStyle.cloneStyleFrom(oldCell.getCellStyle()); ; newCell.setCellStyle(newCellStyle); // If there is a cell comment, copy if (oldCell.getCellComment() != null) { newCell.setCellComment(oldCell.getCellComment()); } // If there is a cell hyperlink, copy if (oldCell.getHyperlink() != null) { newCell.setHyperlink(oldCell.getHyperlink()); } // Set the cell data type newCell.setCellType(oldCell.getCellType()); //newCell.setCellValue(oldCell.getRawValue()); //newCell.setCellType(oldCell.getCellType()); // Set the cell data value switch (oldCell.getCellType()) { case Cell.CELL_TYPE_BLANK: newCell.setCellValue(oldCell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: newCell.setCellValue(oldCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: newCell.setCellErrorValue(oldCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: newCell.setCellFormula(oldCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: newCell.setCellValue(oldCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: newCell.setCellValue(oldCell.getRichStringCellValue()); break; } } // If there are are any merged regions in the source row, copy to new row /* for (int i = 0; i < worksheet.getNumMergedRegions(); i++) { CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i); if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) { CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(), (newRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow() )), cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn()); worksheet.addMergedRegion(newCellRangeAddress); } } */ }
From source file:com.centurylink.mdw.drools.Excel2007Parser.java
License:Apache License
@SuppressWarnings("deprecation") private void processSheet(XSSFSheet sheet, List<? extends DataListener> listeners) { int mergedRegionCount = sheet.getNumMergedRegions(); CellRangeAddress[] mergedRanges = new CellRangeAddress[mergedRegionCount]; for (int i = 0; i < mergedRegionCount; i++) { mergedRanges[i] = sheet.getMergedRegion(i); }/*from www.j a va2s . c o m*/ for (int i = 0; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); if (row != null) { newRow(listeners, i, row.getLastCellNum()); for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) { XSSFCell cell = row.getCell(cellNum); if (cell != null) { CellRangeAddress merged = getRangeIfMerged(cell, mergedRanges); if (merged != null) { XSSFRow topRow = sheet.getRow(merged.getFirstRow()); XSSFCell topLeft = topRow.getCell(merged.getFirstColumn()); String cellValue = topLeft.getCellType() == CellType.NUMERIC.getCode() ? String.valueOf(topLeft.getNumericCellValue()) : topLeft.getStringCellValue(); newCell(listeners, i, cellNum, cellValue, topLeft.getColumnIndex()); } else { String cellValue = null; if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cellValue = String.valueOf(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = String.valueOf(cell.getNumericCellValue()); else cellValue = cell.getStringCellValue(); newCell(listeners, i, cellNum, cellValue, DataListener.NON_MERGED); } } } } } finishSheet(listeners); }
From source file:com.centurylink.mdw.workflow.drools.Excel2007Parser.java
License:Apache License
private void processSheet(XSSFSheet sheet, List<? extends DataListener> listeners) { int mergedRegionCount = sheet.getNumMergedRegions(); CellRangeAddress[] mergedRanges = new CellRangeAddress[mergedRegionCount]; for (int i = 0; i < mergedRegionCount; i++) { mergedRanges[i] = sheet.getMergedRegion(i); }/*from w w w. j a v a 2s .c om*/ for (int i = 0; i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); if (row != null) { newRow(listeners, i, row.getLastCellNum()); for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) { XSSFCell cell = row.getCell(cellNum); if (cell != null) { CellRangeAddress merged = getRangeIfMerged(cell, mergedRanges); if (merged != null) { XSSFRow topRow = sheet.getRow(merged.getFirstRow()); XSSFCell topLeft = topRow.getCell(merged.getFirstColumn()); newCell(listeners, i, cellNum, topLeft.getStringCellValue(), topLeft.getColumnIndex()); } else { String cellValue = null; if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cellValue = String.valueOf(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = String.valueOf(cell.getNumericCellValue()); else cellValue = cell.getStringCellValue(); newCell(listeners, i, cellNum, cellValue, DataListener.NON_MERGED); } } } } } finishSheet(listeners); }
From source file:com.cn.util.ExcelImport.java
/** * ?2007excel//from w w w. j a v a 2 s .c o m * * @param file * @return */ private static List<List<Object>> read2007Excel(InputStream inputStream) throws IOException { List<List<Object>> dataList = new ArrayList<>(); XSSFWorkbook xwb = new XSSFWorkbook(inputStream); XSSFSheet sheet = xwb.getSheetAt(0); XSSFRow row = null; XSSFCell cell = null; Object val = null; DecimalFormat df = new DecimalFormat("0");// ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ? for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); if (row == null) { continue; } List<Object> objList = new ArrayList<>(); for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { val = null; objList.add(val); continue; } switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: val = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: if ("@".equals(cell.getCellStyle().getDataFormatString())) { val = df.format(cell.getNumericCellValue()); } else if ("General".equals(cell.getCellStyle().getDataFormatString())) { val = df.format(cell.getNumericCellValue()); } else { val = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())); } break; case XSSFCell.CELL_TYPE_BOOLEAN: val = cell.getBooleanCellValue(); break; case XSSFCell.CELL_TYPE_BLANK: val = ""; break; default: val = cell.toString(); break; } objList.add(val); } dataList.add(objList); } return dataList; }
From source file:com.esd.cs.common.XExcelSheetParser.java
License:Open Source License
private Object getCellString(XSSFCell cell) { // TODO Auto-generated method stub Object result = null;/* w w w . j a v a2s . c om*/ if (cell != null) { // ?Numeric:0,String:1,Formula:2,Blank:3,Boolean:4,Error:5 int cellType = cell.getCellType(); switch (cellType) { case HSSFCell.CELL_TYPE_STRING: result = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_NUMERIC: result = cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_FORMULA: result = cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case HSSFCell.CELL_TYPE_BLANK: result = null; break; case HSSFCell.CELL_TYPE_ERROR: result = null; break; default: break; } } return result; }
From source file:com.excel.javafx.frames.MainFrame.java
public static boolean compareTwoCells(XSSFCell cell1, XSSFCell cell2) { if ((cell1 == null) && (cell2 == null)) { return true; } else if ((cell1 == null) || (cell2 == null)) { return false; }//from w w w. j av a 2s .co m boolean equalCells = false; int type1 = cell1.getCellType(); int type2 = cell2.getCellType(); if (type1 == type2) { if (cell1.getCellStyle().equals(cell2.getCellStyle())) { // Compare cells based on its type switch (cell1.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: if (cell1.getCellFormula().equals(cell2.getCellFormula())) { equalCells = true; } break; case HSSFCell.CELL_TYPE_NUMERIC: if (cell1.getNumericCellValue() == cell2.getNumericCellValue()) { equalCells = true; } break; case HSSFCell.CELL_TYPE_STRING: if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) { equalCells = true; } break; case HSSFCell.CELL_TYPE_BLANK: if (cell2.getCellType() == HSSFCell.CELL_TYPE_BLANK) { equalCells = true; } break; case HSSFCell.CELL_TYPE_BOOLEAN: if (cell1.getBooleanCellValue() == cell2.getBooleanCellValue()) { equalCells = true; } break; case HSSFCell.CELL_TYPE_ERROR: if (cell1.getErrorCellValue() == cell2.getErrorCellValue()) { equalCells = true; } break; default: if (cell1.getStringCellValue().equals(cell2.getStringCellValue())) { equalCells = true; } break; } } else { return false; } } else { return false; } return equalCells; }
From source file:com.jitendrasinghnz.excelreadutility.ExcelReadStringArrayXSLX.java
License:Open Source License
public String convertCellToString(XSSFCell cell) { int type;// ww w .j a va 2 s . c o m if (cell == null) { type = Cell.CELL_TYPE_BLANK; } else { type = cell.getCellType(); } Object result; switch (type) { case Cell.CELL_TYPE_STRING: result = cell.getStringCellValue(); break; case Cell.CELL_TYPE_NUMERIC: result = cell.getNumericCellValue(); break; case Cell.CELL_TYPE_BLANK: result = ""; break; case Cell.CELL_TYPE_BOOLEAN: result = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_ERROR: result = cell.getErrorCellValue(); break; case Cell.CELL_TYPE_FORMULA: result = cell.getCellFormula(); break; default: throw new RuntimeException("There are no support fot the cell type"); } return result.toString(); }
From source file:com.krawler.spring.importFunctionality.ImportUtil.java
License:Open Source License
/** * Generate the preview of the xls grid/*from w w w .ja v a 2 s. c o m*/ * @param filename * @param sheetNo * @return * @throws FileNotFoundException * @throws IOException * @throws JSONException */ public static JSONObject parseXLSX(String filename, int sheetNo) throws FileNotFoundException, IOException, JSONException { JSONObject jobj = new JSONObject(); FileInputStream fs = new FileInputStream(filename); XSSFWorkbook wb = new XSSFWorkbook(fs); XSSFSheet sheet = wb.getSheetAt(sheetNo); //DateFormat sdf = new SimpleDateFormat(df); int startRow = 0; int maxRow = sheet.getLastRowNum(); int maxCol = 0; int noOfRowsDisplayforSample = 20; if (noOfRowsDisplayforSample > sheet.getLastRowNum()) { noOfRowsDisplayforSample = sheet.getLastRowNum(); } JSONArray jArr = new JSONArray(); try { for (int i = 0; i <= noOfRowsDisplayforSample; i++) { XSSFRow row = sheet.getRow(i); JSONObject obj = new JSONObject(); JSONObject jtemp1 = new JSONObject(); if (row == null) { continue; } if (i == 0) { maxCol = row.getLastCellNum(); } for (int cellcount = 0; cellcount < maxCol; cellcount++) { XSSFCell cell = row.getCell(cellcount); CellReference cref = new CellReference(i, cellcount); String colHeader = cref.getCellRefParts()[2]; String val = null; if (cell != null) { switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { val = Long.toString(cell.getDateCellValue().getTime()); } else { val = dfmt.format(cell.getNumericCellValue()); } break; case XSSFCell.CELL_TYPE_STRING: val = ImportUtil.cleanHTML(cell.getRichStringCellValue().getString()); break; } } if (i == 0) { // List of Headers (Consider first row as Headers) if (val != null) { jtemp1 = new JSONObject(); jtemp1.put("header", val == null ? "" : val); jtemp1.put("index", cellcount); jobj.append("Header", jtemp1); } } obj.put(colHeader, val); } // if(obj.length()>0){ //Don't show blank row in preview grid[SK] jArr.put(obj); // } } } catch (Exception ex) { Logger.getLogger(ImportHandler.class.getName()).log(Level.SEVERE, null, ex); } jobj.put("startrow", startRow); jobj.put("maxrow", maxRow); jobj.put("maxcol", maxCol); jobj.put("index", sheetNo); jobj.put("data", jArr); jobj.put("filename", filename); jobj.put("msg", "XLSX has been successfully uploaded"); jobj.put("lsuccess", true); jobj.put("valid", true); return jobj; }