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

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

Introduction

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

Prototype

int getColumnWidth(int columnIndex);

Source Link

Document

get the width (in units of 1/256th of a character width )

Character width is defined as the maximum digit width of the numbers 0, 1, 2, ...

Usage

From source file:uk.co.spudsoft.birt.emitters.excel.tests.BasicReportTest.java

License:Open Source License

@Test
public void testRunReportWithJpeg() throws BirtException, IOException {

    InputStream inputStream = runAndRenderReport("SimpleWithJpeg.rptdesign", "xlsx");
    assertNotNull(inputStream);//from  ww  w .j a v  a 2  s .c  om
    try {

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(1, workbook.getNumberOfSheets());
        assertEquals("Simple Test Report", workbook.getSheetAt(0).getSheetName());

        Sheet sheet = workbook.getSheetAt(0);
        assertNotNull(sheet.getRow(0));
        assertNotNull(sheet.getRow(1));
        assertNotNull(sheet.getRow(2));
        assertNotNull(sheet.getRow(3));
        assertNotNull(sheet.getRow(4));
        assertNotNull(sheet.getRow(5));
        assertNull(sheet.getRow(6));

        assertEquals(1.0, sheet.getRow(2).getCell(0).getNumericCellValue(), 0.001);
        assertEquals(2.0, sheet.getRow(2).getCell(1).getNumericCellValue(), 0.001);
        assertEquals(3.0, sheet.getRow(2).getCell(2).getNumericCellValue(), 0.001);
        assertEquals(2.0, sheet.getRow(3).getCell(0).getNumericCellValue(), 0.001);
        assertEquals(4.0, sheet.getRow(3).getCell(1).getNumericCellValue(), 0.001);
        assertEquals(6.0, sheet.getRow(3).getCell(2).getNumericCellValue(), 0.001);
        assertEquals(3.0, sheet.getRow(4).getCell(0).getNumericCellValue(), 0.001);
        assertEquals(6.0, sheet.getRow(4).getCell(1).getNumericCellValue(), 0.001);
        assertEquals(9.0, sheet.getRow(4).getCell(2).getNumericCellValue(), 0.001);

        assertEquals(5266, sheet.getColumnWidth(0));
        assertEquals(3510, sheet.getColumnWidth(1));
        assertEquals(3510, sheet.getColumnWidth(2));

        assertEquals(960, sheet.getRow(0).getHeight());
        assertEquals(300, sheet.getRow(1).getHeight());
        assertEquals(300, sheet.getRow(2).getHeight());
        assertEquals(300, sheet.getRow(3).getHeight());
        assertEquals(300, sheet.getRow(4).getHeight());
        assertEquals(2160, sheet.getRow(5).getHeight());

        // Unfortunately it's not currently possible/easy to check the dimensions of images using POI
        // So the XL file has to be opened manually for verification
    } finally {
        inputStream.close();
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.BasicReportTest.java

License:Open Source License

public void performSimpleWithJpegTests(Sheet sheet) {
    assertNotNull(sheet.getRow(0));//w  w w .  ja v  a 2  s. c o  m
    assertNotNull(sheet.getRow(1));
    assertNotNull(sheet.getRow(2));
    assertNotNull(sheet.getRow(3));
    assertNotNull(sheet.getRow(4));
    assertNotNull(sheet.getRow(5));
    assertNull(sheet.getRow(6));

    assertEquals(1.0, sheet.getRow(2).getCell(0).getNumericCellValue(), 0.001);
    assertEquals(2.0, sheet.getRow(2).getCell(1).getNumericCellValue(), 0.001);
    assertEquals(3.0, sheet.getRow(2).getCell(2).getNumericCellValue(), 0.001);
    assertEquals(2.0, sheet.getRow(3).getCell(0).getNumericCellValue(), 0.001);
    assertEquals(4.0, sheet.getRow(3).getCell(1).getNumericCellValue(), 0.001);
    assertEquals(6.0, sheet.getRow(3).getCell(2).getNumericCellValue(), 0.001);
    assertEquals(3.0, sheet.getRow(4).getCell(0).getNumericCellValue(), 0.001);
    assertEquals(6.0, sheet.getRow(4).getCell(1).getNumericCellValue(), 0.001);
    assertEquals(9.0, sheet.getRow(4).getCell(2).getNumericCellValue(), 0.001);

    assertEquals(5266, sheet.getColumnWidth(0));
    assertEquals(3510, sheet.getColumnWidth(1));
    assertEquals(3510, sheet.getColumnWidth(2));

    assertEquals(960, sheet.getRow(0).getHeight());
    assertEquals(2160, sheet.getRow(5).getHeight());
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.ForceColWidthTest.java

License:Open Source License

@Test
public void testRunReportXlsx() throws BirtException, IOException {

    InputStream inputStream = runAndRenderReport("GappyData.rptdesign", "xlsx");
    assertNotNull(inputStream);// w w  w  .java 2  s. co  m
    try {

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(2, workbook.getNumberOfSheets());
        assertEquals("Summary", workbook.getSheetAt(0).getSheetName());
        assertEquals("Data", workbook.getSheetAt(1).getSheetName());

        Sheet summary = workbook.getSheetAt(0);

        assertEquals(5522, summary.getColumnWidth(0));
        assertEquals(1353, summary.getColumnWidth(1));
        assertEquals(529, summary.getColumnWidth(2));
        assertEquals(773, summary.getColumnWidth(3));
        assertEquals(773, summary.getColumnWidth(4));
        assertEquals(0, summary.getColumnWidth(5));
        assertEquals(0, summary.getColumnWidth(6));
        assertEquals(3437, summary.getColumnWidth(7));
        assertEquals(3437, summary.getColumnWidth(8));
        assertEquals(3437, summary.getColumnWidth(9));
        assertEquals(3437, summary.getColumnWidth(10));
        assertEquals(3437, summary.getColumnWidth(11));

        Sheet data = workbook.getSheetAt(1);

        assertEquals(4460, data.getColumnWidth(0));
        assertEquals(1353, data.getColumnWidth(1));
        assertEquals(538, data.getColumnWidth(2));
        assertEquals(773, data.getColumnWidth(3));
        assertEquals(773, data.getColumnWidth(4));
        assertEquals(0, data.getColumnWidth(5));
        assertEquals(0, data.getColumnWidth(6));
        assertEquals(2048, data.getColumnWidth(7));
        assertEquals(2048, data.getColumnWidth(8));
        assertEquals(2048, data.getColumnWidth(9));
        assertEquals(2048, data.getColumnWidth(10));
        assertEquals(2048, data.getColumnWidth(11));

    } finally {
        inputStream.close();
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.NumberFormatsTest.java

License:Open Source License

@Test
public void testRunReport() throws BirtException, IOException {

    debug = false;/*from  w ww .j  a  v a  2s .c o m*/
    InputStream inputStream = runAndRenderReport("NumberFormats.rptdesign", "xlsx");
    assertNotNull(inputStream);
    try {

        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(1, workbook.getNumberOfSheets());
        assertEquals("Number Formats Test Report", workbook.getSheetAt(0).getSheetName());

        Sheet sheet = workbook.getSheetAt(0);
        assertEquals(22, this.firstNullRow(sheet));

        assertEquals(3035, sheet.getColumnWidth(0));
        assertEquals(3913, sheet.getColumnWidth(1));
        assertEquals(7021, sheet.getColumnWidth(2));
        assertEquals(4205, sheet.getColumnWidth(3));
        assertEquals(3474, sheet.getColumnWidth(4));
        assertEquals(2852, sheet.getColumnWidth(5));
        assertEquals(3510, sheet.getColumnWidth(6));
        assertEquals(2889, sheet.getColumnWidth(7));
        assertEquals(2048, sheet.getColumnWidth(8));

        DataFormatter formatter = new DataFormatter();
        Locale locale = Locale.getDefault();

        assertEquals("1", formatter.formatCellValue(sheet.getRow(1).getCell(1)));
        assertEquals("2019-10-11 13:18:46", formatter.formatCellValue(sheet.getRow(1).getCell(2)));
        assertEquals("3.1415926536", formatter.formatCellValue(sheet.getRow(1).getCell(3)));
        assertEquals("3.1415926536", formatter.formatCellValue(sheet.getRow(1).getCell(4)));
        assertEquals("false", formatter.formatCellValue(sheet.getRow(1).getCell(5)));
        if (locale.getDisplayName().equals("en-US")) {
            assertEquals("Oct 11, 2019", formatter.formatCellValue(sheet.getRow(1).getCell(6)));
            assertEquals("1:18:46 PM", formatter.formatCellValue(sheet.getRow(1).getCell(7)));
        } else if (locale.getDisplayName().equals("en-GB")) {
            assertEquals("11-Oct-2019", formatter.formatCellValue(sheet.getRow(1).getCell(6)));
            assertEquals("13:18:46", formatter.formatCellValue(sheet.getRow(1).getCell(7)));
        }

        assertEquals("2", formatter.formatCellValue(sheet.getRow(2).getCell(1)));
        assertEquals("2019-10-11 13:18:46", formatter.formatCellValue(sheet.getRow(2).getCell(2)));
        assertEquals("6.2831853072", formatter.formatCellValue(sheet.getRow(2).getCell(3)));
        assertEquals("6.2831853072", formatter.formatCellValue(sheet.getRow(2).getCell(4)));
        assertEquals("true", formatter.formatCellValue(sheet.getRow(2).getCell(5)));
        if (locale.getDisplayName().equals("en-US")) {
            assertEquals("Oct 11, 2019", formatter.formatCellValue(sheet.getRow(1).getCell(6)));
            assertEquals("1:18:46 PM", formatter.formatCellValue(sheet.getRow(1).getCell(7)));
        } else if (locale.getDisplayName().equals("en-GB")) {
            assertEquals("11-Oct-2019", formatter.formatCellValue(sheet.getRow(1).getCell(6)));
            assertEquals("13:18:46", formatter.formatCellValue(sheet.getRow(1).getCell(7)));
        }

        assertEquals("3.1415926536", formatter.formatCellValue(sheet.getRow(5).getCell(1)));
        assertEquals("3.1415926536", formatter.formatCellValue(sheet.getRow(5).getCell(2)));
        assertEquals("3.14", formatter.formatCellValue(sheet.getRow(5).getCell(3)));
        assertEquals("3.14", formatter.formatCellValue(sheet.getRow(5).getCell(4)));
        assertEquals("314.16%", formatter.formatCellValue(sheet.getRow(5).getCell(5)));
        assertEquals("3.14E00", formatter.formatCellValue(sheet.getRow(5).getCell(6)));
        assertEquals("3.14E00", formatter.formatCellValue(sheet.getRow(5).getCell(7)));

        assertEquals("6.2831853072", formatter.formatCellValue(sheet.getRow(6).getCell(1)));
        assertEquals("6.2831853072", formatter.formatCellValue(sheet.getRow(6).getCell(2)));
        assertEquals("6.28", formatter.formatCellValue(sheet.getRow(6).getCell(3)));
        assertEquals("6.28", formatter.formatCellValue(sheet.getRow(6).getCell(4)));
        assertEquals("628.32%", formatter.formatCellValue(sheet.getRow(6).getCell(5)));
        assertEquals("6.28E00", formatter.formatCellValue(sheet.getRow(6).getCell(6)));
        assertEquals("6.28E00", formatter.formatCellValue(sheet.getRow(6).getCell(7)));

        assertEquals("1", formatter.formatCellValue(sheet.getRow(9).getCell(1)));
        if (locale.getDisplayName().equals("en-US")) {
            assertEquals("October 11, 2019 1:18:46 PM", formatter.formatCellValue(sheet.getRow(9).getCell(2)));
        } else if (locale.getDisplayName().equals("en-GB")) {
            assertEquals("11 October 2019 13:18:46", formatter.formatCellValue(sheet.getRow(9).getCell(2)));
        }
        assertEquals("3.1415926536", formatter.formatCellValue(sheet.getRow(9).getCell(3)));
        assertEquals("3.1415926536", formatter.formatCellValue(sheet.getRow(9).getCell(4)));
        assertEquals("false", formatter.formatCellValue(sheet.getRow(9).getCell(5)));
        if (locale.getDisplayName().equals("en-US")) {
            assertEquals("10/11/19", formatter.formatCellValue(sheet.getRow(9).getCell(6)));
        } else if (locale.getDisplayName().equals("en-GB")) {
            assertEquals("11/10/19", formatter.formatCellValue(sheet.getRow(9).getCell(6)));
        }
        assertEquals("13:18", formatter.formatCellValue(sheet.getRow(9).getCell(7)));

        assertEquals("2", formatter.formatCellValue(sheet.getRow(10).getCell(1)));
        if (locale.getDisplayName().equals("en-US")) {
            assertEquals("October 11, 2019 1:18:46 PM", formatter.formatCellValue(sheet.getRow(9).getCell(2)));
        } else if (locale.getDisplayName().equals("en-GB")) {
            assertEquals("11 October 2019 13:18:46", formatter.formatCellValue(sheet.getRow(9).getCell(2)));
        }
        assertEquals("6.2831853072", formatter.formatCellValue(sheet.getRow(10).getCell(3)));
        assertEquals("6.2831853072", formatter.formatCellValue(sheet.getRow(10).getCell(4)));
        assertEquals("true", formatter.formatCellValue(sheet.getRow(10).getCell(5)));
        if (locale.getDisplayName().equals("en-US")) {
            assertEquals("10/11/19", formatter.formatCellValue(sheet.getRow(9).getCell(6)));
        } else if (locale.getDisplayName().equals("en-GB")) {
            assertEquals("11/10/19", formatter.formatCellValue(sheet.getRow(9).getCell(6)));
        }
        assertEquals("13:18", formatter.formatCellValue(sheet.getRow(10).getCell(7)));

        assertEquals("MSRP $3.14", formatter.formatCellValue(sheet.getRow(15).getCell(1)));

        assertEquals("_-* #,##0.00_-;-* #,##0.00_-;_-* \"-\"??_-;_-@_-",
                sheet.getRow(19).getCell(1).getCellStyle().getDataFormatString());
        assertEquals(sheet.getRow(18).getCell(2).getStringCellValue(),
                sheet.getRow(19).getCell(2).getCellStyle().getDataFormatString());
        assertEquals(sheet.getRow(18).getCell(3).getStringCellValue(),
                sheet.getRow(19).getCell(3).getCellStyle().getDataFormatString());
        assertEquals(sheet.getRow(18).getCell(4).getStringCellValue(),
                sheet.getRow(19).getCell(4).getCellStyle().getDataFormatString());
        assertEquals(sheet.getRow(18).getCell(5).getStringCellValue(),
                sheet.getRow(19).getCell(5).getCellStyle().getDataFormatString());
        assertEquals(sheet.getRow(18).getCell(6).getStringCellValue(),
                sheet.getRow(19).getCell(6).getCellStyle().getDataFormatString());
        assertEquals(sheet.getRow(18).getCell(7).getStringCellValue(),
                sheet.getRow(19).getCell(7).getCellStyle().getDataFormatString());

    } finally {
        inputStream.close();
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.SideBySide.java

License:Open Source License

@Test
public void singleCells() throws Exception {
    debug = false;//from w  ww . jav a  2  s .  com
    InputStream inputStream = runAndRenderReport("SideBySideOneCellEach.rptdesign", "xlsx");
    assertNotNull(inputStream);
    try {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(1, workbook.getNumberOfSheets());
        assertEquals("Sheet0", workbook.getSheetAt(0).getSheetName());

        Sheet sheet = workbook.getSheetAt(0);
        assertEquals(1, this.firstNullRow(sheet));

        assertEquals(305, sheet.getRow(0).getHeightInPoints(), 1.0);
        assertEquals(19346, sheet.getColumnWidth(0));
        assertEquals(19346, sheet.getColumnWidth(1));
    } finally {
        inputStream.close();
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.SideBySide.java

License:Open Source License

@Test
public void multiColumns() throws Exception {
    debug = false;/*from  w  ww .j  av a  2  s  . co  m*/
    InputStream inputStream = runAndRenderReport("SideBySideMultiColumns.rptdesign", "xlsx");
    assertNotNull(inputStream);
    try {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(1, workbook.getNumberOfSheets());
        assertEquals("Sheet0", workbook.getSheetAt(0).getSheetName());

        Sheet sheet = workbook.getSheetAt(0);
        assertEquals(124, this.firstNullRow(sheet));

        assertEquals(297, sheet.getRow(0).getHeightInPoints(), 1.0);
        assertEquals(2048, sheet.getColumnWidth(0));
        assertEquals(6196, sheet.getColumnWidth(1));
        assertEquals(3749, sheet.getColumnWidth(2));
        assertEquals(2396, sheet.getColumnWidth(3));
        assertEquals(4516, sheet.getColumnWidth(4));
        assertEquals(7072, sheet.getColumnWidth(5));
        assertEquals(2048, sheet.getColumnWidth(6));
        assertEquals(3509, sheet.getColumnWidth(7));
        assertEquals(2048, sheet.getColumnWidth(8));
        assertEquals(2314, sheet.getColumnWidth(9));
        assertEquals(2338, sheet.getColumnWidth(10));
        assertEquals(2048, sheet.getColumnWidth(11));
        assertEquals(2048, sheet.getColumnWidth(12));

        assertTrue(mergedRegion(sheet, 0, 0, 0, 5));
        assertTrue(mergedRegion(sheet, 0, 7, 0, 12));
    } finally {
        inputStream.close();
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.tests.SideBySide.java

License:Open Source License

@Test
public void tables() throws Exception {
    debug = false;/*from   w w  w. j a v a 2 s .com*/
    removeEmptyRows = false;
    InputStream inputStream = runAndRenderReport("SideBySideTables.rptdesign", "xlsx");
    assertNotNull(inputStream);
    try {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        assertNotNull(workbook);

        assertEquals(1, workbook.getNumberOfSheets());
        assertEquals("Sheet0", workbook.getSheetAt(0).getSheetName());

        Sheet sheet = workbook.getSheetAt(0);
        assertEquals(29, this.firstNullRow(sheet));

        assertEquals(5522, sheet.getColumnWidth(0));
        assertEquals(2742, sheet.getColumnWidth(1));
        assertEquals(1353, sheet.getColumnWidth(2));
        assertEquals(5522, sheet.getColumnWidth(3));
        assertEquals(2742, sheet.getColumnWidth(4));

        assertTrue(mergedRegion(sheet, 0, 2, 28, 2));
        assertTrue(mergedRegion(sheet, 8, 3, 28, 3));
        assertTrue(mergedRegion(sheet, 8, 4, 28, 4));

    } finally {
        inputStream.close();
    }

}