Search in sources :

Example 1 with CTWorkbookPr

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) CTWorkbookPr(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr) RichTextString(org.apache.poi.ss.usermodel.RichTextString) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Sheet(org.apache.poi.ss.usermodel.Sheet) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 2 with CTWorkbookPr

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>();
}
Also used : CTBookViews(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews) POIXMLProperties(org.apache.poi.POIXMLProperties) CTWorkbookPr(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr) CTBookView(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView)

Aggregations

CTWorkbookPr (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 POIXMLProperties (org.apache.poi.POIXMLProperties)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 MemoryPackagePart (org.apache.poi.openxml4j.opc.internal.MemoryPackagePart)1 RichTextString (org.apache.poi.ss.usermodel.RichTextString)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 TempFile (org.apache.poi.util.TempFile)1 Test (org.junit.Test)1 CTBookView (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView)1 CTBookViews (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews)1