use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr in project poi by apache.
the class TestXSSFWorkbook method saveLoadNew.
/**
* Tests that we can save, and then re-load a new document
*/
@Test
public void saveLoadNew() throws IOException, InvalidFormatException {
XSSFWorkbook wb1 = new XSSFWorkbook();
//check that the default date system is set to 1900
CTWorkbookPr pr = wb1.getCTWorkbook().getWorkbookPr();
assertNotNull(pr);
assertTrue(pr.isSetDate1904());
assertFalse("XSSF must use the 1900 date system", pr.getDate1904());
Sheet sheet1 = wb1.createSheet("sheet1");
Sheet sheet2 = wb1.createSheet("sheet2");
wb1.createSheet("sheet3");
RichTextString rts = wb1.getCreationHelper().createRichTextString("hello world");
sheet1.createRow(0).createCell((short) 0).setCellValue(1.2);
sheet1.createRow(1).createCell((short) 0).setCellValue(rts);
sheet2.createRow(0);
assertEquals(0, wb1.getSheetAt(0).getFirstRowNum());
assertEquals(1, wb1.getSheetAt(0).getLastRowNum());
assertEquals(0, wb1.getSheetAt(1).getFirstRowNum());
assertEquals(0, wb1.getSheetAt(1).getLastRowNum());
assertEquals(0, wb1.getSheetAt(2).getFirstRowNum());
assertEquals(0, wb1.getSheetAt(2).getLastRowNum());
File file = TempFile.createTempFile("poi-", ".xlsx");
OutputStream out = new FileOutputStream(file);
wb1.write(out);
out.close();
// Check the package contains what we'd expect it to
OPCPackage pkg = OPCPackage.open(file.toString());
PackagePart wbRelPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
assertNotNull(wbRelPart);
assertTrue(wbRelPart.isRelationshipPart());
assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
// Links to the three sheets, shared strings and styles
assertTrue(wbPart.hasRelationships());
assertEquals(5, wbPart.getRelationships().size());
wb1.close();
// Load back the XSSFWorkbook
@SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
assertEquals(3, wb2.getNumberOfSheets());
assertNotNull(wb2.getSheetAt(0));
assertNotNull(wb2.getSheetAt(1));
assertNotNull(wb2.getSheetAt(2));
assertNotNull(wb2.getSharedStringSource());
assertNotNull(wb2.getStylesSource());
assertEquals(0, wb2.getSheetAt(0).getFirstRowNum());
assertEquals(1, wb2.getSheetAt(0).getLastRowNum());
assertEquals(0, wb2.getSheetAt(1).getFirstRowNum());
assertEquals(0, wb2.getSheetAt(1).getLastRowNum());
assertEquals(0, wb2.getSheetAt(2).getFirstRowNum());
assertEquals(0, wb2.getSheetAt(2).getLastRowNum());
sheet1 = wb2.getSheetAt(0);
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
pkg.close();
}
use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr in project poi by apache.
the class XSSFWorkbook method onWorkbookCreate.
/**
* Create a new CTWorkbook with all values set to default
*/
private void onWorkbookCreate() {
workbook = CTWorkbook.Factory.newInstance();
// don't EVER use the 1904 date system
CTWorkbookPr workbookPr = workbook.addNewWorkbookPr();
workbookPr.setDate1904(false);
CTBookViews bvs = workbook.addNewBookViews();
CTBookView bv = bvs.addNewWorkbookView();
bv.setActiveTab(0);
workbook.addNewSheets();
POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties();
expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR);
sharedStringSource = (SharedStringsTable) createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
stylesSource = (StylesTable) createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
stylesSource.setWorkbook(this);
namedRanges = new ArrayList<XSSFName>();
namedRangesByName = new ArrayListValuedHashMap<String, XSSFName>();
sheets = new ArrayList<XSSFSheet>();
pivotTables = new ArrayList<XSSFPivotTable>();
}
Aggregations