Example usage for org.apache.poi.ss.usermodel Workbook getNumberOfSheets

List of usage examples for org.apache.poi.ss.usermodel Workbook getNumberOfSheets

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Workbook getNumberOfSheets.

Prototype

int getNumberOfSheets();

Source Link

Document

Get the number of spreadsheets in the workbook

Usage

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.PostReviewQAReportGenerator.java

License:Apache License

/**
 * Create Report File.//from   ww w.ja v a2s .co  m
 */
protected File getFile(String p_reportType, Job p_job, Workbook p_workBook) {
    String langInfo = null;
    // If the Workbook has only one sheet, the report name should contain
    // language pair info, such as en_US_de_DE.
    if (p_workBook != null && p_workBook.getNumberOfSheets() == 1) {
        Sheet sheet = p_workBook.getSheetAt(0);
        String srcLang = null, trgLang = null;
        if (p_job != null) {
            srcLang = p_job.getSourceLocale().toString();
        }
        if (trgLang == null) {
            trgLang = sheet.getSheetName();
        }
        if (srcLang != null && trgLang != null) {
            langInfo = srcLang + "_" + trgLang;
        }
    }

    return ReportHelper.getReportFile(p_reportType, p_job, ReportConstants.EXTENSION_XLSX, langInfo);
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.ReviewersCommentsReportGenerator.java

License:Apache License

/**
 * Create Report File.// w w  w  .  j av  a 2 s  .  co m
 */
protected File getFile(String p_reportType, Job p_job, Workbook p_workBook) {
    String langInfo = null;
    // If the Workbook has only one sheet, the report name should contain language pair info, such as en_US_de_DE.
    if (p_workBook != null && p_workBook.getNumberOfSheets() == 1) {
        Sheet sheet = p_workBook.getSheetAt(0);
        String srcLang = null, trgLang = null;
        if (p_job != null) {
            srcLang = p_job.getSourceLocale().toString();
        }
        if (trgLang == null) {
            trgLang = sheet.getSheetName();
        }
        if (srcLang != null && trgLang != null) {
            langInfo = srcLang + "_" + trgLang;
        }
    }

    return ReportHelper.getReportFile(p_reportType, p_job, ReportConstants.EXTENSION_XLSX, langInfo);
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.reports.generator.ScorecardReportGenerator.java

License:Apache License

/**
 * Create Report File./*from   ww  w.jav a 2  s. c om*/
 */
protected File getFile(String p_reportType, Job p_job, Workbook p_workBook) {
    String langInfo = null;
    // If the Workbook has only one sheet, the report name should contain language pair info, such as en_US_de_DE.
    if (p_workBook != null && p_workBook.getNumberOfSheets() == 1) {
        Sheet sheet = p_workBook.getSheetAt(0);
        String srcLang = null, trgLang = null;
        if (p_job != null) {
            srcLang = p_job.getSourceLocale().toString();
        }
        if (srcLang == null) {
            Row languageInfoRow = sheet.getRow(1);
            if (languageInfoRow != null) {
                srcLang = languageInfoRow.getCell(0).getStringCellValue();
                srcLang = srcLang.substring(srcLang.indexOf("[") + 1, srcLang.indexOf("]"));
                trgLang = languageInfoRow.getCell(1).getStringCellValue();
                trgLang = trgLang.substring(trgLang.indexOf("[") + 1, trgLang.indexOf("]"));
            } else {
                Row dataRow = sheet.getRow(sheet.getLastRowNum());
                if (dataRow != null) {
                    try {
                        long jobId = (long) dataRow.getCell(0).getNumericCellValue();
                        Job job = ServerProxy.getJobHandler().getJobById(jobId);
                        srcLang = job.getSourceLocale().toString();
                    } catch (Exception e) {
                    }

                }
            }
        }
        if (trgLang == null) {
            trgLang = sheet.getSheetName();
        }
        if (srcLang != null && trgLang != null) {
            langInfo = srcLang + "_" + trgLang;
        }
    }

    return ReportHelper.getReportFile(p_reportType, p_job, ReportConstants.EXTENSION_XLSX, langInfo);
}

From source file:com.hust.zsuper.DealWithPatent.ExcelToMySQL.java

License:Open Source License

public void addWorkbook(Connection conn, Workbook workbook) throws SQLException {
    final int numberOfSheets = workbook.getNumberOfSheets();
    for (int sheetIndex = 0; sheetIndex < numberOfSheets; sheetIndex++) {
        final Sheet sheet = workbook.getSheetAt(sheetIndex);
        if (filter.accept(sheet)) {
            addTableFromSheet(conn, sheet);
        }//  ww  w. j  av  a  2s . c  o  m
    }
}

From source file:com.ibm.db2j.GExcel.java

License:Open Source License

/**
 * Finds the spreadsheet defined by the name given in parameter and return it if found.
 * Else return null./*from ww w .  jav  a2s. c o m*/
 * 
 * @param workbook
 * @param spreadsheetName
 * @return the spreadsheet
 * @throws SQLException
 */
private Sheet findSpreadsheet(Workbook workbook, String spreadsheetName) throws SQLException {
    boolean sheetFound = false;
    Sheet sheetTmp = null;
    for (int i = 0; i < workbook.getNumberOfSheets() && !sheetFound; ++i) {
        sheetTmp = workbook.getSheetAt(i);

        if (sheetTmp.getSheetName().equals(spreadsheetName))
            sheetFound = true;
    }

    if (sheetFound == false && sheet == null) {
        throw new SQLException("The file does not contain a spreadsheet named : " + spreadsheetName);
    }

    return sheetTmp;
}

From source file:com.inspiracode.nowgroup.scspro.xl.source.ExcelFile.java

License:Open Source License

public List<LogMessage> validateFieldNames() {
    List<LogMessage> result = new ArrayList<LogMessage>();
    FileInputStream fis = null;//from   w w w. ja v a 2 s .  c om
    Workbook wb = null;
    try {
        fis = new FileInputStream(file);
        // Get the workbook instance for XLS file

        if (".xls".equals(file.getName().substring(file.getName().length() - 4))) {
            wb = new HSSFWorkbook(fis);
        } else if ("xlsx".equals(file.getName().substring(file.getName().length() - 4))) {
            wb = new XSSFWorkbook(fis);
        } else {
            throw new IllegalArgumentException("Received file does not have a standard excel extension.");
        }

        // Get each sheet from the workbook
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            result.addAll(validateFieldNames(wb.getSheetAt(i)));
        }

    } catch (Exception e) {
        log.error("Error al validar campos en excel: [" + e.getMessage() + "]", e);
        result.add(new LogMessage("Validacin de encabezados",
                "Error al validar campos en excel: [" + e.getMessage() + "]"));
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            wb.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    return result;
}

From source file:com.inspiracode.nowgroup.scspro.xl.source.ExcelFile.java

License:Open Source License

public List<LogMessage> readPurchaseOrders() {

    List<LogMessage> result = new ArrayList<LogMessage>();
    FileInputStream fis = null;//  w ww.  j a v a2 s  . c o  m
    Workbook wb = null;
    try {
        fis = new FileInputStream(file);
        // Get the workbook instance for XLS file

        if (".xls".equals(file.getName().substring(file.getName().length() - 4))) {
            wb = new HSSFWorkbook(fis);
        } else if ("xlsx".equals(file.getName().substring(file.getName().length() - 4))) {
            wb = new XSSFWorkbook(fis);
        } else {
            throw new IllegalArgumentException("Received file does not have a standard excel extension.");
        }

        // Get each sheet from the workbook
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            result.addAll(readPurchaseOrders(wb.getSheetAt(i)));
        }

    } catch (Exception e) {
        log.error("Error al validar campos en excel: [" + e.getMessage() + "]", e);
        result.add(new LogMessage("Validacin de ordenes de compra",
                "Error al validar las Ordenes de compra: [" + e.getMessage() + "]"));
    } finally {
        try {
            fis.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        try {
            wb.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    return result;
}

From source file:com.jmc.jfxxlsdiff.task.GetWorkSheetNames.java

@Override
protected ObservableList<String> call() throws Exception {
    logger.log(Level.INFO, "call()");
    updateProgress(-1L, 1L);/*from w w  w  . java  2  s.c  o  m*/

    ObservableList<String> ol = null;
    Workbook wb = null;

    try {
        logger.log(Level.INFO, "creating workbook");
        wb = WorkbookFactory.create(xlsFile);
    } catch (IOException | InvalidFormatException ex) {
        logger.log(Level.SEVERE, null, ex);
        updateProgress(0L, 1L);
        throw ex;
    }

    if (wb != null) {
        logger.log(Level.INFO, "get sheet names");

        ol = FXCollections.observableArrayList();
        for (int i = 0; i < wb.getNumberOfSheets(); i++) {
            ol.add(wb.getSheetAt(i).getSheetName());
        }

        logger.log(Level.INFO, "got {0} sheet names", ol.size());
        updateProgress(1L, 1L);
    } else {
        logger.log(Level.WARNING, "workbook *is* null");
        updateProgress(0L, 1L);
    }

    return ol;
}

From source file:com.lnganalysis.fileupload.util.ReadExcelFile.java

public Map<String, List> readExcelData(Workbook wb, List<Tab> tabsList) {
    logger.info("Class - ReadExcelFile - readExcelData()");
    int sheetCount = wb.getNumberOfSheets();
    Map<String, List> populatedData = new HashMap<String, List>();

    for (int i = 0; i < sheetCount; i++) {
        Sheet sheet = wb.getSheetAt(i);// ww w . java  2 s .  c o  m
        if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.EXPLORATION.toString())) {
            Tab tab = new Tab();
            List<Exploration> explorationList = populateExplorationData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), explorationList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.REFINERIES.toString())) {
            Tab tab = new Tab();
            List<Refinery> refineryList = populateRefineryData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), refineryList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.STORAGE.toString())) {
            Tab tab = new Tab();
            List<Storage> storageList = populateStorageData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), storageList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.LNG.toString())) {
            Tab tab = new Tab();
            List<Lng> lngList = populateLngData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), lngList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.PIPELINES.toString())) {
            Tab tab = new Tab();
            List<PipeLine> pipeLineList = populatePipeLinesData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), pipeLineList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.CRUDEOIL.toString())) {
            Tab tab = new Tab();
            List<CrudeOil> crudeOilList = populateCrudeOilData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), crudeOilList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.NATURALGAS.toString())) {
            Tab tab = new Tab();
            List<NaturalGas> naturalGasList = populateNaturalGasData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName().toLowerCase(), naturalGasList);

        } else if (sheet.getSheetName().toLowerCase().equalsIgnoreCase(LngData.SUPPLYDEMAND.toString())) {
            Tab tab = new Tab();
            List<SupplyDemand> supplyDemandList = populateSupplyDemandData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName(), supplyDemandList);
        } else if (sheet.getSheetName().equalsIgnoreCase(LngData.CONTRACTS.toString())) {
            Tab tab = new Tab();
            List<Contracts> contractsList = populateContractsData(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName(), contractsList);
        } else if (sheet.getSheetName().equalsIgnoreCase(LngData.PRODUCTION_COMPANY_OILGAS.toString())) {
            Tab tab = new Tab();
            List<CompanyOilGas> companyOilGasList = populateProductionCompanyOilGas(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName(), companyOilGasList);
        } else if (sheet.getSheetName().equalsIgnoreCase(LngData.SMALLSCALELNG.toString())) {
            Tab tab = new Tab();
            List<SmallScaleLng> smallScaleLngList = populateSmallScaleLng(sheet, tab);
            if (tab != null && tab.getTotalRecords() > 0)
                tabsList.add(tab);
            populatedData.put(sheet.getSheetName(), smallScaleLngList);
        }
    }

    return populatedData;
}

From source file:com.movielabs.availslib.AvailSS.java

License:Open Source License

/**
 * Dump the contents (sheet-by-sheet) of an Excel spreadsheet
 * @param file name of the Excel .xlsx spreadsheet
 * @throws Exception if any error is encountered (e.g. non-existant or corrupt file)
 *//*w  ww .j a v a  2 s  .c o m*/
public static void dumpFile(String file) throws Exception {
    Workbook wb = new XSSFWorkbook(new FileInputStream(file));
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        System.out.println("Sheet <" + wb.getSheetName(i) + ">");
        for (Row row : sheet) {
            System.out.println("rownum: " + row.getRowNum());
            for (Cell cell : row) {
                System.out.println("   | " + cell.toString());
            }
        }
    }
    wb.close();
}