Search in sources :

Example 41 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestStylesTable method removeNumberFormat.

@Test
public void removeNumberFormat() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    try {
        final String fmt = customDataFormat;
        final short fmtIdx = (short) wb1.getStylesSource().putNumberFormat(fmt);
        Cell cell = wb1.createSheet("test").createRow(0).createCell(0);
        cell.setCellValue(5.25);
        CellStyle style = wb1.createCellStyle();
        style.setDataFormat(fmtIdx);
        cell.setCellStyle(style);
        assertEquals(fmt, cell.getCellStyle().getDataFormatString());
        assertEquals(fmt, wb1.getStylesSource().getNumberFormatAt(fmtIdx));
        // remove the number format from the workbook
        wb1.getStylesSource().removeNumberFormat(fmt);
        // number format in CellStyles should be restored to default number format
        final short defaultFmtIdx = 0;
        final String defaultFmt = BuiltinFormats.getBuiltinFormat(0);
        assertEquals(defaultFmtIdx, style.getDataFormat());
        assertEquals(defaultFmt, style.getDataFormatString());
        // The custom number format should be entirely removed from the workbook
        Map<Short, String> numberFormats = wb1.getStylesSource().getNumberFormats();
        assertNotContainsKey(numberFormats, fmtIdx);
        assertNotContainsValue(numberFormats, fmt);
        // The default style shouldn't be added back to the styles source because it's built-in
        assertEquals(0, wb1.getStylesSource().getNumDataFormats());
        cell = null;
        style = null;
        numberFormats = null;
        XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1);
        cell = wb2.getSheet("test").getRow(0).getCell(0);
        style = cell.getCellStyle();
        // number format in CellStyles should be restored to default number format
        assertEquals(defaultFmtIdx, style.getDataFormat());
        assertEquals(defaultFmt, style.getDataFormatString());
        // The custom number format should be entirely removed from the workbook
        numberFormats = wb2.getStylesSource().getNumberFormats();
        assertNotContainsKey(numberFormats, fmtIdx);
        assertNotContainsValue(numberFormats, fmt);
        // The default style shouldn't be added back to the styles source because it's built-in
        assertEquals(0, wb2.getStylesSource().getNumDataFormats());
        wb2.close();
    } finally {
        wb1.close();
    }
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 42 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestStylesTable method testLoadExisting.

@Test
public void testLoadExisting() {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
    assertNotNull(workbook.getStylesSource());
    StylesTable st = workbook.getStylesSource();
    doTestExisting(st);
    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 43 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestThemesTable method testAddNew.

@Test
@SuppressWarnings("resource")
public void testAddNew() throws Exception {
    XSSFWorkbook wb = new XSSFWorkbook();
    wb.createSheet();
    assertEquals(null, wb.getTheme());
    StylesTable styles = wb.getStylesSource();
    assertEquals(null, styles.getTheme());
    styles.ensureThemesTable();
    assertNotNull(styles.getTheme());
    assertNotNull(wb.getTheme());
    wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
    styles = wb.getStylesSource();
    assertNotNull(styles.getTheme());
    assertNotNull(wb.getTheme());
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 44 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestStylesTable method populateExisting.

@Test
public void populateExisting() {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
    assertNotNull(workbook.getStylesSource());
    StylesTable st = workbook.getStylesSource();
    assertEquals(11, st._getXfsSize());
    assertEquals(1, st._getStyleXfsSize());
    assertEquals(8, st.getNumDataFormats());
    int nf1 = st.putNumberFormat("YYYY-mm-dd");
    int nf2 = st.putNumberFormat("YYYY-mm-DD");
    assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
    st = XSSFTestDataSamples.writeOutAndReadBack(workbook).getStylesSource();
    assertEquals(11, st._getXfsSize());
    assertEquals(1, st._getStyleXfsSize());
    assertEquals(10, st.getNumDataFormats());
    assertEquals("YYYY-mm-dd", st.getNumberFormatAt((short) nf1));
    assertEquals(nf1, st.putNumberFormat("YYYY-mm-dd"));
    assertEquals(nf2, st.putNumberFormat("YYYY-mm-DD"));
    assertNotNull(XSSFTestDataSamples.writeOutAndReadBack(workbook));
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 45 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook in project poi by apache.

the class TestSXSSFCell method testPreserveSpaces.

@Test
public void testPreserveSpaces() throws IOException {
    String[] samplesWithSpaces = { " POI", "POI ", " POI ", "\nPOI", "\n\nPOI \n" };
    for (String str : samplesWithSpaces) {
        Workbook swb = _testDataProvider.createWorkbook();
        Cell sCell = swb.createSheet().createRow(0).createCell(0);
        sCell.setCellValue(str);
        assertEquals(sCell.getStringCellValue(), str);
        // read back as XSSF and check that xml:spaces="preserve" is set
        XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb);
        XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
        CTRst is = xCell.getCTCell().getIs();
        XmlCursor c = is.newCursor();
        c.toNextToken();
        String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
        c.dispose();
        assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
        xwb.close();
        swb.close();
    }
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) QName(javax.xml.namespace.QName) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) BaseTestXCell(org.apache.poi.ss.usermodel.BaseTestXCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XmlCursor(org.apache.xmlbeans.XmlCursor) Test(org.junit.Test)

Aggregations

XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)228 Test (org.junit.Test)109 Sheet (org.apache.poi.ss.usermodel.Sheet)87 Workbook (org.apache.poi.ss.usermodel.Workbook)87 FileOutputStream (java.io.FileOutputStream)49 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)49 Cell (org.apache.poi.ss.usermodel.Cell)40 Row (org.apache.poi.ss.usermodel.Row)40 ByteArrayInputStream (java.io.ByteArrayInputStream)34 File (java.io.File)28 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)27 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)26 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)25 LangYearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.LangYearFilterPagingRequest)23 XSSFMap (org.apache.poi.xssf.usermodel.XSSFMap)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)19 IOException (java.io.IOException)17 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)17 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)16