List of usage examples for org.apache.poi.ss.usermodel Workbook getNumberOfSheets
int getNumberOfSheets();
From source file:coolmap.application.io.external.ImportCOntologyFromXLS.java
@Override public void configure(File... file) { try {/* w w w.j a va 2 s .c o m*/ File inFile = file[0]; String fileNameString = inFile.getName().toLowerCase(); FileInputStream inStream = new FileInputStream(inFile); Workbook workbook = null; if (fileNameString.endsWith("xls")) { workbook = new HSSFWorkbook(inStream); } else if (fileNameString.toLowerCase().endsWith("xlsx")) { workbook = new XSSFWorkbook(inStream); } int sheetCount = workbook.getNumberOfSheets(); String[] sheetNames = new String[sheetCount]; for (int i = 0; i < sheetNames.length; i++) { String sheetName = workbook.getSheetAt(i).getSheetName(); sheetNames[i] = sheetName == null || sheetName.length() == 0 ? "Untitled" : sheetName; } DefaultTableModel tableModels[] = new DefaultTableModel[sheetCount]; Cell cell; Row row; ArrayList<ArrayList<ArrayList<Object>>> previewData = new ArrayList(); for (int si = 0; si < sheetCount; si++) { //The row iterator automatically skips the blank rows //so only need to figure out how many rows to skip; which is nice //columns, not the same though Sheet sheet = workbook.getSheetAt(si); Iterator<Row> rowIterator = sheet.rowIterator(); int ri = 0; ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>(); while (rowIterator.hasNext()) { row = rowIterator.next(); ArrayList<Object> rowData = new ArrayList<>(); for (int j = 0; j < row.getLastCellNum(); j++) { cell = row.getCell(j); try { if (cell == null) { rowData.add(null); } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { rowData.add(null); } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { rowData.add(cell.getStringCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { rowData.add(cell.getNumericCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { rowData.add(cell.getBooleanCellValue()); } else { rowData.add(cell.toString()); } } catch (Exception e) { // CMConsole.logError(" error parsing excel cell: " + cell + ", [" + ri + "," + j + "]"); rowData.add(null); } } data.add(rowData); ri++; if (ri == previewNum) { break; } } //end // System.out.println(data); // now the data is the data // ugh-> this is not a generic importer previewData.add(data); } //end of loop sheets ConfigPanel configPanel = new ConfigPanel(sheetNames, previewData); int returnVal = JOptionPane.showConfirmDialog(CoolMapMaster.getCMainFrame(), configPanel, "Import from Excel: " + inFile.getAbsolutePath(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null); if (returnVal == JOptionPane.OK_OPTION) { proceed = true; inStream.close(); workbook = null; //set parameters inFile = file[0]; rowStart = configPanel.getRowStart(); columnStart = configPanel.getColumnStart(); sheetIndex = configPanel.getSheetIndex(); formatIndex = configPanel.getFormatIndex(); } else { //mark operation cancelled proceed = false; } } catch (Exception e) { } }
From source file:coolmap.application.io.external.ImportDataFromXLS.java
@Override public void configure(File... file) { //need to popup a secondary dialog; this must be done differently try {//from ww w . j a v a 2 s . c o m File inFile = file[0]; String fileNameString = inFile.getName().toLowerCase(); FileInputStream inStream = new FileInputStream(inFile); Workbook workbook = null; if (fileNameString.endsWith("xls")) { workbook = new HSSFWorkbook(inStream); } else if (fileNameString.toLowerCase().endsWith("xlsx")) { workbook = new XSSFWorkbook(inStream); } int sheetCount = workbook.getNumberOfSheets(); String[] sheetNames = new String[sheetCount]; for (int i = 0; i < sheetNames.length; i++) { String sheetName = workbook.getSheetAt(i).getSheetName(); sheetNames[i] = sheetName == null || sheetName.length() == 0 ? "Untitled" : sheetName; } //also need to get the top 100 rows + all columns DefaultTableModel tableModels[] = new DefaultTableModel[sheetCount]; Cell cell; Row row; ArrayList<ArrayList<ArrayList<Object>>> previewData = new ArrayList(); for (int si = 0; si < sheetCount; si++) { //The row iterator automatically skips the blank rows //so only need to figure out how many rows to skip; which is nice //columns, not the same though Sheet sheet = workbook.getSheetAt(si); Iterator<Row> rowIterator = sheet.rowIterator(); int ri = 0; ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>(); while (rowIterator.hasNext()) { row = rowIterator.next(); ArrayList<Object> rowData = new ArrayList<>(); for (int j = 0; j < row.getLastCellNum(); j++) { cell = row.getCell(j); try { if (cell == null) { rowData.add(null); } else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) { rowData.add(null); } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) { rowData.add(cell.getStringCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { rowData.add(cell.getNumericCellValue()); } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { rowData.add(cell.getBooleanCellValue()); } else { rowData.add(cell.toString()); } } catch (Exception e) { // CMConsole.logError(" error parsing excel cell: " + cell + ", [" + ri + "," + j + "]"); rowData.add(null); } } data.add(rowData); ri++; if (ri == previewNum) { break; } } //end // System.out.println(data); // now the data is the data // ugh-> this is not a generic importer previewData.add(data); } //end of iterating all sheets ConfigPanel configPanel = new ConfigPanel(sheetNames, previewData); //int returnVal = JOptionPane.showMessageDialog(CoolMapMaster.getCMainFrame(), configPanel); int returnVal = JOptionPane.showConfirmDialog(CoolMapMaster.getCMainFrame(), configPanel, "Import from Excel: " + inFile.getAbsolutePath(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null); if (returnVal == JOptionPane.OK_OPTION) { proceed = true; inStream.close(); workbook = null; //set parameters inFile = file[0]; importOntology = configPanel.getImportOntology(); rowStart = configPanel.getRowStart(); columnStart = configPanel.getColumnStart(); sheetIndex = configPanel.getSheetIndex(); } else { //mark operation cancelled proceed = false; } } catch (Exception e) { CMConsole.logError(" failed to import numeric matrix data from: " + file); e.printStackTrace(); } }
From source file:de.bund.bfr.knime.pmm.common.XLSReader.java
License:Open Source License
public List<String> getSheets(File file) throws Exception { List<String> sheets = new ArrayList<>(); Workbook workbook = getWorkbook(file); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { sheets.add(workbook.getSheetName(i)); }/* w w w. java2 s. com*/ return sheets; }
From source file:de.ingrid.iplug.excel.service.SheetsService.java
License:EUPL
/** * Create sheets.//w ww . ja va 2s . c o m * * @param inputStream * @return Created sheets. * @throws IOException */ public static Sheets createSheets(final InputStream inputStream) throws IOException { // sheets final Sheets sheets = new Sheets(); // create workbook final Workbook workbook = new HSSFWorkbook(inputStream); final FormulaEvaluator eval = new HSSFFormulaEvaluator((HSSFWorkbook) workbook); for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) { final org.apache.poi.ss.usermodel.Sheet poiSheet = workbook.getSheetAt(sheetNum); // ingrid sheet final Sheet sheet = new Sheet(); sheet.setSheetIndex(sheetNum); sheets.addSheet(sheet); final Values values = new Values(); sheet.setValues(values); for (final org.apache.poi.ss.usermodel.Row poiRow : poiSheet) { boolean hasValues = false; final Map<Point, Comparable<? extends Object>> valuesInCell = new HashMap<Point, Comparable<? extends Object>>(); for (final Cell poiCell : poiRow) { Comparable<? extends Object> value = null; switch (poiCell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: value = new Boolean(poiCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(poiCell)) { value = getFormattedDateString(poiCell); } else { value = new Double(poiCell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: value = poiCell.getStringCellValue(); break; case Cell.CELL_TYPE_FORMULA: value = calculateFormula(poiCell, eval); break; default: value = ""; break; } // trim strings if (value instanceof String) { value = ((String) value).trim(); } // only add if at least one value does exist in row if (!value.equals("")) { hasValues = true; // ingrid column if (sheet.getColumn(poiCell.getColumnIndex()) == null) { final Column column = new Column(poiCell.getColumnIndex()); sheet.addColumn(column); } } // ingrid point and value final Point point = new Point(poiCell.getColumnIndex(), poiCell.getRowIndex()); valuesInCell.put(point, value); } // ingrid row // ! only add if at least one value does exist if (hasValues) { final Row row = new Row(poiRow.getRowNum()); sheet.addRow(row); for (final Point point : valuesInCell.keySet()) { // if (sheet.getColumn(point.getX()) != null) { values.addValue(point, valuesInCell.get(point)); } } } } } return sheets; }
From source file:de.interactive_instruments.ShapeChange.SBVR.SbvrRuleLoader.java
License:Open Source License
/** * @param sbvrXls// w ww . j a va2 s . c o m * @return mapping of schema package name to SBVR rules that apply to * classes in this schema * * <ul> * <li>key: class name</li> * <li>value: mapping of schema package name to SBVR rule info * <ul> * <li>key: schema package name ( * {@value #UNSPECIFIED_SCHEMA_PACKAGE_NAME} if no schema package * name has been provided)</li> * <li>value: list of SBVR rules that apply to classes in that * schema (the list is sorted according to lexical order on a) the * class name and b) the rule text)</li> * </ul> * </ul> */ private TreeMap<String, TreeMap<String, List<SbvrRuleInfo>>> parseSBVRRuleInfos(Workbook sbvrXls) { TreeMap<String, TreeMap<String, List<SbvrRuleInfo>>> rules = new TreeMap<String, TreeMap<String, List<SbvrRuleInfo>>>(); if (sbvrXls == null) return null; Sheet rulesSheet = null; for (int i = 0; i < sbvrXls.getNumberOfSheets(); i++) { String sheetName = sbvrXls.getSheetName(i); if (sheetName.equalsIgnoreCase("Constraints")) { rulesSheet = sbvrXls.getSheetAt(i); break; } } if (rulesSheet == null) { result.addError(this, 3); return null; } // read header row to determine which columns contain relevant // information Map<String, Integer> fieldIndexes = new HashMap<String, Integer>(); Row header = rulesSheet.getRow(rulesSheet.getFirstRowNum()); if (header == null) { result.addError(this, 4); return null; } boolean classNameFound = false; boolean commentsFound = false; boolean ruleNameFound = false; boolean ruleTextFound = false; boolean schemaPackageFound = false; for (short i = header.getFirstCellNum(); i < header.getLastCellNum(); i++) { Cell c = header.getCell(i, Row.RETURN_BLANK_AS_NULL); if (c == null) { // this is allowed } else { String value = c.getStringCellValue(); if (value.equalsIgnoreCase(SbvrRuleInfo.CLASS_COLUMN_NAME)) { fieldIndexes.put(SbvrRuleInfo.CLASS_COLUMN_NAME, (int) i); classNameFound = true; } else if (value.equalsIgnoreCase(SbvrRuleInfo.COMMENT_COLUMN_NAME)) { fieldIndexes.put(SbvrRuleInfo.COMMENT_COLUMN_NAME, (int) i); commentsFound = true; } else if (value.equalsIgnoreCase(SbvrRuleInfo.SCHEMA_PACKAGE_COLUMN_NAME)) { fieldIndexes.put(SbvrRuleInfo.SCHEMA_PACKAGE_COLUMN_NAME, (int) i); schemaPackageFound = true; } else if (value.equalsIgnoreCase(SbvrRuleInfo.RULE_TEXT_COLUMN_NAME)) { fieldIndexes.put(SbvrRuleInfo.RULE_TEXT_COLUMN_NAME, (int) i); ruleTextFound = true; } else if (value.equalsIgnoreCase(SbvrRuleInfo.RULE_NAME_COLUMN_NAME)) { fieldIndexes.put(SbvrRuleInfo.RULE_NAME_COLUMN_NAME, (int) i); ruleNameFound = true; } } } // if (fieldIndexes.size() != 5) { if (!ruleNameFound && !ruleTextFound) { // log message that required fields were not found result.addError(this, 5); return null; } /* * Read rule content */ for (int i = rulesSheet.getFirstRowNum() + 1; i <= rulesSheet.getLastRowNum(); i++) { Row r = rulesSheet.getRow(i); int rowNumber = i + 1; if (r == null) { // ignore empty rows continue; } SbvrRuleInfo sri = new SbvrRuleInfo(); // get rule name (required) Cell c = r.getCell(fieldIndexes.get(SbvrRuleInfo.RULE_NAME_COLUMN_NAME), Row.RETURN_BLANK_AS_NULL); if (c == null) { // log message result.addWarning(this, 6, "" + rowNumber); continue; } else { String cellValue = c.getStringCellValue(); if (cellValue != null) { if (cellValue.contains(":")) { sri.setName(cellValue.substring(cellValue.lastIndexOf(":") + 1)); } else { sri.setName(cellValue); } } } // get rule text (required) c = r.getCell(fieldIndexes.get(SbvrRuleInfo.RULE_TEXT_COLUMN_NAME), Row.RETURN_BLANK_AS_NULL); if (c == null) { // log message result.addWarning(this, 7, "" + rowNumber); continue; } else { sri.setText(c.getStringCellValue()); } // get comment (optional) if (commentsFound) { c = r.getCell(fieldIndexes.get(SbvrRuleInfo.COMMENT_COLUMN_NAME), Row.RETURN_BLANK_AS_NULL); if (c != null) { sri.setComment(c.getStringCellValue()); } } // get schema package (optional) if (schemaPackageFound) { c = r.getCell(fieldIndexes.get(SbvrRuleInfo.SCHEMA_PACKAGE_COLUMN_NAME), Row.RETURN_BLANK_AS_NULL); if (c == null) { sri.setSchemaPackageName(UNSPECIFIED_SCHEMA_PACKAGE_NAME); } else { sri.setSchemaPackageName(c.getStringCellValue()); } } /* * get class name (optional when loading from excel because later we * can still try parsing it from the rule text) */ if (classNameFound) { c = r.getCell(fieldIndexes.get(SbvrRuleInfo.CLASS_COLUMN_NAME), Row.RETURN_BLANK_AS_NULL); if (c == null) { /* * then after this we'll try to parse the class name from * the rule text */ } else { sri.setClassName(c.getStringCellValue()); } } if (sri.getClassName() == null) { /* * try parsing the main class name from the rule text */ result.addInfo(this, 10, sri.getName()); String mainClassName = parseClassNameFromRuleText(sri.getText()); if (mainClassName == null) { result.addWarning(this, 8, sri.getName()); continue; } else { sri.setClassName(mainClassName); } } List<SbvrRuleInfo> rulesList; TreeMap<String, List<SbvrRuleInfo>> rulesBySchemaPackageName; if (rules.containsKey(sri.getClassName())) { rulesBySchemaPackageName = rules.get(sri.getClassName()); if (rulesBySchemaPackageName.containsKey(sri.getSchemaPackageName())) { rulesList = rulesBySchemaPackageName.get(sri.getSchemaPackageName()); } else { rulesList = new ArrayList<SbvrRuleInfo>(); rulesBySchemaPackageName.put(sri.getSchemaPackageName(), rulesList); } } else { rulesBySchemaPackageName = new TreeMap<String, List<SbvrRuleInfo>>(); rules.put(sri.getClassName(), rulesBySchemaPackageName); rulesList = new ArrayList<SbvrRuleInfo>(); rulesBySchemaPackageName.put(sri.getSchemaPackageName(), rulesList); } rulesList.add(sri); } // now sort all lists contained in the map for (TreeMap<String, List<SbvrRuleInfo>> rulesBySchemaPackageName : rules.values()) { for (List<SbvrRuleInfo> rulesList : rulesBySchemaPackageName.values()) { Collections.sort(rulesList, new Comparator<SbvrRuleInfo>() { @Override public int compare(SbvrRuleInfo o1, SbvrRuleInfo o2) { int classNameComparison = o1.getClassName().compareTo(o2.getClassName()); if (classNameComparison != 0) { return classNameComparison; } else { return o1.getText().compareTo(o2.getText()); } } }); } } return rules; }
From source file:de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoExcelTransformerTest.java
License:Open Source License
/** * Test method for {@link de.iteratec.iteraplan.businesslogic.exchange.nettoExport.NettoExcelTransformer#transform(java.util.List, java.io.OutputStream, de.iteratec.iteraplan.model.TypeOfBuildingBlock)}. *///ww w . j av a 2 s .c om @Test public void testTransform() { NettoTransformer inst2007op = NettoExcelTransformer.newInstance(createSimpleOverviewPageTableStructure(), NettoExcelTransformer.ExcelVersion.EXCEL_VERSION_2007); NettoTransformer inst2003sr = NettoExcelTransformer.newInstance( createSimpleSpreadsheetReportTableStructure(), NettoExcelTransformer.ExcelVersion.EXCEL_VERSION_2003); assertNotNull("Can't create netto transformer for overview page table structure with excel 2007", inst2007op); assertNotNull("Can't create netto transformer for spreadsheet report table structure with excel 2003", inst2003sr); List<BuildingBlock> sourceList = new ArrayList<BuildingBlock>(); String firstInfstrElemName = "Infrastructure Element for UnitTest"; String lastInfstrElemDesc = "Last comment"; sourceList.add(testDataHelper.createInfrastructureElement(firstInfstrElemName, "Some comment")); sourceList.add( testDataHelper.createInfrastructureElement("Another Infrastructure Element", "Some more comment")); sourceList.add(testDataHelper.createInfrastructureElement("Yet another Infrastructure Element", "Even more comment")); sourceList .add(testDataHelper.createInfrastructureElement("Last Infrastructure Element", lastInfstrElemDesc)); ByteArrayOutputStream bufferA = new ByteArrayOutputStream(); ByteArrayOutputStream bufferB = new ByteArrayOutputStream(); inst2007op.transform(sourceList, bufferA, TypeOfBuildingBlock.INFRASTRUCTUREELEMENT); inst2003sr.transform(sourceList, bufferB, TypeOfBuildingBlock.INFRASTRUCTUREELEMENT); InputStream in2007 = new ByteArrayInputStream(bufferA.toByteArray()); InputStream in2003 = new ByteArrayInputStream(bufferB.toByteArray()); // Excel version try { assertTrue("Generated excel file is not version 2007.", POIXMLDocument.hasOOXMLHeader(in2007)); assertTrue("Generated excel file is not version 2003.", POIFSFileSystem.hasPOIFSHeader(in2003)); } catch (IOException e1) { fail("Can't read excel header from buffers."); } Workbook workbook2007 = null; Workbook workbook2003 = null; try { workbook2007 = WorkbookFactory.create(in2007); workbook2003 = WorkbookFactory.create(in2003); } catch (Exception e) { fail("Can't open generated excel workbook."); } assertNotNull("Could not create excel workbook instance from generated output (excel 2007).", workbook2007); assertNotNull("Could not create excel workbook instance from generated output (excel 2003).", workbook2003); assertSame("Number of sheets is not equal 1 (excel 2007).", Integer.valueOf(workbook2007.getNumberOfSheets()), Integer.valueOf(1)); assertSame("Number of sheets is not equal 1 (excel 2003).", Integer.valueOf(workbook2003.getNumberOfSheets()), Integer.valueOf(1)); String stringCellValue2007 = null; String stringCellValue2003 = null; String stringCellValueDesc2007 = null; String stringCellValueDesc2003 = null; Sheet sheet2007; Sheet sheet2003; Row firstDataRow2007; Row firstDataRow2003; Row lastDataRow2007; Row lastDataRow2003; Cell nameCell2007; Cell nameCell2003; Cell descCell2007; Cell descCell2003; try { sheet2007 = workbook2007.getSheetAt(0); sheet2003 = workbook2003.getSheetAt(0); firstDataRow2007 = sheet2007.getRow(1); firstDataRow2003 = sheet2003.getRow(1); lastDataRow2007 = sheet2007.getRow(4); lastDataRow2003 = sheet2003.getRow(4); nameCell2007 = firstDataRow2007.getCell(0); nameCell2003 = firstDataRow2003.getCell(0); descCell2007 = lastDataRow2007.getCell(1); descCell2003 = lastDataRow2003.getCell(1); stringCellValue2007 = nameCell2007.getStringCellValue(); stringCellValue2003 = nameCell2003.getStringCellValue(); stringCellValueDesc2007 = descCell2007.getStringCellValue(); stringCellValueDesc2003 = descCell2003.getStringCellValue(); } catch (Exception e) { fail("Wrong structure inside workbook/sheet/row."); } assertEquals( "String in generated excel 2007 workbook does not match the string from the first element in the List of BuildingBlocks.", stringCellValue2007, firstInfstrElemName); assertEquals( "String in generated excel 2003 workbook does not match the string from the first element in the List of BuildingBlocks.", stringCellValue2003, firstInfstrElemName); assertEquals( "String in generated excel 2007 workbook does not match the string from the last element in the List of BuildingBlocks.", stringCellValueDesc2007, lastInfstrElemDesc); assertEquals( "String in generated excel 2003 workbook does not match the string from the last element in the List of BuildingBlocks.", stringCellValueDesc2003, lastInfstrElemDesc); }
From source file:de.iteratec.iteraplan.businesslogic.exchange.timeseriesExcel.exporter.TimeseriesExcelExportServiceImpl.java
License:Open Source License
private void adjustColumnWidths(Workbook workbook) { for (int i = 0; i < workbook.getNumberOfSheets(); i++) { Sheet sheet = workbook.getSheetAt(i); if (!AbstractIntroSheetGenerator.INTRO_SHEET_NAME.equals(sheet.getSheetName())) { sheet.autoSizeColumn(TimeseriesExcelImporter.BB_COL_NO, false); sheet.setColumnWidth(TimeseriesExcelImporter.DATE_COL_NO, DATE_COL_WIDTH); sheet.autoSizeColumn(TimeseriesExcelImporter.VALUE_COL_NO, false); }//from www .java 2 s.c om } }
From source file:de.iteratec.iteraplan.businesslogic.exchange.timeseriesExcel.exporter.TimeseriesExcelExportServiceImplTest.java
License:Open Source License
private void assertTemplateSheets(Workbook workbook) { int actualNumberOfSheets = workbook.getNumberOfSheets(); assertEquals(4, actualNumberOfSheets); for (int i = 0; i < actualNumberOfSheets; i++) { Sheet sheet = workbook.getSheetAt(i); assertEquals(EXPECTED_SHEET_NAMES[i], sheet.getSheetName()); }/* w ww .ja va 2s. c o m*/ }
From source file:de.iteratec.iteraplan.businesslogic.exchange.timeseriesExcel.importer.TimeseriesExcelImporter.java
License:Open Source License
/** * Imports data from the workbook into the database. * @return True if the import was successful, false otherwise. *//* w w w .j a va2s . c om*/ public boolean importExcel(Workbook workbook) { assert (workbook != null); for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) { Sheet sheet = workbook.getSheetAt(sheetIndex); if (!AbstractIntroSheetGenerator.INTRO_SHEET_NAME.equals(sheet.getSheetName())) { importTimeseriesSheet(sheet); } } return errorMessages.isEmpty(); }
From source file:de.teststory.jspwiki.worksheetplugin.WorksheetPlugin.java
License:Apache License
/** * Try to find sheet by id, then by name. Default is first sheet. * /* w w w . j a v a 2 s. c o m*/ * @param wb * @param sheetId * @param sheetName * @return */ protected Sheet findSheet(Workbook wb, int sheetId, String sheetName) { Sheet ret = null; // sheet ID if (sheetId >= 0 && sheetId < wb.getNumberOfSheets()) { ret = wb.getSheetAt(sheetId); } // sheet name if (ret == null && sheetName != null) { ret = wb.getSheet(sheetName); } if (ret == null && wb.getNumberOfSheets() > 0) { ret = wb.getSheetAt(0); } return ret; }