List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet getRow
@Override public XSSFRow getRow(int rownum)
From source file:com.photon.phresco.framework.commons.FrameworkUtil.java
License:Apache License
public void addNew(String filePath, String testName, String cellValue[]) throws PhrescoException { try {/*from w ww. ja va 2s .c o m*/ //FileInputStream myInput = new FileInputStream(filePath); int numCol; int cellno = 0; CellStyle tryStyle[] = new CellStyle[20]; String sheetName = testName; //String cellValue[] = {"","",testName,success, fail,"","","",total,testCoverage,"","",""}; Iterator<Row> rowIterator; File testDir = new File(filePath); StringBuilder sb = new StringBuilder(filePath); if (testDir.isDirectory()) { FilenameFilter filter = new PhrescoFileFilter("", "xlsx"); File[] listFiles = testDir.listFiles(filter); if (listFiles.length != 0) { for (File file1 : listFiles) { if (file1.isFile()) { sb.append(File.separator); sb.append(file1.getName()); break; } } FileInputStream myInput = new FileInputStream(sb.toString()); OPCPackage opc = OPCPackage.open(myInput); XSSFWorkbook myWorkBook = new XSSFWorkbook(opc); XSSFSheet mySheet = myWorkBook.getSheetAt(0); rowIterator = mySheet.rowIterator(); numCol = 13; Row next; for (Cell cell : mySheet.getRow((mySheet.getLastRowNum()) - 2)) { tryStyle[cellno] = cell.getCellStyle(); cellno = cellno + 1; } do { int flag = 0; next = rowIterator.next(); if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index") && ((mySheet.getLastRowNum() - next.getRowNum()) < 3)) { for (Cell cell : next) { cell.setCellType(1); if (cell.getStringCellValue().equalsIgnoreCase("total")) { mySheet.shiftRows((mySheet.getLastRowNum() - 1), (mySheet.getPhysicalNumberOfRows() - 1), 1); flag = 1; } if (flag == 1) break; } if (flag == 1) break; } } while (rowIterator.hasNext()); Row r = null; if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) { r = mySheet.createRow(next.getRowNum() - 1); } else { r = mySheet.createRow(next.getRowNum() + 1); } for (int i = 0; i < numCol; i++) { Cell cell = r.createCell(i); cell.setCellValue(cellValue[i]); // used only when sheet is 'index' if (i == 2) sheetName = cellValue[i]; cell.setCellStyle(tryStyle[i]); } if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) { Sheet fromSheet = myWorkBook.getSheetAt((myWorkBook.getNumberOfSheets() - 1)); Sheet toSheet = myWorkBook.createSheet(sheetName); int i = 0; Iterator<Row> copyFrom = fromSheet.rowIterator(); Row fromRow, toRow; CellStyle newSheetStyle[] = new CellStyle[20]; Integer newSheetType[] = new Integer[100]; String newSheetValue[] = new String[100]; do { fromRow = copyFrom.next(); if (fromRow.getRowNum() == 24) { break; } toRow = toSheet.createRow(i); int numCell = 0; for (Cell cell : fromRow) { Cell newCell = toRow.createCell(numCell); cell.setCellType(1); newSheetStyle[numCell] = cell.getCellStyle(); newCell.setCellStyle(newSheetStyle[numCell]); newSheetType[numCell] = cell.getCellType(); newCell.setCellType(newSheetType[numCell]); if (fromRow.getCell(0).getStringCellValue().length() != 1 && fromRow.getCell(0).getStringCellValue().length() != 2 && fromRow.getCell(0).getStringCellValue().length() != 3) { newSheetValue[numCell] = cell.getStringCellValue(); newCell.setCellValue(newSheetValue[numCell]); } numCell = numCell + 1; } i = i + 1; } while (copyFrom.hasNext()); } // write to file FileOutputStream fileOut = new FileOutputStream(sb.toString()); myWorkBook.write(fileOut); myInput.close(); fileOut.close(); } else { FilenameFilter xlsFilter = new PhrescoFileFilter("", "xls"); File[] xlsListFiles = testDir.listFiles(xlsFilter); if (xlsListFiles.length != 0) { for (File file2 : xlsListFiles) { if (file2.isFile()) { sb.append(File.separator); sb.append(file2.getName()); break; } } FileInputStream myInput = new FileInputStream(sb.toString()); HSSFWorkbook myWorkBook = new HSSFWorkbook(myInput); HSSFSheet mySheet = myWorkBook.getSheetAt(0); rowIterator = mySheet.rowIterator(); numCol = 13; Row next; for (Cell cell : mySheet.getRow((mySheet.getLastRowNum()) - 2)) { tryStyle[cellno] = cell.getCellStyle(); cellno = cellno + 1; } do { int flag = 0; next = rowIterator.next(); if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index") && ((mySheet.getLastRowNum() - next.getRowNum()) < 3)) { for (Cell cell : next) { cell.setCellType(1); if (cell.getStringCellValue().equalsIgnoreCase("total")) { mySheet.shiftRows((mySheet.getLastRowNum() - 1), (mySheet.getPhysicalNumberOfRows() - 1), 1); flag = 1; } if (flag == 1) break; } if (flag == 1) break; } } while (rowIterator.hasNext()); Row r = null; if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) { r = mySheet.createRow(mySheet.getLastRowNum() - 2); } else { r = mySheet.createRow(next.getRowNum() + 1); } for (int i = 0; i < numCol; i++) { Cell cell = r.createCell(i); cell.setCellValue(cellValue[i]); // used only when sheet is 'index' if (i == 2) sheetName = cellValue[i]; cell.setCellStyle(tryStyle[i]); } if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) { Sheet fromSheet = myWorkBook.getSheetAt((myWorkBook.getNumberOfSheets() - 1)); Sheet toSheet = myWorkBook.createSheet(sheetName); int i = 0; Iterator<Row> copyFrom = fromSheet.rowIterator(); Row fromRow, toRow; CellStyle newSheetStyle[] = new CellStyle[20]; Integer newSheetType[] = new Integer[100]; String newSheetValue[] = new String[100]; do { fromRow = copyFrom.next(); if (fromRow.getRowNum() == 24) { break; } toRow = toSheet.createRow(i); int numCell = 0; for (Cell cell : fromRow) { Cell newCell = toRow.createCell(numCell); cell.setCellType(1); newSheetStyle[numCell] = cell.getCellStyle(); newCell.setCellStyle(newSheetStyle[numCell]); newSheetType[numCell] = cell.getCellType(); newCell.setCellType(newSheetType[numCell]); if (fromRow.getCell(0).getStringCellValue().length() != 1 && fromRow.getCell(0).getStringCellValue().length() != 2 && fromRow.getCell(0).getStringCellValue().length() != 3) { newSheetValue[numCell] = cell.getStringCellValue(); newCell.setCellValue(newSheetValue[numCell]); } numCell = numCell + 1; if (numCell == 15) { break; } } i = i + 1; } while (copyFrom.hasNext()); } // write to file FileOutputStream fileOut = new FileOutputStream(sb.toString()); myWorkBook.write(fileOut); myInput.close(); fileOut.close(); } else { FilenameFilter odsFilter = new PhrescoFileFilter("", "ods"); File[] odsListFiles = testDir.listFiles(odsFilter); for (File file1 : odsListFiles) { if (file1.isFile()) { sb.append(File.separator); sb.append(file1.getName()); break; } } File file = new File(sb.toString()); addTestSuiteToOds(file, cellValue); } } } } catch (Exception e) { // throw new PhrescoException(e); } }
From source file:com.photon.phresco.framework.commons.FrameworkUtil.java
License:Apache License
private void writeTestCasesToXLSX(String testSuiteName, String[] cellValue, String status, int numCol, int cellno, CellStyle[] tryStyle, StringBuilder sb) throws PhrescoException { Iterator<Row> rowIterator; try {/*from ww w .j a va2 s .c o m*/ FileInputStream myInput = new FileInputStream(sb.toString()); OPCPackage opc = OPCPackage.open(myInput); XSSFWorkbook myWorkBook = new XSSFWorkbook(opc); int numberOfSheets = myWorkBook.getNumberOfSheets(); for (int j = 0; j < numberOfSheets; j++) { XSSFSheet mySheet = myWorkBook.getSheetAt(j); if (mySheet.getSheetName().equals(testSuiteName)) { rowIterator = mySheet.rowIterator(); Row next; for (Cell cell : mySheet.getRow((mySheet.getLastRowNum()) - 2)) { tryStyle[cellno] = cell.getCellStyle(); cellno = cellno + 1; } float totalPass = 0; float totalFail = 0; float totalNotApp = 0; float totalBlocked = 0; float notExecuted = 0; float totalTestCases = 0; for (int i = 0; i <= 22; i++) { rowIterator.next(); } do { next = rowIterator.next(); if (StringUtils.isNotEmpty(getValue(next.getCell(1))) && !getValue(next.getCell(0)).equalsIgnoreCase("S.NO")) { String value = getValue(next.getCell(11)); if (StringUtils.isNotEmpty(value)) { if (value.equalsIgnoreCase("success")) { totalPass = totalPass + 1; } else if (value.equalsIgnoreCase("failure")) { totalFail = totalFail + 1; } else if (value.equalsIgnoreCase("notApplicable")) { totalNotApp = totalNotApp + 1; } else if (value.equalsIgnoreCase("blocked")) { totalBlocked = totalBlocked + 1; } } else { notExecuted = notExecuted + 1; } } } while (rowIterator.hasNext()); //to update the status in the index page if (status.equalsIgnoreCase("success")) { totalPass = totalPass + 1; } else if (status.equalsIgnoreCase("failure")) { totalFail = totalFail + 1; } else if (status.equalsIgnoreCase("notApplicable")) { totalNotApp = totalNotApp + 1; } else if (status.equalsIgnoreCase("blocked")) { totalBlocked = totalBlocked + 1; } else { notExecuted = notExecuted + 1; } totalTestCases = totalPass + totalFail + totalNotApp + totalBlocked + notExecuted; XSSFSheet mySheet1 = myWorkBook.getSheetAt(0); rowIterator = mySheet1.rowIterator(); for (int i = 0; i <= 2; i++) { rowIterator.next(); } while (rowIterator.hasNext()) { Row next1 = rowIterator.next(); if (StringUtils.isNotEmpty(getValue(next1.getCell(2))) && !getValue(next1.getCell(2)).equalsIgnoreCase("Total")) { TestSuite createObject = createObject(next1); if (createObject.getName().equals(testSuiteName)) { addCalculationsToIndex(totalPass, totalFail, totalNotApp, totalBlocked, notExecuted, totalTestCases, next1); } } } Row r = null; if (mySheet.getSheetName().equalsIgnoreCase("Index")) { r = mySheet.createRow(next.getRowNum() - 1); } else { r = mySheet.createRow(next.getRowNum() + 1); } for (int i = 0; i < numCol; i++) { Cell cell = r.createCell(i); cell.setCellValue(cellValue[i]); cell.setCellStyle(tryStyle[i]); } FileOutputStream fileOut = new FileOutputStream(sb.toString()); myWorkBook.write(fileOut); myInput.close(); fileOut.close(); } } } catch (PhrescoException e) { throw new PhrescoException(e); } catch (IOException e) { throw new PhrescoException(e); } catch (InvalidFormatException e) { throw new PhrescoException(e); } }
From source file:com.rakhi.selenium.readfromexcel.ReadDataFromExcelFileTest.java
@Test public void testPreloadNPUHomepageFromGoogleSearch() throws Exception { try {/*from w w w . j av a 2 s . com*/ FileInputStream file = new FileInputStream( new File("/Users/rakhipartani/Downloads/CSS522_Automation_Data.xlsx")); //HSSFWorkbook workbook = new HSSFWorkbook(file); XSSFWorkbook workbook = new XSSFWorkbook(file); //HSSFSheet sheet = workbook.getSheetAt(0); XSSFSheet sheet = workbook.getSheetAt(0); String heading = sheet.getRow(0).getCell(0).getStringCellValue(); searchText1 = sheet.getRow(1).getCell(0).getStringCellValue(); searchText2 = sheet.getRow(2).getCell(0).getStringCellValue(); System.out.println("Heading is: " + heading); System.out.println("Search Text 1 is: " + searchText1); System.out.println("Search Text 2 is: " + searchText2); file.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } driver.get(baseUrl1); // google.com driver.manage().window().maximize(); // max window size driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS); // wait until page load driver.findElement(By.id("UserName")).clear(); // clear driver.findElement(By.id("UserName")).sendKeys(searchText1); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); this.sleep(2); driver.findElement(By.id("Password")).clear(); // clear driver.findElement(By.id("Password")).sendKeys(searchText2); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); this.sleep(2); driver.findElement(By.xpath("/html/body/div/div[2]/fieldset/form/p[2]/input")).click(); // Go driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); this.sleep(2); // go back to NPU Home page driver.navigate().back(); // close all Thread.sleep(2); driver.quit(); }
From source file:com.respam.comniq.models.POIexcelExporter.java
License:Open Source License
private void findLastRow() throws IOException { String path = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output"; File file = new File(path + File.separator + "POImovieInfo.xlsx"); try {/*from ww w . j a v a 2 s . c o m*/ FileInputStream fis = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet sheet = workbook.getSheet("Movies"); XSSFRow sheetRow = sheet.getRow(lastRow); while (sheetRow != null) { lastRow = lastRow + 1; sheetRow = sheet.getRow(lastRow); } checked = true; } catch (Exception e) { e.printStackTrace(); } }
From source file:com.sandstone_tech.lendfastselenium2.util.PoiFileReader.java
/** * Extracts the content of the excel file to be converted as * {@link TestDataModel} object usable by the data provider * // w w w. j av a 2s . c o m * @param fileName * @return */ protected Object[][] readExcelFileAsTDM(String fileName) { try { XSSFWorkbook wb = new XSSFWorkbook(FileUtils.openInputStream(new File(fileName))); // CONFIGURATION // XSSFSheet sheet = wb.getSheetAt(0); // DATA XSSFSheet dataSheet = wb.getSheetAt(1); if (dataSheet != null && dataSheet.getPhysicalNumberOfRows() > 1) { Object[][] testData = new Object[dataSheet.getPhysicalNumberOfRows() - 2][1]; for (int rowCount = 2; rowCount < dataSheet.getPhysicalNumberOfRows(); rowCount++) { XSSFRow row = dataSheet.getRow(rowCount); TestDataModel testDataModel = new TestDataModel(); testDataModel.setUsername(getStringValue(row.getCell(0))); testDataModel.setPassword(getStringValue(row.getCell(1))); testDataModel.setApplicationType(getStringValue(row.getCell(2))); testDataModel.setOrginationFormModel(this.getOrigination(row)); testDataModel.setPersonalDetailsModel(this.getPersonalDetail(row)); testDataModel.setSignedDeclaration(this.getSignedDeclaration(row)); testDataModel.setCurrentAddress(this.getCurrentAddress(row)); testDataModel.setEmploymentDetails(this.getEmploymentDetails(row)); testDataModel.setPreviousEmploymentDetails(this.getPreviousEmploymentDetails(row)); testDataModel.setPurchaseProperty( new PurchaseProperty(this.getPropertyContract(row), this.getPropertyPurpose(row))); testData[rowCount - 2][0] = testDataModel; } wb.close(); return testData; } wb.close(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:com.smanempat.controller.ControllerClassification.java
private void showXLSX(JTextField txtFileDirectory, JTable tablePreview) throws FileNotFoundException, IOException { DefaultTableModel tableModel = new DefaultTableModel(); File fileName = new File(txtFileDirectory.getText()); FileInputStream inputStream = new FileInputStream(fileName); XSSFWorkbook workbook = new XSSFWorkbook(inputStream); XSSFSheet sheet = workbook.getSheetAt(0); int rowValue = sheet.getLastRowNum() + 1; int colValue = sheet.getRow(0).getLastCellNum(); String[][] data = new String[rowValue][colValue]; String[] colName = new String[colValue]; for (int i = 0; i < rowValue; i++) { XSSFRow row = sheet.getRow(i);/* w ww. ja v a 2s. c o m*/ for (int j = 0; j < colValue; j++) { XSSFCell cell = row.getCell(j); int type = cell.getCellType(); Object returnCellValue = null; if (type == 0) { returnCellValue = cell.getNumericCellValue(); } else if (type == 1) { returnCellValue = cell.getStringCellValue(); } data[i][j] = returnCellValue.toString(); } } for (int i = 0; i < colValue; i++) { colName[i] = data[0][i]; } tableModel = new DefaultTableModel(data, colName); tablePreview.setModel(tableModel); tableModel.removeRow(0); }
From source file:com.smanempat.view.ReadWorkbook.java
private void ekstensiXLSXTraining() { try {/*from w w w. j a va 2s .c o m*/ FileInputStream FIS = new FileInputStream(new File(txtNamaFile.getText())); XSSFWorkbook wb = new XSSFWorkbook(FIS); XSSFSheet ws = wb.getSheetAt(0); int baris = ws.getLastRowNum() + 1; System.out.println(baris); int kolom = ws.getRow(0).getLastCellNum(); System.out.println(kolom); String[][] data = new String[baris][kolom]; String[] nKolom = new String[kolom]; for (int i = 0; i < baris; i++) { XSSFRow row = ws.getRow(i); for (int j = 0; j < kolom; j++) { XSSFCell cell = row.getCell(j); System.out.println("Isi Cell Adalah =" + cell); String value = nilaiCell(cell); data[i][j] = value; System.out.println("data['" + i + "']['" + j + "'] " + data[i][j]); } } //ambil nama kolom pada index data baris ke 0 for (int a = 0; a < kolom; a++) { nKolom[a] = data[0][a]; } //Isi ke database String nim = null; String nama = null; String nilai = null; for (int b = 1; b < baris; b++) { int c = 0; while (c < kolom) { nim = data[b][c]; nama = data[b][c + 1]; nilai = data[b][c + 2]; break; } insert(nim, nama, nilai); } DefaultTableModel DTM = new DefaultTableModel(data, nKolom); jTable1.setModel(DTM); DTM.removeRow(0); } catch (Exception er1) { System.out.print("Error : \n" + er1.toString()); } }
From source file:com.vaadin.addon.spreadsheet.GroupingUtil.java
public static void expandRow(XSSFSheet sheet, int rowNumber) { if (rowNumber == -1) { return;/*from w w w. jav a 2 s .c om*/ } XSSFRow row = sheet.getRow(rowNumber); // If it is already expanded do nothing. if (!row.getCTRow().isSetHidden()) { return; } // Find the start of the group. int startIdx = findStartOfRowOutlineGroup(sheet, rowNumber); // Find the end of the group. int endIdx = findEndOfRowOutlineGroup(sheet, rowNumber); // expand: // collapsed must be unset // hidden bit gets unset _if_ surrounding groups are expanded you can // determine // this by looking at the hidden bit of the enclosing group. You will // have // to look at the start and the end of the current group to determine // which // is the enclosing group // hidden bit only is altered for this outline level. ie. don't // un-collapse contained groups short level = row.getCTRow().getOutlineLevel(); if (!isRowGroupHiddenByParent(sheet, rowNumber)) { /** change start */ // start and end are off by one because POI did edge detection. Move // start back to correct pos: startIdx++; // end is already correct because of another bug (using '<' instead // of '<=' below) /** change end */ for (int i = startIdx; i < endIdx; i++) { XSSFRow r = sheet.getRow(i); if (level == r.getCTRow().getOutlineLevel()) { r.getCTRow().unsetHidden(); } else if (!isRowGroupOrParentCollapsed(sheet, i, level)) { r.getCTRow().unsetHidden(); } } } // Write collapse field /** start */ if (isRowsInverted(sheet)) { XSSFRow r = sheet.getRow(startIdx - 1); if (r != null && r.getCTRow().getCollapsed()) { r.getCTRow().unsetCollapsed(); } } else { CTRow ctRow = sheet.getRow(endIdx).getCTRow(); // This avoids an IndexOutOfBounds if multiple nested groups are // collapsed/expanded if (ctRow.getCollapsed()) { ctRow.unsetCollapsed(); } } /** end */ }
From source file:com.vaadin.addon.spreadsheet.GroupingUtil.java
private static int findEndOfRowOutlineGroup(XSSFSheet sheet, int row) { short level = sheet.getRow(row).getCTRow().getOutlineLevel(); int currentRow; /** start *//* w w w . j a v a 2 s . c o m*/ int lastRowNum = sheet.getLastRowNum() + 1; /** end */ for (currentRow = row; currentRow < lastRowNum; currentRow++) { XSSFRow row2 = sheet.getRow(currentRow); if (row2 == null || row2.getCTRow().getOutlineLevel() < level) { break; } } return currentRow; }
From source file:com.vaadin.addon.spreadsheet.GroupingUtil.java
/** * Replaces {@link XSSFSheet#isRowGroupCollapsed(XSSFSheet, int)}, which * doesn't account for intermediary levels being collapsed or not. *///from w w w . java 2 s. co m private static boolean isRowGroupOrParentCollapsed(XSSFSheet sheet, int row, int originalLevel) { int level = sheet.getRow(row).getCTRow().getOutlineLevel(); // start from row level and work upwards to original level while (level > originalLevel) { int collapseRow; if (isRowsInverted(sheet)) { collapseRow = findStartOfRowOutlineGroup(sheet, row); row--; } else { collapseRow = findEndOfRowOutlineGroup(sheet, row); row++; } if (sheet.getRow(collapseRow) != null) { CTRow ctRow = sheet.getRow(collapseRow).getCTRow(); level = ctRow.getOutlineLevel(); boolean collapsed = ctRow.getCollapsed(); if (collapsed && ctRow.getOutlineLevel() >= originalLevel) { // this parent is collapsed return true; } } } return false; }