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();
}
}
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));
}
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());
}
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));
}
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();
}
}
Aggregations