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

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

Introduction

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

Prototype

Sheet getSheetAt(int index);

Source Link

Document

Get the Sheet object at the given index.

Usage

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporterTest.java

License:Apache License

/**
 * Test that an exception is raised if a row doesn't have enough columns
 * /*w w  w . j a  va  2 s .  c  o m*/
 * @throws IOException
 */
@Test
public void testCreateWorkbook_NotEnoughRows() throws IOException {
    byte[] bytes = readFile("importertest _notenoughrows.xlsx");
    Workbook wb = importer.createWorkbook(bytes);

    Sheet sheet = wb.getSheetAt(0);

    try {
        importer.processRow(0, sheet.getRow(0), PersonDTO.class);
    } catch (OCSImportException ex) {
        Assert.assertEquals("Row doesn't have enough columns", ex.getMessage());
    }

}

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporterTest.java

License:Apache License

/**
 * Test that an exception occurs if a value for a required field is missing
 * //from w  w w  .  j ava  2  s  .  c  o m
 * @throws IOException
 */
@Test
public void testCreateWorkbook_RequiredValueMissing() throws IOException {
    byte[] bytes = readFile("importertest_required_missing.xlsx");
    Workbook wb = importer.createWorkbook(bytes);

    Sheet sheet = wb.getSheetAt(0);

    try {
        importer.processRow(0, sheet.getRow(1), PersonDTO.class);
    } catch (OCSImportException ex) {
        Assert.assertEquals("Required value for field 'number' is missing", ex.getMessage());
    }
}

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporterTest.java

License:Apache License

/**
 * Test that an exception occurs if an illegal negative value is found
 * //  www  .  j a  va  2  s. c  o m
 * @throws IOException
 */
@Test
public void testCreateWorkbook_CannotBeNegative() throws IOException {
    byte[] bytes = readFile("importertest_notnegative.xlsx");
    Workbook wb = importer.createWorkbook(bytes);

    Sheet sheet = wb.getSheetAt(0);

    try {
        importer.processRow(0, sheet.getRow(1), PersonDTO.class);
    } catch (OCSImportException ex) {
        Assert.assertEquals("Negative value -1.2 found for field 'factor'", ex.getMessage());
    }
}

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporterTest.java

License:Apache License

/**
 * Test whether a certain row contains a cell with a certain string value
 * //from w w w .j ava2  s .c o m
 * @throws IOException
 */
@Test
public void testContainsStringValue() throws IOException {
    byte[] bytes = readFile("importertest.xlsx");
    Workbook wb = importer.createWorkbook(bytes);

    Sheet sheet = wb.getSheetAt(0);

    Assert.assertTrue(importer.containsStringValue(sheet.getRow(0), "Bas"));
    Assert.assertFalse(importer.containsStringValue(sheet.getRow(0), "Bob"));

    Assert.assertFalse(importer.containsStringValue(null, "Bas"));
}

From source file:com.ocs.dynamo.importer.impl.BaseXlsImporterTest.java

License:Apache License

@Test
public void testIsRowEmpty() throws IOException {
    Assert.assertTrue(importer.isRowEmpty(null));

    byte[] bytes = readFile("importertest.xlsx");
    Workbook wb = importer.createWorkbook(bytes);

    Sheet sheet = wb.getSheetAt(0);
    Assert.assertFalse(importer.isRowEmpty(sheet.getRow(0)));

    Assert.assertTrue(importer.isRowEmpty(sheet.getRow(7)));
}

From source file:com.ocs.dynamo.ui.composite.table.export.TableExportActionHandlerTest.java

License:Apache License

@Test
public void testExportSimpleWithoutEntityModel() throws IOException {

    handler = new TableExportActionHandler(ui, messageService, columnIds, REPORT_TITLE, false, null);

    handler.handleAction(handler.getActions(null, null)[0], getTable(), null);

    byte[] bytes = captureSave();
    Workbook wb = importer.createWorkbook(bytes);

    // string//from   w ww .j a  v a  2  s .  com
    Assert.assertEquals("Bas, Bob", wb.getSheetAt(0).getRow(2).getCell(0).getStringCellValue());
    Assert.assertEquals("Patrick", wb.getSheetAt(0).getRow(3).getCell(0).getStringCellValue());

    // integer
    Assert.assertEquals(35, wb.getSheetAt(0).getRow(2).getCell(1).getNumericCellValue(), 0.001);
    Assert.assertEquals(44, wb.getSheetAt(0).getRow(3).getCell(1).getNumericCellValue(), 0.001);

    // bigdecimal
    Assert.assertEquals(76.0, wb.getSheetAt(0).getRow(2).getCell(2).getNumericCellValue(), 0.001);
    Assert.assertEquals(77.0, wb.getSheetAt(0).getRow(3).getCell(2).getNumericCellValue(), 0.001);

    // without an entity model, the application doesn't know this is a
    // percentage
    Assert.assertEquals(12, wb.getSheetAt(0).getRow(2).getCell(3).getNumericCellValue(), 0.001);
    Assert.assertEquals(15, wb.getSheetAt(0).getRow(3).getCell(3).getNumericCellValue(), 0.001);
}

From source file:com.ocs.dynamo.ui.composite.table.export.TableExportActionHandlerTest.java

License:Apache License

