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:org.dbunit.dataset.excel.XlsDataSet.java

License:Open Source License

/**
 * Creates a new XlsDataSet object that loads the specified Excel document.
 *//*from  w w w  . j  a va2 s  .c o m*/
public XlsDataSet(InputStream in) throws IOException, DataSetException {
    _tables = super.createTableNameMap();

    Workbook workbook;
    try {
        workbook = WorkbookFactory.create(in);
    } catch (InvalidFormatException e) {
        throw new IOException(e);
    }

    int sheetCount = workbook.getNumberOfSheets();
    for (int i = 0; i < sheetCount; i++) {
        ITable table = new XlsTable(workbook.getSheetName(i), workbook.getSheetAt(i));
        _tables.add(table.getTableMetaData().getTableName(), table);
    }
}

From source file:org.drools.decisiontable.DecisionTableProviderImpl.java

License:Apache License

public List<String> loadWorkSheetsFromResource(Resource resource, DecisionTableConfiguration configuration) {
    List<String> drls = new ArrayList<String>(configuration.getRuleTemplateConfigurations().size());

    SpreadsheetCompiler compiler = new SpreadsheetCompiler();

    Workbook w;
    String sheetName = "";
    try {/*from w  w  w. ja v  a 2  s .  co m*/
        w = WorkbookFactory.create(resource.getInputStream());
        for (int i = 0; i < w.getNumberOfSheets(); i++) {
            org.apache.poi.ss.usermodel.Sheet sheet = w.getSheetAt(i);
            sheetName = sheet.getSheetName();
            logger.info("Compiling sheet {} ", sheetName);
            String compiled = compiler.compile(resource, sheetName);
            drls.add(compiled);
        }
        /*for (Sheet sheet : w.getSheets()) {
           sheetName = sheet.getName();
           log.info("Compiling sheet {} from {}", sheetName, xls.getAbsolutePath());
           compileSheet(kbuilder, compiledFiles, xls, compiler, sheetName, in);
           IOUtils.closeQuietly(in);
        }*/
    } catch (Exception e) {
        logger.error("Cannot open ", e);
    }

    return drls;
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.XLSBuilderAttributesTest.java

License:Apache License

@BeforeClass
public static void setBeforeClass() throws Exception {
    final String xml = loadResource(XLSBuilderAttributesTest.class.getResourceAsStream("Attributes.gdst"));

    final GuidedDecisionTable52 dtable = GuidedDTXMLPersistence.getInstance().unmarshal(xml);

    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, makeDMO()).build();
    final Workbook workbook = buildResult.getWorkbook();

    assertEquals(1, workbook.getNumberOfSheets());
    sheet = workbook.iterator().next();/*  w ww  .  j  ava2s .  c o  m*/
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.XLSBuilderCarsInCityTest.java

License:Apache License

@BeforeClass
public static void setUpBefore() throws Exception {
    final String xml = loadResource(XLSBuilderCarsInCityTest.class.getResourceAsStream("cars in city.gdst"));

    final GuidedDecisionTable52 dtable = GuidedDTXMLPersistence.getInstance().unmarshal(xml);

    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, makeDMO()).build();
    final Workbook workbook = buildResult.getWorkbook();

    assertEquals(1, workbook.getNumberOfSheets());
    sheet = workbook.iterator().next();/*from  www .  j  a  v a2  s .com*/
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.XLSBuilderExampleTest.java

License:Apache License

@BeforeClass
public static void setUp() throws Exception {
    final String xml = loadResource(XLSBuilderExampleTest.class.getResourceAsStream("Example.gdst"));

    final GuidedDecisionTable52 dtable = GuidedDTXMLPersistence.getInstance().unmarshal(xml);

    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, makeDMO()).build();
    final Workbook workbook = buildResult.getWorkbook();

    assertEquals(1, workbook.getNumberOfSheets());
    sheet = workbook.iterator().next();/*from   ww  w.  jav a  2s.  co m*/
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.XLSBuilderGoodOldPricingLoansTest.java

License:Apache License

@BeforeClass
public static void setUp() throws Exception {
    final String xml = loadResource(
            XLSBuilderGoodOldPricingLoansTest.class.getResourceAsStream("Pricing loans.gdst"));

    final GuidedDecisionTable52 dtable = GuidedDTXMLPersistence.getInstance().unmarshal(xml);

    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, makeDMO()).build();
    final Workbook workbook = buildResult.getWorkbook();

    assertEquals(1, workbook.getNumberOfSheets());
    sheet = workbook.iterator().next();/*from   ww  w.  j a  v a2 s  . c  o  m*/
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.XLSBuilderInsertTest.java

License:Apache License

@BeforeClass
public static void setUp() throws Exception {
    final String xml = loadResource(XLSBuilderInsertTest.class.getResourceAsStream("Insert.gdst"));

    final GuidedDecisionTable52 dtable = GuidedDTXMLPersistence.getInstance().unmarshal(xml);

    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, makeDMO()).build();
    final Workbook workbook = buildResult.getWorkbook();

    assertEquals(1, workbook.getNumberOfSheets());
    sheet = workbook.iterator().next();/*  ww  w  .  j  a  v  a 2 s.  c o  m*/
}

