Search in sources :

Example 31 with XSSFWorkbook

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

the class TestPOIXMLProperties method testWorkbookExtendedProperties.

@Test
public void testWorkbookExtendedProperties() throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook();
    POIXMLProperties props = workbook.getProperties();
    assertNotNull(props);
    org.apache.poi.POIXMLProperties.ExtendedProperties properties = props.getExtendedProperties();
    org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties ctProps = properties.getUnderlyingProperties();
    String appVersion = "3.5 beta";
    String application = "POI";
    ctProps.setApplication(application);
    ctProps.setAppVersion(appVersion);
    XSSFWorkbook newWorkbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    workbook.close();
    assertTrue(workbook != newWorkbook);
    POIXMLProperties newProps = newWorkbook.getProperties();
    assertNotNull(newProps);
    org.apache.poi.POIXMLProperties.ExtendedProperties newProperties = newProps.getExtendedProperties();
    assertEquals(application, newProperties.getApplication());
    assertEquals(appVersion, newProperties.getAppVersion());
    org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties newCtProps = newProperties.getUnderlyingProperties();
    assertEquals(application, newCtProps.getApplication());
    assertEquals(appVersion, newCtProps.getAppVersion());
    newWorkbook.close();
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 32 with XSSFWorkbook

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

the class TestXMLPropertiesTextExtractor method testGetFromMainExtractor.

public void testGetFromMainExtractor() throws Exception {
    OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    XSSFExcelExtractor ext = new XSSFExcelExtractor(wb);
    POIXMLPropertiesTextExtractor textExt = ext.getMetadataTextExtractor();
    // Check basics
    assertNotNull(textExt);
    assertTrue(textExt.getText().length() > 0);
    // Check some of the content
    String text = textExt.getText();
    String cText = textExt.getCorePropertiesText();
    assertContains(text, "LastModifiedBy = Yury Batrakov");
    assertContains(cText, "LastModifiedBy = Yury Batrakov");
    textExt.close();
    ext.close();
}
Also used : XSSFExcelExtractor(org.apache.poi.xssf.extractor.XSSFExcelExtractor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 33 with XSSFWorkbook

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

the class TestXMLPropertiesTextExtractor method testCore.

public void testCore() throws Exception {
    OPCPackage pkg = PackageHelper.open(_ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm"));
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(wb);
    ext.getText();
    // Now check
    String text = ext.getText();
    String cText = ext.getCorePropertiesText();
    assertContains(text, "LastModifiedBy = Yury Batrakov");
    assertContains(cText, "LastModifiedBy = Yury Batrakov");
    ext.close();
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 34 with XSSFWorkbook

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

the class TestFormulaParser method testMacroFunction.

// copied from org.apache.poi.hssf.model.TestFormulaParser
@Test
public void testMacroFunction() throws Exception {
    // testNames.xlsm contains a VB function called 'myFunc'
    final String testFile = "testNames.xlsm";
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(testFile);
    try {
        XSSFEvaluationWorkbook workbook = XSSFEvaluationWorkbook.create(wb);
        //Expected ptg stack: [NamePtg(myFunc), StringPtg(arg), (additional operands would go here...), FunctionPtg(myFunc)]
        Ptg[] ptg = FormulaParser.parse("myFunc(\"arg\")", workbook, FormulaType.CELL, -1);
        assertEquals(3, ptg.length);
        // the name gets encoded as the first operand on the stack
        NameXPxg tname = (NameXPxg) ptg[0];
        assertEquals("myFunc", tname.toFormulaString());
        // the function's arguments are pushed onto the stack from left-to-right as OperandPtgs
        StringPtg arg = (StringPtg) ptg[1];
        assertEquals("arg", arg.getValue());
        // The external FunctionPtg is the last Ptg added to the stack
        // During formula evaluation, this Ptg pops off the the appropriate number of
        // arguments (getNumberOfOperands()) and pushes the result on the stack 
        AbstractFunctionPtg tfunc = (AbstractFunctionPtg) ptg[2];
        assertTrue(tfunc.isExternalFunction());
        // confirm formula parsing is case-insensitive
        FormulaParser.parse("mYfUnC(\"arg\")", workbook, FormulaType.CELL, -1);
        // confirm formula parsing doesn't care about argument count or type
        // this should only throw an error when evaluating the formula.
        FormulaParser.parse("myFunc()", workbook, FormulaType.CELL, -1);
        FormulaParser.parse("myFunc(\"arg\", 0, TRUE)", workbook, FormulaType.CELL, -1);
        // A completely unknown formula name (not saved in workbook) should still be parseable and renderable
        // but will throw an NotImplementedFunctionException or return a #NAME? error value if evaluated.
        FormulaParser.parse("yourFunc(\"arg\")", workbook, FormulaType.CELL, -1);
        // Make sure workbook can be written and read
        XSSFTestDataSamples.writeOutAndReadBack(wb).close();
    // Manually check to make sure file isn't corrupted
    // TODO: develop a process for occasionally manually reviewing workbooks
    // to verify workbooks are not corrupted
    /*
            final File fileIn = XSSFTestDataSamples.getSampleFile(testFile);
            final File reSavedFile = new File(fileIn.getParentFile(), fileIn.getName().replace(".xlsm", "-saved.xlsm"));
            final FileOutputStream fos = new FileOutputStream(reSavedFile);
            wb.write(fos);
            fos.close();
            */
    } finally {
        wb.close();
    }
}
Also used : XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Ptg(org.apache.poi.ss.formula.ptg.Ptg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) NameXPxg(org.apache.poi.ss.formula.ptg.NameXPxg) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) Test(org.junit.Test)

Example 35 with XSSFWorkbook

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

the class TestFormulaParser method testParseExternalReferencesWithQuotedSheetName.

// bug 60219: FormulaParser can't parse external references when sheet name is quoted
@Test
public void testParseExternalReferencesWithQuotedSheetName() throws Exception {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFEvaluationWorkbook fpwb = XSSFEvaluationWorkbook.create(wb);
    Ptg[] ptgs = FormulaParser.parse("'[1]Sheet 1'!A1", fpwb, FormulaType.CELL, -1);
    // org.apache.poi.ss.formula.ptg.Ref3DPxg [ [workbook=1] sheet=Sheet 1 ! A1]
    assertEquals("Ptgs length", 1, ptgs.length);
    assertTrue("Ptg class", ptgs[0] instanceof Ref3DPxg);
    Ref3DPxg pxg = (Ref3DPxg) ptgs[0];
    assertEquals("External workbook number", 1, pxg.getExternalWorkbookNumber());
    assertEquals("Sheet name", "Sheet 1", pxg.getSheetName());
    assertEquals("Row", 0, pxg.getRow());
    assertEquals("Column", 0, pxg.getColumn());
    wb.close();
}
Also used : XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Ptg(org.apache.poi.ss.formula.ptg.Ptg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Ref3DPxg(org.apache.poi.ss.formula.ptg.Ref3DPxg) 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