List of usage examples for org.apache.poi.ss.usermodel Cell getColumnIndex
int getColumnIndex();
From source file:com.accenture.control.ExcelDAO.java
public String[] carregaPlanilhaSistemaMaster() throws IOException { Plano plano = new Plano(); String[] sistemaMaster = null; try {/* w ww . j a v a2s .c o m*/ FileInputStream arquivo = new FileInputStream(new File(ExcelDAO.fileName)); XSSFWorkbook workbook = new XSSFWorkbook(arquivo); //setado a planilha de configuraes XSSFSheet sheetPlano = workbook.getSheetAt(2); //linha pa int linha = 1; int coluna = 2; sistemaMaster = new String[sheetPlano.getLastRowNum()]; int index = 0; for (int count = 1; count < sheetPlano.getLastRowNum(); count++) { Row row = sheetPlano.getRow(count); for (int countColuna = 0; countColuna < 1; countColuna++) { Cell cell = row.getCell(coluna, Row.CREATE_NULL_AS_BLANK); System.out.println(cell.getColumnIndex() + "-" + cell.getRowIndex()); if (cell.getCellType() == CELL_TYPE_BLANK) { System.out.println("Campo vazio"); } else if (cell.getCellType() == CELL_TYPE_NUMERIC) { double valor = cell.getNumericCellValue(); System.out.println(valor); } else { String valor = cell.getStringCellValue(); System.out.println(valor); sistemaMaster[index] = valor; System.out.println(sistemaMaster[index]); index++; } } } } catch (FileNotFoundException ex) { Logger.getLogger(ExcelDAO.class.getName()).log(Level.SEVERE, null, ex); } return sistemaMaster; }
From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java
License:Open Source License
public static void convertHSSF2Pdf(Workbook wb, String header, File reportFile) throws Exception { assert wb != null; assert reportFile != null; //Precompute formula FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i);/* www . jav a 2 s . c o m*/ for (Row r : sheet) { for (Cell c : r) { if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { try { evaluator.evaluateFormulaCell(c); } catch (Exception e) { System.err.println(e); } } } } } File tmp = File.createTempFile("tmp_", ".xlsx"); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) { wb.write(out); } //Find page orientation int maxColumnsGlobal = 0; for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) { Sheet sheet = wb.getSheetAt(sheetNo); for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) { Row row = rowIterator.next(); maxColumnsGlobal = Math.max(maxColumnsGlobal, row.getLastCellNum()); } } Rectangle pageSize = maxColumnsGlobal < 10 ? PageSize.A4 : PageSize.A4.rotate(); Document pdfDocument = new Document(pageSize, 10f, 10f, 20f, 20f); PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(reportFile)); addHeader(writer, header); pdfDocument.open(); //we have two columns in the Excel sheet, so we create a PDF table with two columns //Note: There are ways to make this dynamic in nature, if you want to. //Loop through sheets for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) { Sheet sheet = wb.getSheetAt(sheetNo); //Loop through rows, to find number of columns int minColumns = 1000; int maxColumns = 0; for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) { Row row = rowIterator.next(); if (row.getFirstCellNum() >= 0) minColumns = Math.min(minColumns, row.getFirstCellNum()); if (row.getLastCellNum() >= 0) maxColumns = Math.max(maxColumns, row.getLastCellNum()); } if (maxColumns == 0) continue; //Loop through first rows, to find relative width float[] widths = new float[maxColumns]; int totalWidth = 0; for (int c = 0; c < maxColumns; c++) { int w = sheet.getColumnWidth(c); widths[c] = w; totalWidth += w; } for (int c = 0; c < maxColumns; c++) { widths[c] /= totalWidth; } //Create new page and a new chapter with the sheet's name if (sheetNo > 0) pdfDocument.newPage(); Chapter pdfSheet = new Chapter(sheet.getSheetName(), sheetNo + 1); PdfPTable pdfTable = null; PdfPCell pdfCell = null; boolean inTable = false; //Loop through cells, to create the content // boolean leftBorder = true; // boolean[] topBorder = new boolean[maxColumns+1]; for (int r = 0; r <= sheet.getLastRowNum(); r++) { Row row = sheet.getRow(r); //Check if we exited a table (empty line) if (row == null) { if (pdfTable != null) { addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable); pdfTable = null; } inTable = false; continue; } //Check if we start a table (>MIN_COL_IN_TABLE columns) if (row.getLastCellNum() >= MIN_COL_IN_TABLE) { inTable = true; } if (!inTable) { //Process the data outside table, just add the text boolean hasData = false; Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (cell.getCellType() == Cell.CELL_TYPE_BLANK) continue; Chunk chunk = getChunk(wb, cell); pdfSheet.add(chunk); pdfSheet.add(new Chunk(" ")); hasData = true; } if (hasData) pdfSheet.add(Chunk.NEWLINE); } else { //Process the data in table if (pdfTable == null) { //Create table pdfTable = new PdfPTable(maxColumns); pdfTable.setWidths(widths); // topBorder = new boolean[maxColumns+1]; } int cellNumber = minColumns; // leftBorder = false; Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); for (; cellNumber < cell.getColumnIndex(); cellNumber++) { pdfCell = new PdfPCell(); pdfCell.setBorder(0); pdfTable.addCell(pdfCell); } Chunk phrase = getChunk(wb, cell); pdfCell = new PdfPCell(new Phrase(phrase)); pdfCell.setFixedHeight(row.getHeightInPoints() - 3); pdfCell.setNoWrap(!cell.getCellStyle().getWrapText()); pdfCell.setPaddingLeft(1); pdfCell.setHorizontalAlignment( cell.getCellStyle().getAlignment() == CellStyle.ALIGN_CENTER ? PdfPCell.ALIGN_CENTER : cell.getCellStyle().getAlignment() == CellStyle.ALIGN_RIGHT ? PdfPCell.ALIGN_RIGHT : PdfPCell.ALIGN_LEFT); pdfCell.setUseBorderPadding(false); pdfCell.setUseVariableBorders(false); pdfCell.setBorderWidthRight(cell.getCellStyle().getBorderRight() == 0 ? 0 : .5f); pdfCell.setBorderWidthBottom(cell.getCellStyle().getBorderBottom() == 0 ? 0 : cell.getCellStyle().getBorderBottom() > 1 ? 1 : .5f); pdfCell.setBorderWidthLeft(cell.getCellStyle().getBorderLeft() == 0 ? 0 : cell.getCellStyle().getBorderLeft() > 1 ? 1 : .5f); pdfCell.setBorderWidthTop(cell.getCellStyle().getBorderTop() == 0 ? 0 : cell.getCellStyle().getBorderTop() > 1 ? 1 : .5f); String color = cell.getCellStyle().getFillForegroundColorColor() == null ? null : ((XSSFColor) cell.getCellStyle().getFillForegroundColorColor()).getARGBHex(); if (color != null) pdfCell.setBackgroundColor(new Color(Integer.decode("0x" + color.substring(2)))); pdfTable.addCell(pdfCell); cellNumber++; } for (; cellNumber < maxColumns; cellNumber++) { pdfCell = new PdfPCell(); pdfCell.setBorder(0); pdfTable.addCell(pdfCell); } } //Custom code to add all images on the first sheet (works for reporting) if (sheetNo == 0 && row.getRowNum() == 0) { for (PictureData pd : wb.getAllPictures()) { try { Image pdfImg = Image.getInstance(pd.getData()); pdfImg.scaleToFit( pageSize.getWidth() * .8f - pageSize.getBorderWidthLeft() - pageSize.getBorderWidthRight(), pageSize.getHeight() * .8f - pageSize.getBorderWidthTop() - pageSize.getBorderWidthBottom()); pdfSheet.add(pdfImg); } catch (Exception e) { e.printStackTrace(); } } } } if (pdfTable != null) { addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable); } pdfDocument.add(pdfSheet); } pdfDocument.close(); }
From source file:com.adobe.acs.commons.data.Spreadsheet.java
License:Apache License
private List<Variant> readRow(Row row) { Iterator<Cell> iterator = row.cellIterator(); List<Variant> rowOut = new ArrayList<>(); while (iterator.hasNext()) { Cell c = iterator.next(); while (c.getColumnIndex() > rowOut.size()) { rowOut.add(null);/* www . j av a2 s. c o m*/ } Variant val = new Variant(c); rowOut.add(val.isEmpty() ? null : val); } return rowOut; }
From source file:com.adobe.acs.commons.mcp.util.Spreadsheet.java
License:Apache License
private List<String> readRow(Row row) { Iterator<Cell> iterator = row.cellIterator(); List<String> rowOut = new ArrayList<>(); while (iterator.hasNext()) { Cell c = iterator.next(); while (c.getColumnIndex() > rowOut.size()) { rowOut.add(null);//from ww w . j a va 2 s.c om } rowOut.add(getStringValueFromCell(c)); } return rowOut; }
From source file:com.AllenBarr.CallSheetGenerator.CallSheetGeneratorControllerController.java
License:Open Source License
private void populateContributorList() { if (excelSheet.exists()) { try {/* www. ja va 2 s .c om*/ wb = WorkbookFactory.create(excelSheet); wbSheet = wb.getSheetAt(0); Row wbRow = wbSheet.getRow(0); Integer vanIDColumnIndex = 0; Integer fNameColumnIndex = 0; Integer lNameColumnIndex = 0; for (Cell cell : wbRow) { if (null != cell.getStringCellValue()) { switch (cell.getStringCellValue()) { case "VANID": vanIDColumnIndex = cell.getColumnIndex(); break; case "LastName": lNameColumnIndex = cell.getColumnIndex(); break; case "FirstName": fNameColumnIndex = cell.getColumnIndex(); break; } } } final ObservableList<String> names = FXCollections.observableArrayList(); for (Row row : wbSheet) { switch (row.getCell(vanIDColumnIndex).getCellType()) { case Cell.CELL_TYPE_STRING: names.add(row.getCell(vanIDColumnIndex).getStringCellValue() + " " + row.getCell(fNameColumnIndex).getStringCellValue() + " " + row.getCell(lNameColumnIndex).getStringCellValue()); break; case Cell.CELL_TYPE_NUMERIC: names.add((int) row.getCell(vanIDColumnIndex).getNumericCellValue() + " " + row.getCell(fNameColumnIndex).getStringCellValue() + " " + row.getCell(lNameColumnIndex).getStringCellValue()); break; } } contributorSelector.setItems(names); contributorSelector.getSelectionModel().select(0); } catch (IOException | InvalidFormatException ex) { Logger.getLogger(CallSheetGeneratorControllerController.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:com.AllenBarr.CallSheetGenerator.CallSheetGeneratorControllerController.java
License:Open Source License
private Contributor makeContributor() { Contributor contributor = new Contributor(); Row contribRow = wbSheet.getRow(contributorSelector.getSelectionModel().getSelectedIndex()); Row headerRow = wbSheet.getRow(0);//from w w w . jav a 2 s .c o m Integer vanIDColumnIndex = 0; Integer lNameColumnIndex = 0; Integer fNameColumnIndex = 0; Integer mNameColumnIndex = 0; Integer suffixColumnIndex = 0; Integer salutationColumnIndex = 0; Integer spouseColumnIndex = 0; Integer mAddressColumnIndex = 0; Integer mCityColumnIndex = 0; Integer mStateColumnIndex = 0; Integer mZipColumnIndex = 0; Integer sexColumnIndex = 0; Integer ageColumnIndex = 0; Integer occupationColumnIndex = 0; Integer employerColumnIndex = 0; Integer phoneColumnIndex = 0; Integer homePhoneColumnIndex = 0; Integer cellPhoneColumnIndex = 0; Integer workPhoneColumnIndex = 0; Integer extColumnIndex = 0; Integer emailColumnIndex = 0; Integer notesColumnIndex = 0; Integer partyColumnIndex = 0; for (Cell cell : headerRow) { if (null != cell.getStringCellValue()) { switch (cell.getStringCellValue()) { case "VANID": vanIDColumnIndex = cell.getColumnIndex(); break; case "LastName": lNameColumnIndex = cell.getColumnIndex(); break; case "FirstName": fNameColumnIndex = cell.getColumnIndex(); break; case "MiddleName": mNameColumnIndex = cell.getColumnIndex(); break; case "Suffix": suffixColumnIndex = cell.getColumnIndex(); break; case "Salutation": salutationColumnIndex = cell.getColumnIndex(); break; case "Spouse": spouseColumnIndex = cell.getColumnIndex(); break; case "mAddress": mAddressColumnIndex = cell.getColumnIndex(); break; case "mCity": mCityColumnIndex = cell.getColumnIndex(); break; case "mState": mStateColumnIndex = cell.getColumnIndex(); break; case "mZip5": mZipColumnIndex = cell.getColumnIndex(); break; case "Sex": sexColumnIndex = cell.getColumnIndex(); break; case "Age": ageColumnIndex = cell.getColumnIndex(); break; case "Occupation": occupationColumnIndex = cell.getColumnIndex(); break; case "Employer": employerColumnIndex = cell.getColumnIndex(); break; case "Phone": phoneColumnIndex = cell.getColumnIndex(); break; case "CellPhone": cellPhoneColumnIndex = cell.getColumnIndex(); break; case "HomePhone": homePhoneColumnIndex = cell.getColumnIndex(); break; case "WorkPhone": workPhoneColumnIndex = cell.getColumnIndex(); break; case "WorkPhoneExt": extColumnIndex = cell.getColumnIndex(); break; case "Email": emailColumnIndex = cell.getColumnIndex(); break; case "Notes": notesColumnIndex = cell.getColumnIndex(); break; case "Party": partyColumnIndex = cell.getColumnIndex(); break; } } } try { contributor.setVANID((int) contribRow.getCell(vanIDColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setVANID(Integer.parseInt(contribRow.getCell(vanIDColumnIndex).getStringCellValue())); } String name = ""; try { name = name.concat(contribRow.getCell(fNameColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { name = name.concat(" " + contribRow.getCell(mNameColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { name = name.concat(" " + contribRow.getCell(lNameColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { name = name.concat(" " + contribRow.getCell(suffixColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } contributor.setName(name); try { contributor.setSalutation(contribRow.getCell(salutationColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setSex(contribRow.getCell(sexColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setParty(contribRow.getCell(partyColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setPhone(contribRow.getCell(phoneColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setPhone(Double.parseDouble(contribRow.getCell(phoneColumnIndex).getStringCellValue())); } try { contributor.setHomePhone(contribRow.getCell(homePhoneColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setHomePhone( Double.parseDouble(contribRow.getCell(homePhoneColumnIndex).getStringCellValue())); } try { contributor.setCellPhone(contribRow.getCell(cellPhoneColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setCellPhone( Double.parseDouble(contribRow.getCell(cellPhoneColumnIndex).getStringCellValue())); } try { contributor.setWorkPhone(contribRow.getCell(workPhoneColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setWorkPhone( Double.parseDouble(contribRow.getCell(workPhoneColumnIndex).getStringCellValue())); } try { contributor.setWorkExtension(contribRow.getCell(extColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor .setWorkExtension(Double.parseDouble(contribRow.getCell(extColumnIndex).getStringCellValue())); } try { contributor.setEmail(contribRow.getCell(emailColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setEmployer(contribRow.getCell(employerColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setOccupation(contribRow.getCell(occupationColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setAge((int) contribRow.getCell(ageColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setAge(Integer.parseInt(contribRow.getCell(ageColumnIndex).getStringCellValue())); } try { contributor.setSpouse(contribRow.getCell(spouseColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setStreetAddress(contribRow.getCell(mAddressColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setCity(contribRow.getCell(mCityColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setState(contribRow.getCell(mStateColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } try { contributor.setZip((int) contribRow.getCell(mZipColumnIndex).getNumericCellValue()); } catch (NullPointerException ex) { } catch (IllegalStateException ex) { contributor.setZip(Integer.parseInt(contribRow.getCell(mZipColumnIndex).getStringCellValue())); } try { contributor.setNotes(contribRow.getCell(notesColumnIndex).getStringCellValue()); } catch (NullPointerException ex) { } ICsvListReader listReader = null; try { try { listReader = new CsvListReader( new FileReader(contributionDirectory.getPath() + "/" + ((String) contributorSelector.getItems() .get(contributorSelector.getSelectionModel().getSelectedIndex())) .replaceAll("[\\D]", "") + ".csv"), CsvPreference.STANDARD_PREFERENCE); listReader.getHeader(true); // skip the header (can't be used with CsvListReader) while ((listReader.read()) != null) { // use different processors depending on the number of columns final CellProcessor[] processors; processors = getProcessors(); final List<Object> customerList = listReader.executeProcessors(processors); Matcher date = dateRegex.matcher(customerList.toString()); Matcher des = descRegex.matcher(customerList.toString()); Matcher amt = amtRegex.matcher(customerList.toString()); date.find(); des.find(); amt.find(); contributor.addDonation( new Donation(new Date(date.group(1)), des.group(1), Double.parseDouble(amt.group(1)))); } } catch (FileNotFoundException | NullPointerException ex) { //Logger.getLogger(CallSheetGeneratorControllerController.class.getName()).log(Level.SEVERE, null, ex); } } catch (IOException ex) { //Logger.getLogger(CallSheetGeneratorControllerController.class.getName()).log(Level.SEVERE, null, ex); } finally { if (listReader != null) { try { listReader.close(); } catch (IOException ex) { //Logger.getLogger(CallSheetGeneratorControllerController.class.getName()).log(Level.SEVERE, null, ex); } } } return contributor; }
From source file:com.asakusafw.testdata.generator.excel.SheetEditor.java
License:Apache License
private void fillRuleTotalCondition(Sheet sheet) { assert sheet != null; Cell value = getCell(sheet, RuleSheetFormat.TOTAL_CONDITION, 0, 1); value.setCellStyle(info.optionsStyle); String[] options = TotalConditionKind.getOptions(); value.setCellValue(options[0]);/*w ww.j a v a 2 s . c o m*/ setExplicitListConstraint(sheet, options, value.getRowIndex(), value.getColumnIndex(), value.getRowIndex(), value.getColumnIndex()); }
From source file:com.asakusafw.testdriver.excel.DefaultExcelRuleExtractor.java
License:Apache License
@Override public String extractName(Row row) throws FormatException { if (row == null) { throw new IllegalArgumentException("row must not be null"); //$NON-NLS-1$ }/*from w w w . jav a2 s . c o m*/ // strict checking for cell type Cell cell = row.getCell(RuleSheetFormat.PROPERTY_NAME.getColumnIndex()); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) { return null; } else if (cell.getCellType() != Cell.CELL_TYPE_STRING) { throw new FormatException(MessageFormat.format( Messages.getString("DefaultExcelRuleExtractor.errorInvalidNameType"), //$NON-NLS-1$ RuleSheetFormat.PROPERTY_NAME.getTitle(), cell.getRowIndex() + 1, cell.getColumnIndex() + 1)); } String name = cell.getStringCellValue(); if (name.isEmpty()) { return null; } return name; }
From source file:com.asakusafw.testdriver.excel.DefaultExcelRuleExtractor.java
License:Apache License
private String getStringCell(Row row, RuleSheetFormat item) throws FormatException { assert row != null; assert item != null; Cell cell = row.getCell(item.getColumnIndex()); if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) { return ""; //$NON-NLS-1$ } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { return cell.getStringCellValue(); }/*from w ww. j a v a2s .c o m*/ throw new FormatException( MessageFormat.format(Messages.getString("DefaultExcelRuleExtractor.errorInvalidStringCell"), //$NON-NLS-1$ item.getTitle(), cell.getRowIndex() + 1, cell.getColumnIndex() + 1)); }
From source file:com.asakusafw.testdriver.excel.ExcelSheetDataModelSource.java
License:Apache License
private Map<PropertyName, Integer> extractProperties() throws IOException { // first row must be property names Row row = sheet.getRow(0);/* w w w .j a v a 2 s. c om*/ if (row == null) { throw new IOException( MessageFormat.format(Messages.getString("ExcelSheetDataModelSource.errorInvalidHeader"), //$NON-NLS-1$ id)); } nextRowNumber = 1; Map<PropertyName, Integer> results = new LinkedHashMap<>(); for (Iterator<Cell> iter = row.cellIterator(); iter.hasNext();) { Cell cell = iter.next(); int type = cell.getCellType(); if (type == Cell.CELL_TYPE_BLANK) { continue; } if (type != Cell.CELL_TYPE_STRING || cell.getStringCellValue().isEmpty()) { throw new IOException( MessageFormat.format(Messages.getString("ExcelSheetDataModelSource.errorInvalidHeaderCell"), //$NON-NLS-1$ id, cell.getColumnIndex() + 1)); } String name = cell.getStringCellValue(); PropertyName property = toPropertyName(cell, name); if (definition.getType(property) == null) { throw new IOException( MessageFormat.format(Messages.getString("ExcelSheetDataModelSource.errorMissingProperty"), //$NON-NLS-1$ definition.getModelClass().getName(), property, id, cell.getColumnIndex() + 1)); } results.put(property, cell.getColumnIndex()); } if (results.isEmpty()) { throw new IOException( MessageFormat.format(Messages.getString("ExcelSheetDataModelSource.errorEmptyProperty"), //$NON-NLS-1$ id)); } return results; }