List of usage examples for org.apache.poi.ss.usermodel Sheet getLastRowNum
int getLastRowNum();
From source file:nu.mine.kino.projects.utils.POITest.java
License:Open Source License
public static void main(String[] args) { Workbook workbook = null;/*from w w w .j a v a 2s. c om*/ FileInputStream in = null; try { in = new FileInputStream(new java.io.File("project_management_tools.xls")); workbook = WorkbookFactory.create(in); Sheet sheet = workbook.getSheetAt(0); // int[] rowBreaks = sheet.getRowBreaks(); Row row = sheet.getRow(25); Cell cell = row.getCell(24); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; default: System.out.println("cellType=" + cell.getCellType()); break; } System.out.println("value: " + cell); System.out.println(sheet.getRow(46).getCell(4)); final int rowMin = sheet.getFirstRowNum(); System.out.println(rowMin); final int rowMax = sheet.getLastRowNum(); System.out.println(rowMax); } catch (InvalidFormatException e) { // TODO ?? catch u?bN e.printStackTrace(); } catch (FileNotFoundException e) { // TODO ?? catch u?bN e.printStackTrace(); } catch (IOException e) { // TODO ?? catch u?bN e.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { // TODO ?? catch u?bN e.printStackTrace(); } } }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetAddRow.java
License:Open Source License
public cfData execute(cfSession _session, List<cfData> parameters) throws cfmRunTimeException { Collections.reverse(parameters); cfSpreadSheetData spreadsheet = null; cfData data;// www .j a v a 2 s .c o m int rowNo = -1, column = 0; boolean bInsert = true; String delimitor = ","; /* * Collect up the parameters */ spreadsheet = (cfSpreadSheetData) parameters.get(0); data = parameters.get(1); rowNo = getIntParam(parameters, 2, 1) - 1; column = getIntParam(parameters, 3, 1) - 1; bInsert = getBooleanParam(parameters, 4, true); delimitor = getStringParam(parameters, 5, ","); /* * Validate parameters */ if (column < 0) throwException(_session, "column must be 1 or greater (" + column + ")"); if (rowNo < 0) throwException(_session, "row must be 1 or greater (" + rowNo + ")"); cfQueryResultData queryData = null; cfArrayData arrayData = null; String[] colData = null; int rowsToInsert; if (data.getDataType() == cfData.CFARRAYDATA) { arrayData = (cfArrayData) data; rowsToInsert = arrayData.size(); } else if (data.getDataType() == cfData.CFQUERYRESULTDATA) { queryData = (cfQueryResultData) data; rowsToInsert = queryData.getSize(); } else { List<String> elements = string.split(data.getString(), delimitor); colData = elements.toArray(new String[0]); rowsToInsert = 1; } Sheet sheet = spreadsheet.getActiveSheet(); for (int x = 0; x < rowsToInsert; x++) { // Create the row we need Row row = null; if (rowNo == -1) row = sheet.createRow(sheet.getLastRowNum() + 1); else if (!bInsert) { row = sheet.getRow(rowNo + x); if (row == null) throwException(_session, "the row you are looking for does not exist"); } else if (bInsert) { SheetUtility.insertRow(sheet, rowNo + x); row = sheet.getRow(rowNo + x); } // now set the data Cell cell; if (colData != null) { for (int c = 0; c < colData.length; c++) { if (bInsert) cell = row.createCell(column + c); else { cell = row.getCell(column + c); if (cell == null) cell = row.createCell(column + c); } // Set the data; trying to see if its a number try { cell.setCellValue(Double.valueOf(colData[c])); cell.setCellType(Cell.CELL_TYPE_NUMERIC); } catch (Exception e) { cell.setCellValue(colData[c]); cell.setCellType(Cell.CELL_TYPE_STRING); } } } else if (arrayData != null) { String acolData[] = arrayData.getData(new cfNumberData(x + 1)).getString().split(","); for (int c = 0; c < acolData.length; c++) { if (bInsert) cell = row.createCell(column + c); else { cell = row.getCell(column + c); if (cell == null) cell = row.createCell(column + c); } // Set the data; trying to see if its a number try { cell.setCellValue(Double.valueOf(acolData[c])); cell.setCellType(Cell.CELL_TYPE_NUMERIC); } catch (Exception e) { cell.setCellValue(acolData[c]); cell.setCellType(Cell.CELL_TYPE_STRING); } } } else if (queryData != null) { int columnCount = queryData.getNoColumns(); for (int c = 0; c < columnCount; c++) { if (bInsert) cell = row.createCell(column + c); else { cell = row.getCell(column + c); if (cell == null) cell = row.createCell(column + c); } cfData value = queryData.getCell(x + 1, c + 1); SheetUtility.setCell(cell, value); } } } return cfBooleanData.TRUE; }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.functions.SpreadsheetQueryRead.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { cfSpreadSheetData spreadsheet = (cfSpreadSheetData) getNamedParam(argStruct, "spreadsheet", null); if (spreadsheet == null) throwException(_session, "spreadsheet object not given"); //How many columns are we using int[] columnsToUse; String columns = getNamedStringParam(argStruct, "columns", null); if (columns != null) { int x = 0; List<Integer> numbers = tagUtils.getNumberListSorted(columns); columnsToUse = new int[numbers.size()]; Iterator<Integer> numbersIT = numbers.iterator(); while (numbersIT.hasNext()) columnsToUse[x++] = (numbersIT.next() - 1); } else {/* w ww . ja v a2s .c o m*/ int maxColumns = SheetUtility.getMaxColumn(spreadsheet.getActiveSheet()); columnsToUse = new int[maxColumns]; for (int x = 0; x < maxColumns; x++) columnsToUse[x] = x; } //Figure out the columns String columnLabels[] = null; int startRow = 0; String columnnames = getNamedStringParam(argStruct, "columnnames", null); int headerRow = getNamedIntParam(argStruct, "headerrow", -1); FormulaEvaluator evaluator = spreadsheet.getWorkBook().getCreationHelper().createFormulaEvaluator(); if (columnnames != null) { columnLabels = (columnnames.split("\\s*,\\s*")); if (columnLabels.length != columnsToUse.length) throwException(_session, "The COLUMNNAMES does not match the number of columns"); } else if (headerRow > -1) { headerRow = headerRow - 1; Row row = spreadsheet.getActiveSheet().getRow(headerRow); if (row == null) throwException(_session, "The HEADERROW does not exist"); columnLabels = new String[columnsToUse.length]; for (int c = 0; c < columnsToUse.length; c++) { Cell cell = row.getCell(columnsToUse[c]); if (cell == null) columnLabels[c] = ""; else columnLabels[c] = cell.toString(); } startRow = headerRow + 1; } else { columnLabels = new String[columnsToUse.length]; for (int x = 0; x < columnLabels.length; x++) columnLabels[x] = "Column " + (columnsToUse[x] + 1); } //Create the query cfQueryResultData queryData = new cfQueryResultData(columnLabels, "SpreadSheet"); List<Map<String, cfData>> vResults = new ArrayList<Map<String, cfData>>(); int sheetNo = getNamedIntParam(argStruct, "sheet", -1); if (sheetNo != -1) spreadsheet.setActiveSheet(sheetNo); Sheet sheet = spreadsheet.getActiveSheet(); Row row; Cell cell; cfData cfdata; String rowsSt = getNamedStringParam(argStruct, "rows", null); if (rowsSt != null) { List<Integer> rows = tagUtils.getNumberListSorted(rowsSt); Iterator<Integer> rowsIT = rows.iterator(); while (rowsIT.hasNext()) { int r = rowsIT.next() - 1; Map<String, cfData> hm = new FastMap<String, cfData>(); if ((row = sheet.getRow(r)) == null) continue; for (int c = 0; c < columnsToUse.length; c++) { cell = row.getCell(columnsToUse[c]); if (cell == null) cfdata = new cfStringData(""); else { if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cfdata = cfBooleanData.getcfBooleanData(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cfdata = new cfNumberData(cell.getNumericCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) cfdata = new cfStringData(""); else if (cell.getCellType() == Cell.CELL_TYPE_STRING) cfdata = new cfStringData(cell.getStringCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cfdata = cfBooleanData.getcfBooleanData(cellValue.getBooleanValue()); break; case Cell.CELL_TYPE_NUMERIC: cfdata = new cfNumberData(cellValue.getNumberValue()); break; case Cell.CELL_TYPE_STRING: cfdata = new cfStringData(cellValue.getStringValue()); break; default: cfdata = cfStringData.EMPTY_STRING; break; } } else cfdata = new cfStringData(""); } hm.put(columnLabels[c], cfdata); } vResults.add(hm); } } else { /* * Read __ALL__ the rows associated with this spreadsheet */ for (int r = startRow; r < sheet.getLastRowNum() + 1; r++) { Map<String, cfData> hm = new FastMap<String, cfData>(); if ((row = sheet.getRow(r)) == null) continue; for (int c = 0; c < columnsToUse.length; c++) { cell = row.getCell(columnsToUse[c]); if (cell == null) cfdata = new cfStringData(""); else { if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cfdata = cfBooleanData.getcfBooleanData(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cfdata = new cfNumberData(cell.getNumericCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) cfdata = new cfStringData(""); else if (cell.getCellType() == Cell.CELL_TYPE_STRING) cfdata = new cfStringData(cell.getStringCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { CellValue cellValue = evaluator.evaluate(cell); switch (cellValue.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: cfdata = cfBooleanData.getcfBooleanData(cellValue.getBooleanValue()); break; case Cell.CELL_TYPE_NUMERIC: cfdata = new cfNumberData(cellValue.getNumberValue()); break; case Cell.CELL_TYPE_STRING: cfdata = new cfStringData(cellValue.getStringValue()); break; default: cfdata = cfStringData.EMPTY_STRING; break; } } else cfdata = new cfStringData(""); } hm.put(columnLabels[c], cfdata); } vResults.add(hm); } } queryData.populateQuery(vResults); return queryData; }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SheetUtility.java
License:Open Source License
public static int getMaxColumn(Sheet sheet) { int maxColumn = 0; for (int r = 0; r < sheet.getLastRowNum() + 1; r++) { Row row = sheet.getRow(r);//from w w w . j a v a 2 s . c o m // if no row exists here; then nothing to do; next! if (row == null) continue; int lastColumn = row.getLastCellNum(); if (lastColumn > maxColumn) maxColumn = lastColumn; } return maxColumn; }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SheetUtility.java
License:Open Source License
/** * Given a sheet, this method inserts a row to a sheet and moves * all the rows to the bottom down one/*from ww w . j a v a2s .co m*/ * * Note, this method will not update any formula references. * * @param sheet * @param rowPosition */ public static void insertRow(Sheet sheet, int rowPosition) { //Row Position maybe beyond the last if (rowPosition > sheet.getLastRowNum()) { sheet.createRow(rowPosition); return; } //Create a new Row at the end sheet.createRow(sheet.getLastRowNum() + 1); Row row; for (int r = sheet.getLastRowNum(); r > rowPosition; r--) { row = sheet.getRow(r); if (row == null) row = sheet.createRow(r); //Clear the row for (int c = 0; c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null) row.removeCell(cell); } //Move the row Row previousRow = sheet.getRow(r - 1); if (previousRow == null) { sheet.createRow(r - 1); continue; } for (int c = 0; c < previousRow.getLastCellNum(); c++) { Cell cell = previousRow.getCell(c); if (cell != null) { Cell newCell = row.createCell(c, cell.getCellType()); cloneCell(newCell, cell); } } } //Clear the newly inserted row row = sheet.getRow(rowPosition); for (int c = 0; c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null) row.removeCell(cell); } }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SheetUtility.java
License:Open Source License
/** * Given a sheet, this method deletes a column from a sheet and moves * all the columns to the right of it to the left one cell. * //from w w w. jav a2s . com * Note, this method will not update any formula references. * * @param sheet * @param column */ public static void deleteColumn(Sheet sheet, int columnToDelete) { int maxColumn = 0; for (int r = 0; r < sheet.getLastRowNum() + 1; r++) { Row row = sheet.getRow(r); // if no row exists here; then nothing to do; next! if (row == null) continue; int lastColumn = row.getLastCellNum(); if (lastColumn > maxColumn) maxColumn = lastColumn; // if the row doesn't have this many columns then we are good; next! if (lastColumn < columnToDelete) continue; for (int x = columnToDelete + 1; x < lastColumn + 1; x++) { Cell oldCell = row.getCell(x - 1); if (oldCell != null) row.removeCell(oldCell); Cell nextCell = row.getCell(x); if (nextCell != null) { Cell newCell = row.createCell(x - 1, nextCell.getCellType()); cloneCell(newCell, nextCell); } } } // Adjust the column widths for (int c = 0; c < maxColumn; c++) { sheet.setColumnWidth(c, sheet.getColumnWidth(c + 1)); } }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.tags.cfSpreadSheetRead.java
License:Open Source License
private cfQueryResultData getQueryData(cfSpreadSheet tag, cfSession _Session) throws cfmRunTimeException { cfSpreadSheetData spreadsheet = getSpreadSheetData(tag, _Session); //How many columns are we using int[] columnsToUse; if (tag.containsAttribute("COLUMNS")) { int x = 0; List<Integer> numbers = tagUtils.getNumberListSorted(tag.getDynamic(_Session, "COLUMNS").getString()); columnsToUse = new int[numbers.size()]; Iterator<Integer> numbersIT = numbers.iterator(); while (numbersIT.hasNext()) columnsToUse[x++] = (numbersIT.next() - 1); } else {//from w w w. ja v a 2 s . co m int maxColumns = SheetUtility.getMaxColumn(spreadsheet.getActiveSheet()); columnsToUse = new int[maxColumns]; for (int x = 0; x < maxColumns; x++) columnsToUse[x] = x; } //Figure out the columns String columnLabels[] = null; int startRow = 0; if (tag.containsAttribute("COLUMNNAMES")) { columnLabels = tag.getDynamic(_Session, "COLUMNNAMES").getString().split(","); if (columnLabels.length != columnsToUse.length) throw tag.newRunTimeException("The COLUMNNAMES does not match the number of columns"); } else if (tag.containsAttribute("HEADERROW")) { int headerRow = tag.getDynamic(_Session, "HEADERROW").getInt() - 1; Row row = spreadsheet.getActiveSheet().getRow(headerRow); if (row == null) throw tag.newRunTimeException("The HEADERROW does not exist"); columnLabels = new String[columnsToUse.length]; for (int c = 0; c < columnsToUse.length; c++) { Cell cell = row.getCell(columnsToUse[c]); if (cell == null) columnLabels[c] = ""; else columnLabels[c] = cell.toString(); } startRow = headerRow + 1; } else { columnLabels = new String[columnsToUse.length]; for (int x = 0; x < columnLabels.length; x++) { columnLabels[x] = "Column " + (columnsToUse[x] + 1); } } //Create the query cfQueryResultData queryData = new cfQueryResultData(columnLabels, "SpreadSheet"); List<Map<String, cfData>> vResults = new ArrayList<Map<String, cfData>>(); Sheet sheet = spreadsheet.getActiveSheet(); Row row; Cell cell; cfData cfdata; if (tag.containsAttribute("ROWS")) { List<Integer> rows = tagUtils.getNumberListSorted(tag.getDynamic(_Session, "ROWS").getString()); Iterator<Integer> rowsIT = rows.iterator(); while (rowsIT.hasNext()) { int r = rowsIT.next() - 1; Map<String, cfData> hm = new FastMap<String, cfData>(); if ((row = sheet.getRow(r)) == null) continue; for (int c = 0; c < columnsToUse.length; c++) { cell = row.getCell(columnsToUse[c]); if (cell == null) cfdata = new cfStringData(""); else { if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cfdata = cfBooleanData.getcfBooleanData(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cfdata = new cfNumberData(cell.getNumericCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) cfdata = new cfStringData(""); else if (cell.getCellType() == Cell.CELL_TYPE_STRING) cfdata = new cfStringData(cell.getStringCellValue()); else cfdata = new cfStringData(""); } hm.put(columnLabels[c], cfdata); } vResults.add(hm); } } else { /* * Read __ALL__ the rows associated with this spreadsheet */ for (int r = startRow; r < sheet.getLastRowNum() + 1; r++) { Map<String, cfData> hm = new FastMap<String, cfData>(); if ((row = sheet.getRow(r)) == null) continue; for (int c = 0; c < columnsToUse.length; c++) { cell = row.getCell(columnsToUse[c]); if (cell == null) cfdata = new cfStringData(""); else { if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) cfdata = cfBooleanData.getcfBooleanData(cell.getBooleanCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) cfdata = new cfNumberData(cell.getNumericCellValue()); else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) cfdata = new cfStringData(""); else if (cell.getCellType() == Cell.CELL_TYPE_STRING) cfdata = new cfStringData(cell.getStringCellValue()); else cfdata = new cfStringData(""); } hm.put(columnLabels[c], cfdata); } vResults.add(hm); } } queryData.populateQuery(vResults); return queryData; }
From source file:org.alfresco.repo.web.scripts.person.UserCSVUploadPost.java
License:Open Source License
private void processSpreadsheetUpload(Workbook wb, List<Map<QName, String>> users) throws IOException { if (wb.getNumberOfSheets() > 1) { logger.info("Uploaded Excel file has " + wb.getNumberOfSheets() + " sheets, ignoring all except the first one"); }/*from w ww .jav a2 s. c o m*/ int firstRow = 0; Sheet s = wb.getSheetAt(0); DataFormatter df = new DataFormatter(); String[][] data = new String[s.getLastRowNum() + 1][]; // If there is a heading freezepane row, skip it PaneInformation pane = s.getPaneInformation(); if (pane != null && pane.isFreezePane() && pane.getHorizontalSplitTopRow() > 0) { firstRow = pane.getHorizontalSplitTopRow(); logger.debug("Skipping excel freeze header of " + firstRow + " rows"); } // Process each row in turn, getting columns up to our limit for (int row = firstRow; row <= s.getLastRowNum(); row++) { Row r = s.getRow(row); if (r != null) { String[] d = new String[COLUMNS.length]; for (int cn = 0; cn < COLUMNS.length; cn++) { Cell cell = r.getCell(cn); if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) { d[cn] = df.formatCellValue(cell); } } data[row] = d; } } // Handle the contents processSpreadsheetUpload(data, users); }
From source file:org.aludratest.testcase.data.impl.TestConfigInfoHelper.java
License:Apache License
private List<TestDataLoadInfo> parseTestInfos(Sheet sheet, int invocations, Method method, String excelFilePath) {/*from w w w . j a v a2 s . com*/ int configColumn = findConfigColumn(sheet, excelFilePath); int ignoreColumnIndex = findIgnoreColumnIndex(sheet, excelFilePath); int offset = getMethodOffset(method); List<TestDataLoadInfo> testInfos = new ArrayList<TestDataLoadInfo>(); for (int invocationNumber = offset + 1; invocationNumber <= sheet.getLastRowNum(); invocationNumber++) { Row infoRow = sheet.getRow(invocationNumber); Cell infoCell = (infoRow != null ? infoRow.getCell(configColumn) : null); String infoText = (infoCell != null ? infoCell.getStringCellValue() : null); if (invocationNumber - offset > invocations) { Exception e = new AutomationException("Configuration " + infoText + " has no data"); testInfos.add(new TestDataLoadInfo(e)); } else if (StringUtil.isEmpty(infoText)) { Exception e = new AutomationException("Data set #" + invocationNumber + " has no configuration"); testInfos.add(new TestDataLoadInfo(e)); } else { boolean ignored = isIgnored(method) || parseIgnoredCell(infoRow, ignoreColumnIndex); testInfos.add(new TestDataLoadInfo(infoText, ignored)); } LOGGER.debug("testConfiguration for invocation {} on method {} is: {}", new Object[] { invocationNumber, method, infoText }); } for (int i = sheet.getLastRowNum() + 1; i <= invocations; i++) { Exception e = new AutomationException("Test data without test config: #" + (i - 1)); testInfos.add(new TestDataLoadInfo(e)); } return testInfos; }
From source file:org.aludratest.util.ExcelUtil.java
License:Apache License
/** Inserts a column without cells into a sheet at the given index. * @param sheet the sheet in which to insert the column * @param insertionIndex the column index at which to insert the new column */ public static void insertEmptyColumn(Sheet sheet, int insertionIndex) { for (int i = sheet.getLastRowNum(); i >= 0; i--) { Row row = sheet.getRow(i);// w w w . java 2 s .co m short lastCellNum = row.getLastCellNum(); if (lastCellNum >= insertionIndex) { shiftCellsRight(row, insertionIndex, lastCellNum); } } }