@Test
public void testExportTreeTable() throws IOException {

    handler = new TableExportActionHandler(ui, messageService, shortColumnIds, REPORT_TITLE, false, null);
    handler.handleAction(handler.getActions(null, null)[0], getTreeTable(), null);

    byte[] bytes = captureSave();
    Workbook wb = importer.createWorkbook(bytes);

    // string//from  w  w w . ja  v  a 2s . co  m
    Assert.assertEquals("Special ops", wb.getSheetAt(0).getRow(2).getCell(0).getStringCellValue());
    Assert.assertEquals("Bas, Bob", wb.getSheetAt(0).getRow(3).getCell(0).getStringCellValue());
    Assert.assertEquals("Patrick", wb.getSheetAt(0).getRow(4).getCell(0).getStringCellValue());

    // integer
    Assert.assertEquals(35, wb.getSheetAt(0).getRow(3).getCell(1).getNumericCellValue(), 0.001);
    Assert.assertEquals(44, wb.getSheetAt(0).getRow(4).getCell(1).getNumericCellValue(), 0.001);
}

From source file:com.ocs.dynamo.ui.composite.table.export.TableExportActionHandlerTest.java

License:Apache License

@Test
public void testExportGrid() throws IOException {

    handler = new TableExportActionHandler(ui, messageService, columnIds, REPORT_TITLE, false, null);

    handler.exportFromGrid(getGrid());/*from  w ww .  ja v a  2s. c o  m*/

    byte[] bytes = captureSave();
    Workbook wb = importer.createWorkbook(bytes);

    // headers
    Assert.assertEquals("Naam", wb.getSheetAt(0).getRow(1).getCell(0).getStringCellValue());
    Assert.assertEquals("Leeftijd", wb.getSheetAt(0).getRow(1).getCell(1).getStringCellValue());
    Assert.assertEquals("Gewicht", wb.getSheetAt(0).getRow(1).getCell(2).getStringCellValue());
    Assert.assertEquals("Percentage", wb.getSheetAt(0).getRow(1).getCell(3).getStringCellValue());

    // string
    Assert.assertEquals("Bas, Bob", wb.getSheetAt(0).getRow(2).getCell(0).getStringCellValue());
    Assert.assertEquals("Patrick", wb.getSheetAt(0).getRow(3).getCell(0).getStringCellValue());

    // integer
    Assert.assertEquals(35, wb.getSheetAt(0).getRow(2).getCell(1).getNumericCellValue(), 0.001);
    Assert.assertEquals(44, wb.getSheetAt(0).getRow(3).getCell(1).getNumericCellValue(), 0.001);

    // bigdecimal
    Assert.assertEquals(76.0, wb.getSheetAt(0).getRow(2).getCell(2).getNumericCellValue(), 0.001);
    Assert.assertEquals(77.0, wb.getSheetAt(0).getRow(3).getCell(2).getNumericCellValue(), 0.001);

    // without an entity model, the application doesn't know this is a
    // percentage
    Assert.assertEquals(12, wb.getSheetAt(0).getRow(2).getCell(3).getNumericCellValue(), 0.001);
    Assert.assertEquals(15, wb.getSheetAt(0).getRow(3).getCell(3).getNumericCellValue(), 0.001);
}

From source file:com.ocs.dynamo.ui.composite.table.export.TableExportActionHandlerTest.java

License:Apache License

@Test
public void testExportSimpleUsingEntityModel() throws IOException {

    List<EntityModel<?>> models = new ArrayList<>();
    models.add(entityModelFactory.getModel(Person.class));

    handler = new TableExportActionHandler(ui, entityModelFactory, models, messageService, REPORT_TITLE,
            columnIds, false, null);/*  www  . ja va  2  s  . c  o  m*/

    handler.handleAction(handler.getActions(null, null)[0], getTable(), null);

    byte[] bytes = captureSave();
    Workbook wb = importer.createWorkbook(bytes);

    // string
    Assert.assertEquals("Bas, Bob", wb.getSheetAt(0).getRow(2).getCell(0).getStringCellValue());
    Assert.assertEquals("Patrick", wb.getSheetAt(0).getRow(3).getCell(0).getStringCellValue());

    // integer
    Assert.assertEquals(35, wb.getSheetAt(0).getRow(2).getCell(1).getNumericCellValue(), 0.001);
    Assert.assertEquals(44, wb.getSheetAt(0).getRow(3).getCell(1).getNumericCellValue(), 0.001);

    // bigdecimal
    Assert.assertEquals(76.0, wb.getSheetAt(0).getRow(2).getCell(2).getNumericCellValue(), 0.001);
    Assert.assertEquals(77.0, wb.getSheetAt(0).getRow(3).getCell(2).getNumericCellValue(), 0.001);

    // percentage
    Assert.assertEquals(0.12, wb.getSheetAt(0).getRow(2).getCell(3).getNumericCellValue(), 0.001);
    Assert.assertEquals(0.15, wb.getSheetAt(0).getRow(3).getCell(3).getNumericCellValue(), 0.001);
}

From source file:com.ocs.dynamo.ui.composite.table.export.TableExportActionHandlerTest.java

License:Apache License

@Test
public void testExportWithTotalsRow() throws IOException {
    handler = new TableExportActionHandler(ui, messageService, columnIds, REPORT_TITLE, true, null);

    handler.handleAction(handler.getActions(null, null)[0], getTable(), null);

    byte[] bytes = captureSave();
    Workbook wb = importer.createWorkbook(bytes);

    Assert.assertEquals("Bas, Bob", wb.getSheetAt(0).getRow(2).getCell(0).getStringCellValue());
    Assert.assertEquals("Patrick", wb.getSheetAt(0).getRow(3).getCell(0).getStringCellValue());

    Assert.assertEquals(35, wb.getSheetAt(0).getRow(2).getCell(1).getNumericCellValue(), 0.001);
    Assert.assertEquals(44, wb.getSheetAt(0).getRow(3).getCell(1).getNumericCellValue(), 0.001);

    // totals must be summed up
    Assert.assertEquals(79, wb.getSheetAt(0).getRow(4).getCell(1).getNumericCellValue(), 0.001);

}