List of usage examples for org.apache.poi.ss.usermodel Row getRowNum
int getRowNum();
From source file:com.veeduria.web.cargaarchivo.aut.th.CargaPlanta.java
public void cargarArchivoEmpleados(Path rutaArchivo) { //Get first sheet from the workbook try {// w w w . ja va 2 s .c o m StringBuilder strBSql = new StringBuilder(); HSSFWorkbook workbook = new HSSFWorkbook(Files.newInputStream(rutaArchivo, StandardOpenOption.READ)); HSSFSheet sheet = workbook.getSheet("EMPLEADOS"); if (sheet != null) { Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = (Row) rowIterator.next(); if (row.getRowNum() >= 10) { if (row.getCell(0) != null) { for (int i = 0; i < 42; i++) { Cell cell = row.getCell(i); switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: strBSql.append(cell.getBooleanCellValue()); strBSql.append(","); // System.out.print(cell.getBooleanCellValue() + "\t\t"); break; case Cell.CELL_TYPE_NUMERIC: strBSql.append(cell.getNumericCellValue()); strBSql.append(","); // System.out.print(cell.getNumericCellValue() + "\t\t"); break; case Cell.CELL_TYPE_STRING: strBSql.append(",'"); strBSql.append(cell.getStringCellValue()); strBSql.append("',"); // System.out.print(cell.getStringCellValue() + "\t\t"); break; } } strBSql.replace(strBSql.length() - 1, strBSql.length(), ""); } } strBSql.append(System.getProperty("line.separator")); } System.out.println(""); } // FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.home") + File.separator + "test.xls")); // workbook.write(out); // out.close(); } catch (IOException e) { Logger.getLogger(VigilarCarpetaSLBean.class.getName()).log(Level.SEVERE, null, e); } }
From source file:com.virtusa.isq.vtaf.runtime.SeleniumTestBase.java
License:Apache License
/** * Adds the values from excel./* w ww .j a va 2 s . c o m*/ * * @param path * the path * @param index * the index * @return the string[][] * @throws IOException * Signals that an I/O exception has occurred. * @throws InvalidFormatException * the invalid format exception */ public final String[][] addValuesFromExcel(final String path, final String index) throws IOException, InvalidFormatException { String cellStringValue = null; double cellDoubleValue = 0; Boolean cellBooleanValue; byte cellErrorValue = 0; String[][] arrExcelContent; FileInputStream file = null; Workbook workbook = null; Sheet sheet = null; try { file = new FileInputStream(new File(path)); workbook = WorkbookFactory.create(file); sheet = workbook.getSheetAt(Integer.parseInt(index)); Iterator<Row> rowIterator = sheet.iterator(); arrExcelContent = new String[sheet.getPhysicalNumberOfRows()][]; while (rowIterator.hasNext()) { Row row = rowIterator.next(); int rowNumber = row.getRowNum(); Iterator<Cell> cellIterator = row.cellIterator(); arrExcelContent[rowNumber] = new String[sheet.getRow(rowNumber).getPhysicalNumberOfCells()]; while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); int cellNumber = cell.getColumnIndex(); if (cell.getCellType() == Cell.CELL_TYPE_STRING) { cellStringValue = cell.getStringCellValue(); arrExcelContent[rowNumber][cellNumber] = cellStringValue; } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { cellBooleanValue = cell.getBooleanCellValue(); arrExcelContent[rowNumber][cellNumber] = cellBooleanValue.toString(); } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) { cellErrorValue = cell.getErrorCellValue(); arrExcelContent[rowNumber][cellNumber] = Byte.toString(cellErrorValue); } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) { cellStringValue = cell.getCellFormula(); arrExcelContent[rowNumber][cellNumber] = cellStringValue; } else { cellDoubleValue = cell.getNumericCellValue(); arrExcelContent[rowNumber][cellNumber] = Double.toString(cellDoubleValue); } } } } finally { if (((InputStream) workbook) != null) { ((InputStream) workbook).close(); } } return arrExcelContent; }
From source file:com.vodafone.poms.ii.helpers.ActivityLoader.java
public void readFile(FileUploadEvent event) { if (event.getFile() != null) { Activity activity = null;/* w w w. j av a 2 s . c o m*/ List<Activity> activities = new ArrayList<>(); try { String errors = ""; XSSFWorkbook myWorkBook = new XSSFWorkbook(event.getFile().getInputstream()); int numberOfSheets = myWorkBook.getNumberOfSheets(); XSSFSheet sheet = null; System.out.println(numberOfSheets); for (int i = 0; i < numberOfSheets; i++) { if (myWorkBook.getSheetAt(i).getSheetName().toLowerCase().contains("rack")) { sheet = myWorkBook.getSheetAt(i); break; } } if (sheet != null) { int numOfMergedRegions = sheet.getNumMergedRegions(); for (int i = 0; i < numOfMergedRegions; i++) { sheet.removeMergedRegion(0); } Iterator<Row> rowIterator = sheet.iterator(); if (rowIterator.hasNext()) { rowIterator.next(); } while (rowIterator.hasNext()) { Row row = rowIterator.next(); if (getCellValue(row.getCell(0)).length() > 0) { activity = new Activity(); Sites site = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if ((site = sitesController .getSites(getCellValue(row.getCell(0)).toUpperCase())) != null) { activity.setSite(site); // Site activity.setAsp( subContractorsController.getSubcontractors(getCellValue(row.getCell(1)))); // ASP if (activity.getAsp() == null) { errors += "Row:" + (row.getRowNum() + 1) + " contains unrecognized ASP: Current Value: " + getCellValue(row.getCell(1)) + "\n"; System.out.println("Row:" + (row.getRowNum() + 1) + " contains unrecognized ASP: Current Value: " + getCellValue(row.getCell(1))); continue; } activity.setArea(areaController.getArea(getCellValue(row.getCell(2)))); // Area if (activity.getArea() == null) { errors += "Row:" + (row.getRowNum() + 1) + " contains unrecognized Area: Current Value: " + getCellValue(row.getCell(2)) + "\n"; System.out.println("Row:" + (row.getRowNum() + 1) + " contains unrecognized Area: Current Value: " + getCellValue(row.getCell(2))); continue; } if (vendorOwnerController.getByName(getCellValue(row.getCell(3))) != null) { activity.setVendorOwner( vendorOwnerController.getByName(getCellValue(row.getCell(3)))); // Owner } else { errors += "Row:" + (row.getRowNum() + 1) + " contains unrecognized Vendor Owner: Current Value: " + getCellValue(row.getCell(3)) + "\n"; System.out.println("Row:" + (row.getRowNum() + 1) + " contains unrecognized Vendor Owner: Current Value: " + getCellValue(row.getCell(3))); continue; } activity.setClaimStatus( claimStatusController.getClaimStatus(getCellValue(row.getCell(4)))); // Claim activity.setApprovalStatus( approvalStatusController.getApprovalStatus(getCellValue(row.getCell(5)))); // Approval activity.setActivityType( domainNamesController.getDomainNames(getCellValue(row.getCell(6)))); // type if (activity.getActivityType() == null) { System.out.println("Row:" + (row.getRowNum() + 1) + " Activity Type is not recognized, Current Value: " + getCellValue(row.getCell(6))); errors += "Row:" + (row.getRowNum() + 1) + " Activity Type is not recognized, Current Value: " + getCellValue(row.getCell(6)) + "\n"; continue; } activity.setPhase(phasesController.getPhases(getCellValue(row.getCell(7)))); // phase if (row.getCell(8).getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { activity.setActivityDate(getDateCellValue(row.getCell(8))); // date } else { System.out.println("Row:" + (row.getRowNum() + 1) + " Activity date is not recognized, Current Value: " + getCellValue(row.getCell(8))); errors += "Row:" + (row.getRowNum() + 1) + " Activity date is not recognized, Current Value: " + getCellValue(row.getCell(8)) + "\n"; continue; } activity.setActivityCode( activityCodeController.getActivityCode(getCellValue(row.getCell(9)))); // Activity Code activity.setAcDescription(getCellValue(row.getCell(10))); // Claim activity.setActivityDetails(getCellValue(row.getCell(11))); // Claim if (activity.getActivityDetails() == null) { System.out.println("Row:" + (row.getRowNum() + 1) + " Empty Activity Details/Merged Cells"); errors += "Row:" + (row.getRowNum() + 1) + " Empty Activity Details/Merged Cells" + "\n"; continue; } if (activity.getActivityDetails().length() == 0) { activity.setActivityDetails("Regular Activity"); } try { activity.setQty(Double.parseDouble( (getCellValue(row.getCell(12)) != null ? getCellValue(row.getCell(12)) : "0"))); // Qty } catch (NumberFormatException e) { activity.setQty(0); } activity.setAcVendorPrice(Float.parseFloat( (getCellValue(row.getCell(13)) != null ? getCellValue(row.getCell(13)) : "0"))); // vendor price activity.setTotalPriceVendor(Float.parseFloat( (getCellValue(row.getCell(14)) != null ? getCellValue(row.getCell(14)) : "0"))); // vendor price activity.setTotalPriceVendorTaxes(Float.parseFloat( (getCellValue(row.getCell(15)) != null ? getCellValue(row.getCell(15)) : "0"))); // vendor price activity.setAcSubcontractorPrice(Float.parseFloat( (getCellValue(row.getCell(16)) != null ? getCellValue(row.getCell(16)) : "0"))); // vendor price activity.setTotalPriceAsp(Float.parseFloat( (getCellValue(row.getCell(17)) != null ? getCellValue(row.getCell(17)) : "0"))); // vendor price activity.setTotalUm(Float.parseFloat( (getCellValue(row.getCell(18)) != null ? getCellValue(row.getCell(18)) : "0"))); // vendor price activity.setTotalUmPercent(Float.parseFloat( (getCellValue(row.getCell(19)) != null ? getCellValue(row.getCell(19)) : "0"))); // vendor price activity.setActivityComment(getCellValue(row.getCell(20))); // vendor price activity.setCorrelateTo(getCellValue(row.getCell(21))); // vendor price activity.setSysDate(new Date()); activity.setCreator(usersController.getLoggedInUser()); activity.setTaxes(13.0); activities.add(activity); } else { System.out.println("Row:" + (row.getRowNum() + 1) + " Activity Site is not recognized, Current Value: " + getCellValue(row.getCell(0))); errors += "Row:" + (row.getRowNum() + 1) + " Activity Site is not recognized, Current Value: " + getCellValue(row.getCell(0)) + "\n"; } } } } if (!testFile) { activityController.createMultiple(activities); } if (errors.length() == 0) { setErrors("No Errors Found"); } else { setErrors(errors); } } catch (FileNotFoundException ex) { Logger.getLogger(ActivityLoader.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ActivityLoader.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception e) { e.printStackTrace(); } finally { } } }
From source file:com.vodafone.poms.ii.helpers.ExportManager.java
public void exportActivity() { try {/*from ww w . j a v a 2 s .com*/ List<Activity> activities = activityController.getExportItems(fromDate, toDate); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Master Track"); int imgPckId = addImageToWorkbook(workbook, "/home/poms/uploaded_data/pkg.png", Workbook.PICTURE_TYPE_PNG); String imgPckRelId = addImageToSheet(sheet, imgPckId, Workbook.PICTURE_TYPE_PNG); Row row = sheet.createRow(0); for (int i = 0; i < activityHeaders.length; i++) { row.createCell(i).setCellValue(activityHeaders[i]); } for (int i = 0; i < activities.size(); i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue(activities.get(i).getSite().getSitePhysical()); row.createCell(1).setCellValue(activities.get(i).getAsp().getSubcontractorName()); row.createCell(2).setCellValue(activities.get(i).getArea().getAreaName()); row.createCell(3).setCellValue(activities.get(i).getVendorOwner().getOwnerName()); if (activities.get(i).getClaimStatus() != null) { row.createCell(4).setCellValue(activities.get(i).getClaimStatus().getClaimName()); } else { row.createCell(4).setCellValue(""); } if (activities.get(i).getApprovalStatus() != null) { row.createCell(5).setCellValue(activities.get(i).getApprovalStatus().getStatusName()); } else { row.createCell(5).setCellValue(""); } row.createCell(6).setCellValue(activities.get(i).getActivityType().getDomainName()); if (activities.get(i).getPhase() != null) { row.createCell(7).setCellValue(activities.get(i).getPhase().getPhaseName()); } else { row.createCell(7).setCellValue(""); } row.createCell(8).setCellValue(activities.get(i).getActivityDate()); row.createCell(9).setCellValue(activities.get(i).getAcMaterialId()); row.createCell(10).setCellValue(activities.get(i).getAcDescription()); row.createCell(11).setCellValue(activities.get(i).getActivityDetails()); row.createCell(12).setCellValue(activities.get(i).getQty()); row.createCell(13).setCellValue(activities.get(i).getAcVendorPrice()); row.createCell(14).setCellValue(activities.get(i).getTotalPriceVendor()); row.createCell(15).setCellValue(activities.get(i).getTotalPriceVendorTaxes()); row.createCell(16).setCellValue(activities.get(i).getAcSubcontractorPrice()); row.createCell(17).setCellValue(activities.get(i).getTotalPriceAsp()); row.createCell(18).setCellValue(activities.get(i).getTotalUm()); row.createCell(19).setCellValue(activities.get(i).getTotalUmPercent()); row.createCell(20).setCellValue(activities.get(i).getActivityComment()); row.createCell(21).setCellValue((activities.get(i).getAspPoCollection().isEmpty() ? "Uncorrelated" : ((AspPo) activities.get(i).getAspPoCollection().toArray()[0]).getPoNumber())); if (activities.get(i).getActivityAttachmentsCollection() != null) { if (activities.get(i).getActivityAttachmentsCollection().size() > 0) { Object[] attachments = activities.get(i).getActivityAttachmentsCollection().toArray(); for (int j = 0; j < attachments.length; j++) { XSSFClientAnchor imgAnchor1 = new XSSFClientAnchor(0, 0, 0, 0, (23 + j), row.getRowNum(), (23 + j + 1), row.getRowNum() + 1); String oleRelId1 = addFile(sheet, ((ActivityAttachments) attachments[j]).getAttachmentLocation(), (i + j + activities.get(i).getActivityId().intValue() + Math.random())); int shapeId1 = addImageToShape(sheet, imgAnchor1, imgPckId); addObjectToShape(sheet, imgAnchor1, shapeId1, oleRelId1, imgPckRelId, "Objekt-Manager-Shellobjekt"); } } } } FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); externalContext.setResponseContentType("application/vnd.ms-excel"); externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"G-Cairo Region Extra Work.xlsx\""); try { workbook.write(externalContext.getResponseOutputStream()); externalContext.getResponseOutputStream().close(); } catch (IOException ex) { Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex); } facesContext.responseComplete(); JsfUtil.addSuccessMessage("Activity Report is now exported"); } catch (InvalidFormatException ex) { Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ExportManager.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.web.mavenproject6.other.XLSParser.java
public static String parse(String name) { String result = ""; InputStream in = null;/*from w w w .j ava 2 s . c o m*/ HSSFWorkbook wb = null; try { in = new FileInputStream(name); wb = new HSSFWorkbook(in); } catch (IOException e) { e.printStackTrace(); } System.out.println("!!!!sheet count:" + wb.getNumberOfSheets()); for (int i = 0; i < wb.getNumberOfSheets(); i++) { System.out.println("!!!!sheet[" + i + "]:" + wb.getSheetName(i)); } for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i); Iterator<Row> it = sheet.iterator(); String buf = ""; while (it.hasNext()) { Row row = it.next(); //result += row.getRowNum() + ":"; switch (row.getRowNum() + 1) { case 13: result += readCell(row); result += "<br>"; break; case 14: result += readCell(row); result += "<br>"; break; case 15: result += readCell(row); break; case 16: result += readCell(row); result += "<br>"; break; case 18: result += readCell(row); break; case 19: result += readCell(row); break; case 20: result += readCell(row); break; case 21: result += readCell(row); break; case 22: result += readCell(row); result += "<br>"; break; case 25: result += readCell(row); break; case 26: result += readCell(row); result += "<br>"; break; default: result += readCell(row); result += "<br>"; break; } // if (row.getRowNum() + 1 != 34 || row.getRowNum() + 1 != 34) { // result += readCell(row); // } else { // result += "[" + readCell(row, 0, 7) + "][" + readCell(row, 7, 10); // } } } return result; }
From source file:com.web.mavenproject6.other.XLSParser.java
public static String parseStatisticDoc(String name) { String result = ""; InputStream in = null;//from ww w .j a v a2 s . co m HSSFWorkbook wb = null; try { in = new FileInputStream(name); wb = new HSSFWorkbook(in); } catch (IOException e) { e.printStackTrace(); } System.out.println("!!!!sheet count:" + wb.getNumberOfSheets()); for (int i = 0; i < wb.getNumberOfSheets(); i++) { System.out.println("!!!!sheet[" + i + "]:" + wb.getSheetName(i)); } Sheet sheet = wb.getSheetAt(0); Iterator<Row> it = sheet.iterator(); while (it.hasNext()) { Row row = it.next(); Iterator<Cell> cells = row.iterator(); // row.getCell(0).getStringCellValue() switch (row.getRowNum()) { } result += "\n"; } return result; }
From source file:com.ween.learn.util.excel.ExportExcel.java
License:Open Source License
/** * ?annotation.ExportField?//from ww w . j av a 2 s.co m * @return list ? */ public <E> ExportExcel setDataList(List<E> list) { for (E e : list) { int colunm = 0; Row row = this.addRow(); StringBuilder sb = new StringBuilder(); for (Object[] os : annotationList) { ExcelField ef = (ExcelField) os[0]; Object val = null; // Get entity value try { if (StringUtils.isNotBlank(ef.value())) { val = Reflections.invokeGetter(e, ef.value()); } else { if (os[1] instanceof Field) { val = Reflections.invokeGetter(e, ((Field) os[1]).getName()); } else if (os[1] instanceof Method) { val = Reflections.invokeMethod(e, ((Method) os[1]).getName(), new Class[] {}, new Object[] {}); } } } catch (Exception ex) { // Failure to ignore log.info(ex.toString()); val = ""; } this.addCell(row, colunm++, val, ef.align(), ef.fieldType()); sb.append(val + ", "); } log.debug("Write success: [" + row.getRowNum() + "] " + sb.toString()); } return this; }
From source file:com.xl.main.ReadExcelSampleSilk.java
public static String read(String filename) { Gson gson = new Gson(); Map<String, List<SampleSinkBean>> values = new HashMap<String, List<SampleSinkBean>>(); List<SampleSinkBean> byRow = new ArrayList<SampleSinkBean>(); try {/*from w w w. j a v a2 s .c om*/ FileInputStream file = null; if (filename == null) { file = new FileInputStream(new File("H:\\anil\\sample-sink.xlsx")); } else { file = new FileInputStream(new File(filename)); } //Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbook = new XSSFWorkbook(file); //Get first/desired sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); //Iterate through each rows one by one Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); //For each row, iterate through all the columns Iterator<Cell> cellIterator = row.cellIterator(); if (row.getRowNum() > 0 && row.getRowNum() < 20) { SampleSinkBean sb = new SampleSinkBean(); //System.out.println("row value" + sheet.getRow(3).getCell(3)); while (cellIterator.hasNext()) {// Cell cell = cellIterator.next(); String cellString = " "; switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: cellString = cell.getNumericCellValue() + ""; break; case Cell.CELL_TYPE_FORMULA: cellString = cell.getStringCellValue() + ""; break; case Cell.CELL_TYPE_ERROR: cellString = cell.getErrorCellValue() + ""; break; default: cellString = cell.getStringCellValue() + ""; } switch (cell.getColumnIndex()) { case 0: sb.setFrYear(cellString); break; case 1: sb.setVpmod(cellString); case 2: sb.setProjectName(cellString); case 3: sb.setProjectWorktype(cellString); case 4: sb.setBusinessObjective(cellString); } } byRow.add(sb); } // System.out.println(""); } values.put("sink", byRow); System.out.println("output *********" + gson.toJson(values)); file.close(); } catch (Exception e) { e.printStackTrace(); } return gson.toJson(values); }
From source file:com.yqboots.initializer.core.builder.excel.DataDictSheetBuilder.java
License:Apache License
@Override protected void doBuild(final Path root, final ProjectMetadata metadata, final Sheet sheet) throws IOException { final List<DataDict> items = new ArrayList<>(); for (Row row : sheet) { // ignore the header if (row.getRowNum() < 1) { continue; }/* ww w . jav a 2s . c o m*/ items.add(getDataDicts(row)); } // generate an XML for the application importing into Database final Path targetPath = Paths.get(root + File.separator + properties.getExportRelativePath()); if (!Files.exists(targetPath)) { Files.createDirectories(targetPath); } final Path file = Paths.get(targetPath + File.separator + properties.getExportName() + FileType.DOT_XML); try (FileWriter writer = new FileWriter(file.toFile())) { marshaller.marshal(new DataDicts(items), new StreamResult(writer)); } }
From source file:com.yqboots.initializer.core.builder.excel.factory.DomainMetadataFactory.java
License:Apache License
/** * Gets the starting points of each domain. * * @param sheet the Excel sheet/*w w w.j a va 2 s . c om*/ * @return all starting points */ private static List<Integer> getStartingPoints(final Sheet sheet) { List<Integer> results = new ArrayList<>(); for (Row row : sheet) { // ignore the header if (row.getRowNum() < 2) { continue; } Cell module = row.getCell(0); Cell domain = row.getCell(1); if (module != null && domain != null) { results.add(row.getRowNum()); } } // ending flag results.add(sheet.getLastRowNum() + 1); return results; }