List of usage examples for org.apache.poi.ss.usermodel Cell getRowIndex
int getRowIndex();
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private CPoint findPreviousPoint(Sheet sheet, String tag, int startRow) { for (int i = startRow; i > -1; i--) { Row row = sheet.getRow(i);/*from w w w.j a v a 2 s .com*/ if (null == row) continue; for (Cell cell : row) { String text = getFormatCellValue(cell); if (tag.equals(text)) return new CPoint(cell.getColumnIndex(), cell.getRowIndex()); } } return null; }
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private CPoint findNextPoint(Sheet sheet, String tag, int startRow) { for (int i = startRow; i <= sheet.getLastRowNum(); i++) { Row row = sheet.getRow(i);//w w w .j a va2 s . co m if (null == row) continue; for (Cell cell : row) { String text = getFormatCellValue(cell); if (tag.equals(text)) return new CPoint(cell.getColumnIndex(), cell.getRowIndex()); } } return null; }
From source file:se.sll.invoicedata.price.GeneratePriceList.java
License:Open Source License
private List<String> getServicePrice(Sheet sheet, int startRow, int serviceType) { FormulaEvaluator formulaEval = sheet.getWorkbook().getCreationHelper().createFormulaEvaluator(); List<String> priceList = new ArrayList<String>(); for (int i = startRow; i < sheet.getPhysicalNumberOfRows(); i++) { Cell cell = sheet.getRow(i).getCell(serviceType); if (cell != null) { switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: priceList.add(DECIMAL_FORMAT.format(cell.getNumericCellValue())); break; case Cell.CELL_TYPE_FORMULA: double d = formulaEval.evaluate(cell).getNumberValue(); priceList.add(DECIMAL_FORMAT.format(d)); break; case Cell.CELL_TYPE_BLANK: priceList.add(DECIMAL_FORMAT.format(0)); break; default: StringBuffer errorMsg = new StringBuffer("This type of cell is not handled by the program!"); errorMsg.append(" cell type:").append(cell.getCellType()); errorMsg.append(" cell row:").append(cell.getRowIndex()); errorMsg.append(" cell column:").append(cell.getColumnIndex()); errorMsg.append(" cell value:").append(cell.getStringCellValue()); throw new IllegalStateException(errorMsg.toString()); }//from ww w. j a v a2s. com } else { priceList.add(DECIMAL_FORMAT.format(0)); } } return priceList; }
From source file:se.softhouse.garden.oak.ExcelDecisionTableBuilder.java
License:Open Source License
private Number getNumericValue(int type, Cell cell) { switch (type) { case Cell.CELL_TYPE_NUMERIC: if (cell instanceof XSSFCell) { String raw = ((XSSFCell) cell).getRawValue(); return new BigDecimal(raw); }//from ww w. ja v a2 s . c om return cell.getNumericCellValue(); case Cell.CELL_TYPE_FORMULA: return getNumericValue(cell.getCachedFormulaResultType(), cell); case Cell.CELL_TYPE_STRING: { String raw = cell.getStringCellValue(); if (raw == null || raw.isEmpty() || !raw.matches("0-9")) { // We throw this instead of the plain NumberFormatException that would've been // thrown otherwise. throw new IllegalArgumentException("Invalid Numeric String Cell value [" + raw + "] in Sheet(" + cell.getSheet().getSheetName() + "). Row:Column[" + cell.getRowIndex() + 1 + ":" + (cell.getColumnIndex() + 1) + "]"); } return new BigDecimal(raw); } } return BigDecimal.ZERO; }
From source file:sqlitemanager.Excel2Dataset.java
public static List<DataTable> readExcel(String inPath, boolean hasIntColumns, int colsHasInt) { List<DataTable> out = new ArrayList(); try {// w w w . j ava2 s . co m // Create a work book reference Workbook workbook = null; if (inPath.endsWith(".xls")) { workbook = new HSSFWorkbook(new FileInputStream(inPath)); } else if (inPath.endsWith(".xlsx")) { workbook = new XSSFWorkbook(new FileInputStream(inPath)); } else { System.err.println("No XLS or XLSX file found!"); return out; } //Create a count of the sheets in the file short sheetsCount = (short) workbook.getNumberOfSheets(); //create a reference of sheet, cell, first head, last head, head name, //sheet name, row count and row content Sheet sheet; Row row; Cell cell; int firstIndex = Integer.MIN_VALUE; int lastIndex = Integer.MAX_VALUE; String[] headName; fieldType[] fieldTypes; String sheetName; int rowCount; Object cellValue; for (int i = 0; i < sheetsCount; i++) { sheetName = workbook.getSheetName(i); try { sheet = workbook.getSheetAt(i); rowCount = sheet.getLastRowNum() + 1; if (rowCount < 1) { break; } // row = sheet.getRow(0); // for (int j = 0; j < rowCount; j++) { // row = sheet.getRow(j); // if (firstIndex < row.getFirstCellNum()) { // firstIndex = row.getFirstCellNum(); // } // if (lastIndex > row.getLastCellNum()) { // lastIndex = row.getLastCellNum(); // } // } row = sheet.getRow(0); // Head row firstIndex = row.getFirstCellNum(); lastIndex = row.getLastCellNum(); headName = new String[lastIndex]; fieldTypes = new fieldType[lastIndex]; List<String> names = new ArrayList(); for (int index = firstIndex; index < lastIndex; index++) { String name = row.getCell(index).toString(); if (names.contains(name)) { JOptionPane.showMessageDialog(null, String.format("Field \"%s\" duplicated!", name), "Notice", JOptionPane.ERROR_MESSAGE); return null; } else { names.add(name); } headName[index] = name; fieldTypes[index] = fieldType.Double; } // Detect field types for (int k = 1; k < rowCount; k++) { row = sheet.getRow(k); if (row == null) { break; } for (int index = firstIndex; index < lastIndex; index++) { if (fieldTypes[index] != fieldType.String) { if (row.getCell(index) != null) { fieldTypes[index] = fieldType .getType(getCellType(row.getCell(index).getCellType())); } else { fieldTypes[index] = fieldType.String; } } } } DataTable tempTable = new DataTable(sheetName); for (int index = firstIndex; index < lastIndex; index++) { tempTable.addField(headName[index], fieldTypes[index]); } for (int k = 1; k < rowCount; k++) { row = sheet.getRow(k); if (row == null) { break; } tempTable.addRecord(); for (int index = firstIndex; index < lastIndex; index++) { cell = row.getCell(index); if (fieldTypes[index] == fieldType.Double) { try { cellValue = cell.getNumericCellValue(); } catch (Exception e) { System.err.println(String.format("Error reading Sheet: %s, Row: %d, Column: %d", cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex())); cellValue = cell.getStringCellValue().trim(); } } else if (fieldTypes[index] == fieldType.Integer) { try { cellValue = (int) cell.getNumericCellValue(); } catch (Exception e) { System.err.println(String.format("Error reading Sheet: %s, Row: %d, Column: %d", cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex())); cellValue = cell.getStringCellValue().trim(); } } else { if (cell == null) { cellValue = ""; } else { try { try { cellValue = cell.getNumericCellValue(); } catch (Exception e) { cellValue = cell.getStringCellValue().trim(); } } catch (Exception e) { System.err.println( String.format("Error reading Sheet: %s, Row: %d, Column: %d", cell.getSheet().getSheetName(), cell.getRowIndex(), cell.getColumnIndex())); cellValue = cell.getNumericCellValue(); } } } tempTable.getField(index).set(tempTable.getRecordCount() - 1, cellValue); } } if (hasIntColumns) { DataTable table = new DataTable(tempTable.getName()); List<Integer> updateFields = new ArrayList(); if (colsHasInt < 1) { // 0 or negative means check all columns colsHasInt = tempTable.getRecordCount(); } int cols4Check = Math.min(colsHasInt, tempTable.getRecordCount()); for (int j = 0; j < cols4Check; j++) { Field f = tempTable.getField(j); if (f.getType() != fieldType.Double) { continue; } boolean isIntColumn = true; for (int recNum = 0; recNum < tempTable.getRecordCount(); recNum++) { double value = Double.valueOf(f.get(recNum).toString()); double checkValue = Double.valueOf(String.valueOf((int) value)); if (value != checkValue) { isIntColumn = false; break; } } if (isIntColumn) { updateFields.add(j); } } for (int j = 0; j < tempTable.getFieldCount(); j++) { fieldType type = tempTable.getField(j).getType(); if (updateFields.contains(j)) { type = fieldType.Integer; } table.addField(tempTable.getField(j).getName(), type); } for (int recNum = 0; recNum < tempTable.getRecordCount(); recNum++) { table.addRecord(); for (int col = 0; col < tempTable.getFieldCount(); col++) { Object rowItem; if (updateFields.contains(col)) { Double value = (double) tempTable.getRecord(recNum).get(col); rowItem = value.intValue(); } else { rowItem = tempTable.getRecord(recNum).get(col); } table.getField(col).set(table.getRecordCount() - 1, rowItem); } } out.add(table); } else { out.add(tempTable); } } catch (Exception e) { Logger.getLogger(Excel2Dataset.class.getName()).log(Level.SEVERE, null, e); JOptionPane.showMessageDialog(null, String.format("Loading sheet %s error!", sheetName), "Notice", JOptionPane.ERROR_MESSAGE); } } } catch (Exception ex) { Logger.getLogger(Excel2Dataset.class.getName()).log(Level.SEVERE, null, ex); } return out; }
From source file:step.datapool.excel.ExcelFunctions.java
License:Open Source License
private static int evaluateFormulaCell(Cell cell, FormulaEvaluator evaluator) { int typ = -1; try {//from w w w . java 2s.c om typ = evaluator.evaluateFormulaCell(cell); } catch (RuntimeException e) { String cellRef = CellReference.convertNumToColString(cell.getColumnIndex()) + (cell.getRowIndex() + 1); String errMsg = "Error while evaluating cell " + cellRef + " from sheet " + cell.getSheet().getSheetName() + ": " + e.getMessage(); throw new RuntimeException(errMsg, e); } return typ; }
From source file:testpoi.GenerateDailyExcel.java
License:Open Source License
private static void makeEntry(Department deptt) { //create new row in xlsx to be generated Row newRow = sheetNew.createRow(rowCnt++); //Create a new cell in current row Cell newCell = newRow.createCell(0); //Set value to the department's name newCell.setCellValue(deptt.name);//from www . j a v a2s . c om double random = Math.random(); Row row = null; if (deptt.name.equals("Gynaecology")) { //Pick a row from female sheet randomly (Female sheet should have all reproducible ages) int rowNum = (int) (random * sheetFemale.getPhysicalNumberOfRows()); row = sheetFemale.getRow(rowNum); } else if (deptt.name.equals("Paediatrics")) { //Pick a row from children sheet randomly (Children sheet should have all ages under 13) int rowNum = (int) (random * sheetChildren.getPhysicalNumberOfRows()); row = sheetChildren.getRow(rowNum); } else { //Pick a row from all sheet randomly int rowNum = (int) (random * sheetAll.getPhysicalNumberOfRows()); row = sheetAll.getRow(rowNum); } assert (row != null); //read and write fetched row Iterator<Cell> cellIterator = row.cellIterator(); int newCellCnt = 1; while (cellIterator.hasNext()) { //May we write all cells as strings? Cell cell = cellIterator.next(); String cellValue = null; try { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = cell.getNumericCellValue() + ""; else cellValue = cell.getStringCellValue(); newCell = newRow.createCell(newCellCnt++); newCell.setCellValue(cellValue); // System.out.print (cellValue+"("+cell.getColumnIndex()+")\t"); } catch (Exception e) { System.out.println("Could not write from cell (value:" + cellValue + // ", column:"+cell.getSheet().getWorkbook().+ ", sheet:" + cell.getSheet().getSheetName() + ", row:" + cell.getRowIndex() + ", column:" + cell.getColumnIndex() + ")"); e.printStackTrace(); } } System.out.println(); //delete row read if (row.getSheet() == sheetFemale) sheetFemale.removeRow(row); else if (row.getSheet() == sheetChildren) sheetChildren.removeRow(row); else sheetAll.removeRow(row); }
From source file:testpoi.GenerateDailyExcelPickingRowsSequentially.java
License:Open Source License
private static void makeEntry(Department deptt) { //create new row in xlsx to be generated Row newRow = sheetNew.createRow(rowCnt++); //Create a new cell in current row Cell newCell = newRow.createCell(0); //Set value to the department's name newCell.setCellValue(deptt.name);/*from w w w . j a v a 2 s .c o m*/ Row row = null; if (deptt.name.equals("Obs & Gynae") && femaleRowNum < sheetFemale.getPhysicalNumberOfRows()) { // //Pick a row from female sheet randomly (Female sheet should have all reproducible ages) // int rowNum = (int)(random*sheetFemale.getPhysicalNumberOfRows()); row = sheetFemale.getRow(femaleRowNum++); System.out.println("Sheet:Female, row: " + row.getRowNum()); } else if (deptt.name.equals("Paediatrics") && childRowNum < sheetChildren.getPhysicalNumberOfRows()) { row = sheetChildren.getRow(childRowNum++); System.out.println("Sheet:Children, row: " + row.getRowNum()); } else //if (allRowNum<sheetAll.getPhysicalNumberOfRows()) { row = sheetAll.getRow(allRowNum++); System.out.println("Sheet:All, row: " + row.getRowNum()); } assert row != null; //read and write fetched row Iterator<Cell> cellIterator = row.cellIterator(); int newCellCnt = 1; while (cellIterator.hasNext()) { //May we write all cells as strings? Cell cell = cellIterator.next(); String cellValue = null; try { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = (int) (cell.getNumericCellValue()) + ""; else cellValue = cell.getStringCellValue(); newCell = newRow.createCell(newCellCnt++); newCell.setCellValue(cellValue); } catch (Exception e) { System.out.println("Could not write from cell (value:" + cellValue + // ", column:"+cell.getSheet().getWorkbook().+ ", sheet:" + cell.getSheet().getSheetName() + ", row:" + cell.getRowIndex() + ", column:" + cell.getColumnIndex() + ")"); e.printStackTrace(); } } System.out.println(); // //delete row read // if (row.getSheet()==sheetFemale) // sheetFemale.removeRow(row); // else if (row.getSheet()==sheetChildren) // sheetChildren.removeRow(row); // else // sheetAll.removeRow(row); }
From source file:testpoi.Department.java
License:Open Source License
private static void makeEntry(Department deptt) { //create new row in xlsx to be generated Row newRow = sheetNew.createRow(rowCnt++); //Create a new cell in current row Cell newCell = newRow.createCell(0); //Set value to the department's name newCell.setCellValue(deptt.name);/*from w w w .j a v a2 s . co m*/ newCell = newRow.createCell(1); newCell.setCellValue(deptt.isNew ? "New" : "Old"); newCell = newRow.createCell(2); newCell.setCellValue(crNo++); Row row = null; if (deptt.name.equals("Obs & Gynae") && femaleRowNum < sheetFemale.getPhysicalNumberOfRows()) { // //Pick a row from female sheet randomly (Female sheet should have all reproducible ages) // int rowNum = (int)(random*sheetFemale.getPhysicalNumberOfRows()); row = sheetFemale.getRow(femaleRowNum++); System.out.println("Sheet:Female, row: " + row.getRowNum()); } else if (deptt.name.equals("Paediatrics") && childRowNum < sheetChildren.getPhysicalNumberOfRows()) { row = sheetChildren.getRow(childRowNum++); System.out.println("Sheet:Children, row: " + row.getRowNum()); } else //if (allRowNum<sheetAll.getPhysicalNumberOfRows()) { row = sheetAll.getRow(allRowNum++); System.out.println("Sheet:All, row: " + row.getRowNum()); } assert row != null; //read and write fetched row Iterator<Cell> cellIterator = row.cellIterator(); int newCellCnt = 3; while (cellIterator.hasNext()) { //May we write all cells as strings? Cell cell = cellIterator.next(); String cellValue = null; try { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = (int) (cell.getNumericCellValue()) + ""; else cellValue = cell.getStringCellValue(); newCell = newRow.createCell(newCellCnt++); newCell.setCellValue(cellValue); } catch (Exception e) { System.out.println("Could not write from cell (value:" + cellValue + // ", column:"+cell.getSheet().getWorkbook().+ ", sheet:" + cell.getSheet().getSheetName() + ", row:" + cell.getRowIndex() + ", column:" + cell.getColumnIndex() + ")"); e.printStackTrace(); } } System.out.println(); // //delete row read // if (row.getSheet()==sheetFemale) // sheetFemale.removeRow(row); // else if (row.getSheet()==sheetChildren) // sheetChildren.removeRow(row); // else // sheetAll.removeRow(row); }
From source file:testpoi.OldDepttSheet.java
License:Open Source License
private static void makeEntry(Department deptt) { //create new row in xlsx to be generated Row newRow = sheetNew.createRow(rowCnt++); //Create a new cell in current row Cell newCell = newRow.createCell(0); //Set value to the department's name newCell.setCellValue(deptt.name);// w ww . j a v a 2 s. co m newCell = newRow.createCell(1); newCell.setCellValue(deptt.isNew ? "New" : "Old"); if (deptt.isNew) { newCell = newRow.createCell(2); newCell.setCellValue(crNo++); Row row = null; if (deptt.name.equals("Obs & Gynae")) { // //Pick a row from female sheet randomly (Female sheet should have all reproducible ages) // int rowNum = (int)(random*sheetFemale.getPhysicalNumberOfRows()); if (femaleRowNum < sheetFemale.getPhysicalNumberOfRows()) { row = sheetFemale.getRow(femaleRowNum++); System.out.println("Sheet:Female, row: " + row.getRowNum()); } else { System.err.println("Female entries exhausted!"); } } else if (deptt.name.equals("Paediatrics")) { if (childRowNum < sheetChildren.getPhysicalNumberOfRows()) { row = sheetChildren.getRow(childRowNum++); System.out.println("Sheet:Children, row: " + row.getRowNum()); } else { System.err.println("Child entries exhausted!"); } } else { if (allRowNum < sheetAll.getPhysicalNumberOfRows()) { row = sheetAll.getRow(allRowNum++); System.out.println("Sheet:All, row: " + row.getRowNum()); } else { System.err.println("All(General New) entries exhausted!"); } } if (row == null) { throw new IllegalArgumentException("New input Rows Exhausted"); } assert row != null; //read and write fetched row Iterator<Cell> cellIterator = row.cellIterator(); int newCellCnt = 3; while (cellIterator.hasNext()) { //May we write all cells as strings? Cell cell = cellIterator.next(); String cellValue = null; try { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = (int) (cell.getNumericCellValue()) + ""; else cellValue = cell.getStringCellValue(); newCell = newRow.createCell(newCellCnt++); newCell.setCellValue(cellValue); } catch (Exception e) { System.out.println("Could not write from cell (value:" + cellValue + // ", column:"+cell.getSheet().getWorkbook().+ ", sheet:" + cell.getSheet().getSheetName() + ", row:" + cell.getRowIndex() + ", column:" + cell.getColumnIndex() + ")"); e.printStackTrace(); } } } else //deptt is old { OldDepttSheet oldDepttSheetToUse = depttToOldSheetsMap.get(deptt.name); Row row = oldDepttSheetToUse.sheet.getRow(oldDepttSheetToUse.rowCnt++); if (row == null) { throw new IllegalArgumentException("Old Input Rows Exhausted in department " + deptt.name); } System.out.println("Sheet:" + deptt.name + ", row: " + row.getRowNum()); //Copy row from old sheet to newRow int newCellCnt = 2; Iterator<Cell> cellIterator = row.cellIterator(); //Skip columns Department and Patient Type cellIterator.next(); cellIterator.next(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); String cellValue = null; try { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cellValue = (int) (cell.getNumericCellValue()) + ""; else cellValue = cell.getStringCellValue(); newCell = newRow.createCell(newCellCnt++); newCell.setCellValue(cellValue); } catch (Exception e) { System.out.println("Could not write from old sheet cell (value:" + cellValue + ", sheet:" + cell.getSheet().getSheetName() + ", row:" + cell.getRowIndex() + ", column:" + cell.getColumnIndex() + ")"); e.printStackTrace(); } } } }