From source file:org.drools.workbench.screens.guided.dtable.backend.server.conversion.XLSBuilderRetractTest.java

License:Apache License

@BeforeClass
public static void setUp() throws Exception {
    final String xml = loadResource(XLSBuilderRetractTest.class.getResourceAsStream("Retract.gdst"));

    final GuidedDecisionTable52 dtable = GuidedDTXMLPersistence.getInstance().unmarshal(xml);

    final XLSBuilder.BuildResult buildResult = new XLSBuilder(dtable, makeDMO()).build();
    final Workbook workbook = buildResult.getWorkbook();

    assertEquals(1, workbook.getNumberOfSheets());
    sheet = workbook.iterator().next();/*from w w w .j a  v a  2s .  co  m*/
}

From source file:org.drugepi.table.TableCreator.java

License:Mozilla Public License

/**
 * Generate table formats by reading an Excel file.  Each sheet in the Excel file will 
 * serve as a table template./*  w w w .j  a v  a 2  s .  c  o m*/
 * 
 * @param workbookName  Name of the Excel workbook (.xls or .xlsx) with the defined tables.
 * @throws Exception
 */
public void createTablesFromWorkbook(String workbookName) throws Exception {
    InputStream s = new FileInputStream(workbookName);

    Workbook workbook = WorkbookFactory.create(s);
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        Sheet sheet = workbook.getSheetAt(i);
        this.createTableFromSheet(sheet);
    }
}

From source file:org.drugepi.table.TableCreator.java

License:Mozilla Public License

/**
 * Replace the shell table in an Excel file with data from tables.
 * /*from   w  w  w  .  j  av  a2 s .  c  o m*/
 * @param inWorkbookName  Name of the Excel workbook (.xls or .xlsx) with the defined tables.
 * @param outWorkbookName  Name of the Excel workbook (.xls or .xlsx) to use for output.  Any existing file will be replaced.
 * @throws Exception
 */
public void writeTablesToWorkbook(String inWorkbookName, String outWorkbookName) throws Exception {
    InputStream fileIn = new FileInputStream(inWorkbookName);

    Workbook workbook = WorkbookFactory.create(fileIn);
    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
        Sheet sheet = workbook.getSheetAt(i);
        String tableId = TableElement.makeId(sheet.getSheetName());
        Table t = this.tables.get(tableId);
        if (t != null)
            this.fillTableInSheet(sheet, t);
    }

    FileOutputStream fileOut = new FileOutputStream(outWorkbookName);
    workbook.write(fileOut);
    fileOut.close();
}