Example usage for org.apache.poi.ss.usermodel Sheet getRow

List of usage examples for org.apache.poi.ss.usermodel Sheet getRow

Introduction

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

Prototype

Row getRow(int rownum);

Source Link

Document

Returns the logical row (not physical) 0-based.

Usage

From source file:com.rarediscovery.services.logic.WorkPad.java

public void addRowData(String sheetName, String[] dataArray, int rowIndex, int column) {
    Sheet s = addWorksheet(sheetName);
    Row row = null;/*  w w w .  j  av  a 2  s  .c  o  m*/

    // When item requested is out of range of available rows
    if (s.getLastRowNum() > rowIndex) {
        row = s.getRow(rowIndex);
    } else {
        row = s.createRow(rowIndex);
    }

    CellStyle style = applySelectedStyle();

    for (int r = 0; r < dataArray.length; r++) {
        row.createCell(column + r).setCellValue(dataArray[r]);
        row.getCell(column + r).setCellStyle(style);
    }
    //font.setBold(false);
}

From source file:com.rodrigodev.xgen4j_table_generator.test.common.assertion.excel.conditions.ExcelFile.java

License:Open Source License

@Override
public boolean matches(InputStream actualInputStream) {

    boolean result = true;

    try {/*from w  ww .  j ava2s.  c  o m*/
        try (Workbook expectedWb = WorkbookFactory.create(expectedInputStream)) {
            Sheet expectedSheet = expectedWb.getSheetAt(0);

            try (Workbook actualWb = WorkbookFactory.create(actualInputStream)) {
                Sheet actualSheet = actualWb.getSheetAt(0);

                int expectedRowCount = expectedSheet.getLastRowNum();
                for (int r = 0; r <= expectedRowCount; r++) {
                    Row expectedRow = expectedSheet.getRow(r);
                    Row actualRow = actualSheet.getRow(r);
                    if (actualRow == null) {
                        actualRow = actualSheet.createRow(r);
                    }

                    int expectedCellCount = expectedRow.getLastCellNum();
                    for (int c = 0; c < expectedCellCount; c++) {
                        Cell expectedCell = expectedRow.getCell(c, Row.CREATE_NULL_AS_BLANK);
                        Cell actualCell = actualRow.getCell(c, Row.CREATE_NULL_AS_BLANK);

                        if (expectedCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                            assertThat(actualCell.getNumericCellValue())
                                    .isEqualTo(expectedCell.getNumericCellValue(), offset(0.00001));
                        } else {
                            expectedCell.setCellType(Cell.CELL_TYPE_STRING);
                            actualCell.setCellType(Cell.CELL_TYPE_STRING);
                            assertThat(actualCell.getStringCellValue())
                                    .isEqualTo(expectedCell.getStringCellValue());
                        }
                    }
                }
            }
        }
    } catch (AssertionError error) {
        describedAs(error.getMessage());
        result = false;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testExport() throws IOException {
    ExcelExporter exporter = new ExcelExporter();
    exporter.addTemplate(mdBusiness.definesType());
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(1, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);/*from w ww . j av  a  2s .  co m*/
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    List<? extends MdAttributeDAOIF> attributes = ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdAttribute.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(attributes.size()));
    assertNull(labelRow.getCell(attributes.size()));
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testFormExport() throws IOException {
    ExcelExporter exporter = new FormExcelExporter(new MdWebAttributeFilter());
    exporter.addTemplate(mdForm.definesType());
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(1, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);//  ww  w  .  j av a2 s.c  o m
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    List<? extends MdFieldDAOIF> fields = mdForm.getSortedFields();

    for (int i = 0; i < fields.size(); i++) {
        MdFieldDAOIF mdField = fields.get(i);
        MdAttributeDAOIF mdAttribute = ((MdWebAttributeDAOIF) mdField).getDefiningMdAttribute();

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdField.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(fields.size()));
    assertNull(labelRow.getCell(fields.size()));
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testExtraColumns() throws IOException {
    ExcelExporter exporter = new ExcelExporter();
    exporter.addListener(new MockExcelExportListener());
    exporter.addTemplate(mdBusiness.definesType());
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(1, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);//from w w w.  j  a  v a2  s  .c om
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    int cellNum = ((List<? extends MdAttributeDAOIF>) ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter())).size();

    String attributeName = attributeRow.getCell(cellNum).getRichStringCellValue().toString();
    String label = labelRow.getCell(cellNum).getRichStringCellValue().toString();

    assertEquals(MockExcelExportListener.ATTRIBUTE_NAME, attributeName);
    assertEquals(MockExcelExportListener.DISPLAY_LABEL, label);
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testAddRow() throws IOException {
    BusinessDAO business = BusinessDAO.newInstance(mdBusiness.definesType());
    business.setValue(TestFixConst.ATTRIBUTE_CHARACTER, "Test Character Value");
    business.setValue("testDouble", "10");
    business.setValue("testInteger", "-1");

    ExcelExporter exporter = new ExcelExporter();

    ExcelExportSheet excelSheet = exporter.addTemplate(mdBusiness.definesType());
    excelSheet.addRow(business);/* w w  w . j  a  va 2  s .co  m*/

    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(1, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row row = sheet.getRow(3);

    List<? extends MdAttributeDAOIF> attributes = ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);
        String value = ExcelUtil.getString(row.getCell(i));

        assertEquals(business.getValue(mdAttribute.definesAttribute()), value);
    }
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testMultipleSheets() throws IOException {
    ExcelExporter exporter = new ExcelExporter();
    exporter.addTemplate(mdBusiness.definesType());
    exporter.addTemplate(mdBusiness2.definesType());
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(2, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);//from  w ww.  ja v  a  2s. co m
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    List<? extends MdAttributeDAOIF> attributes = ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdAttribute.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(attributes.size()));
    assertNull(labelRow.getCell(attributes.size()));

    sheet = workbook.getSheetAt(1);
    typeRow = sheet.getRow(0);
    attributeRow = sheet.getRow(1);
    labelRow = sheet.getRow(2);

    assertEquals(mdBusiness2.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    attributes = ExcelUtil.getAttributes(mdBusiness2, new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdAttribute.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(attributes.size()));
    assertNull(labelRow.getCell(attributes.size()));
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testMultipleSheetsWithDefaultListeners() throws IOException {
    ExcelExporter exporter = new ExcelExporter();
    exporter.addListener(new MockExcelExportListener());
    exporter.addTemplate(mdBusiness.definesType());
    exporter.addTemplate(mdBusiness2.definesType());
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(2, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);/*from w  w w .  jav a 2  s.  c  o  m*/
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    int cellNum = ((List<? extends MdAttributeDAOIF>) ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter())).size();

    String attributeName = attributeRow.getCell(cellNum).getRichStringCellValue().toString();
    String label = labelRow.getCell(cellNum).getRichStringCellValue().toString();

    assertEquals(MockExcelExportListener.ATTRIBUTE_NAME, attributeName);
    assertEquals(MockExcelExportListener.DISPLAY_LABEL, label);

    sheet = workbook.getSheetAt(1);
    typeRow = sheet.getRow(0);
    attributeRow = sheet.getRow(1);
    labelRow = sheet.getRow(2);

    assertEquals(mdBusiness2.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    cellNum = ((List<? extends MdAttributeDAOIF>) ExcelUtil.getAttributes(mdBusiness2,
            new DefaultExcelAttributeFilter())).size();

    attributeName = attributeRow.getCell(cellNum).getRichStringCellValue().toString();
    label = labelRow.getCell(cellNum).getRichStringCellValue().toString();

    assertEquals(MockExcelExportListener.ATTRIBUTE_NAME, attributeName);
    assertEquals(MockExcelExportListener.DISPLAY_LABEL, label);
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testMultipleSheetsWithDifferentListeners() throws IOException {
    List<ExcelExportListener> listeners = new LinkedList<ExcelExportListener>();
    listeners.add(new MockExcelExportListener());

    ExcelExporter exporter = new ExcelExporter();
    exporter.addTemplate(mdBusiness.definesType());
    exporter.addTemplate(mdBusiness2.definesType(), listeners);
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(2, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);/*from  ww  w . ja  v a2  s .c  o m*/
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    List<? extends MdAttributeDAOIF> attributes = ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdAttribute.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(attributes.size()));
    assertNull(labelRow.getCell(attributes.size()));

    sheet = workbook.getSheetAt(1);
    typeRow = sheet.getRow(0);
    attributeRow = sheet.getRow(1);
    labelRow = sheet.getRow(2);

    assertEquals(mdBusiness2.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    int cellNum = ((List<? extends MdAttributeDAOIF>) ExcelUtil.getAttributes(mdBusiness2,
            new DefaultExcelAttributeFilter())).size();

    String attributeName = attributeRow.getCell(cellNum).getRichStringCellValue().toString();
    String label = labelRow.getCell(cellNum).getRichStringCellValue().toString();

    assertEquals(MockExcelExportListener.ATTRIBUTE_NAME, attributeName);
    assertEquals(MockExcelExportListener.DISPLAY_LABEL, label);
}

From source file:com.runwaysdk.dataaccess.io.ExcelExporterTest.java

License:Open Source License

public void testInvalidSheetNames() throws IOException {
    ExcelExporter exporter = new ExcelExporter();
    exporter.addTemplate(mdBusiness.definesType());
    exporter.addTemplate(mdBusiness2.definesType());
    exporter.addTemplate(mdBusiness3.definesType());
    byte[] bytes = exporter.write();

    Workbook workbook = new HSSFWorkbook(new ByteArrayInputStream(bytes));

    assertEquals(3, workbook.getNumberOfSheets());

    Sheet sheet = workbook.getSheetAt(0);
    Row typeRow = sheet.getRow(0);
    Row attributeRow = sheet.getRow(1);/* w w  w  .  jav  a  2s .co  m*/
    Row labelRow = sheet.getRow(2);

    assertEquals(mdBusiness.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    List<? extends MdAttributeDAOIF> attributes = ExcelUtil.getAttributes(mdBusiness,
            new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdAttribute.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(attributes.size()));
    assertNull(labelRow.getCell(attributes.size()));

    sheet = workbook.getSheetAt(1);
    typeRow = sheet.getRow(0);
    attributeRow = sheet.getRow(1);
    labelRow = sheet.getRow(2);

    assertEquals(mdBusiness2.definesType(), typeRow.getCell(0).getRichStringCellValue().toString());

    attributes = ExcelUtil.getAttributes(mdBusiness2, new DefaultExcelAttributeFilter());

    for (int i = 0; i < attributes.size(); i++) {
        MdAttributeDAOIF mdAttribute = attributes.get(i);

        String attributeName = attributeRow.getCell(i).getRichStringCellValue().toString();
        String label = labelRow.getCell(i).getRichStringCellValue().toString();

        assertEquals(mdAttribute.definesAttribute(), attributeName);
        assertEquals(mdAttribute.getDisplayLabel(Session.getCurrentLocale()), label);
    }

    // Ensure there aren't any extra columns
    assertNull(attributeRow.getCell(attributes.size()));
    assertNull(labelRow.getCell(attributes.size()));
}