Search in sources :

Example 6 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project poi by apache.

the class XSSFWorkbook method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    try {
        WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        this.workbook = doc.getWorkbook();
        ThemesTable theme = null;
        Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
        Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof SharedStringsTable) {
                sharedStringSource = (SharedStringsTable) p;
            } else if (p instanceof StylesTable) {
                stylesSource = (StylesTable) p;
            } else if (p instanceof ThemesTable) {
                theme = (ThemesTable) p;
            } else if (p instanceof CalculationChain) {
                calcChain = (CalculationChain) p;
            } else if (p instanceof MapInfo) {
                mapInfo = (MapInfo) p;
            } else if (p instanceof XSSFSheet) {
                shIdMap.put(rp.getRelationship().getId(), (XSSFSheet) p);
            } else if (p instanceof ExternalLinksTable) {
                elIdMap.put(rp.getRelationship().getId(), (ExternalLinksTable) p);
            }
        }
        boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
        if (stylesSource == null) {
            // Create Styles if it is missing
            if (packageReadOnly) {
                stylesSource = new StylesTable();
            } else {
                stylesSource = (StylesTable) createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
            }
        }
        stylesSource.setWorkbook(this);
        stylesSource.setTheme(theme);
        if (sharedStringSource == null) {
            // Create SST if it is missing
            if (packageReadOnly) {
                sharedStringSource = new SharedStringsTable();
            } else {
                sharedStringSource = (SharedStringsTable) createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
            }
        }
        // Load individual sheets. The order of sheets is defined by the order
        //  of CTSheet elements in the workbook
        sheets = new ArrayList<XSSFSheet>(shIdMap.size());
        for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
            parseSheet(shIdMap, ctSheet);
        }
        // Load the external links tables. Their order is defined by the order 
        //  of CTExternalReference elements in the workbook
        externalLinks = new ArrayList<ExternalLinksTable>(elIdMap.size());
        if (this.workbook.isSetExternalReferences()) {
            for (CTExternalReference er : this.workbook.getExternalReferences().getExternalReferenceArray()) {
                ExternalLinksTable el = elIdMap.get(er.getId());
                if (el == null) {
                    logger.log(POILogger.WARN, "ExternalLinksTable with r:id " + er.getId() + " was defined, but didn't exist in package, skipping");
                    continue;
                }
                externalLinks.add(el);
            }
        }
        // Process the named ranges
        reprocessNamedRanges();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : WorkbookDocument(org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument) ThemesTable(org.apache.poi.xssf.model.ThemesTable) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) CTExternalReference(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) StylesTable(org.apache.poi.xssf.model.StylesTable) POIXMLException(org.apache.poi.POIXMLException) CalculationChain(org.apache.poi.xssf.model.CalculationChain) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) XmlException(org.apache.xmlbeans.XmlException) MapInfo(org.apache.poi.xssf.model.MapInfo) ExternalLinksTable(org.apache.poi.xssf.model.ExternalLinksTable)

Example 7 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project poi by apache.

the class TestReadOnlySharedStringsTable method testParse.

public void testParse() throws Exception {
    OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("SampleSS.xlsx"));
    List<PackagePart> parts = pkg.getPartsByName(Pattern.compile("/xl/sharedStrings.xml"));
    assertEquals(1, parts.size());
    SharedStringsTable stbl = new SharedStringsTable(parts.get(0));
    ReadOnlySharedStringsTable rtbl = new ReadOnlySharedStringsTable(parts.get(0));
    assertEquals(stbl.getCount(), rtbl.getCount());
    assertEquals(stbl.getUniqueCount(), rtbl.getUniqueCount());
    assertEquals(stbl.getItems().size(), stbl.getUniqueCount());
    assertEquals(rtbl.getItems().size(), rtbl.getUniqueCount());
    for (int i = 0; i < stbl.getUniqueCount(); i++) {
        CTRst i1 = stbl.getEntryAt(i);
        String i2 = rtbl.getEntryAt(i);
        assertEquals(i1.getT(), i2);
    }
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 8 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project poi by apache.

the class TestXSSFCell method test47278.

/**
     *  Bug 47278 -  xsi:nil attribute for <t> tag caused Excel 2007 to fail to open workbook
     */
@Test
public void test47278() throws IOException {
    XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.createWorkbook();
    Sheet sheet = wb.createSheet();
    Row row = sheet.createRow(0);
    SharedStringsTable sst = wb.getSharedStringSource();
    assertEquals(0, sst.getCount());
    //case 1. cell.setCellValue(new XSSFRichTextString((String)null));
    Cell cell_0 = row.createCell(0);
    RichTextString str = new XSSFRichTextString((String) null);
    assertNull(str.getString());
    cell_0.setCellValue(str);
    assertEquals(0, sst.getCount());
    assertEquals(CellType.BLANK, cell_0.getCellTypeEnum());
    //case 2. cell.setCellValue((String)null);
    Cell cell_1 = row.createCell(1);
    cell_1.setCellValue((String) null);
    assertEquals(0, sst.getCount());
    assertEquals(CellType.BLANK, cell_1.getCellTypeEnum());
    wb.close();
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) Row(org.apache.poi.ss.usermodel.Row) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Sheet(org.apache.poi.ss.usermodel.Sheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) Cell(org.apache.poi.ss.usermodel.Cell) BaseTestXCell(org.apache.poi.ss.usermodel.BaseTestXCell) Test(org.junit.Test)

Example 9 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project dataverse by IQSS.

the class XLSXFileReader method processSheet.

public void processSheet(InputStream inputStream, DataTable dataTable, PrintWriter tempOut) throws Exception {
    dbglog.info("entering processSheet");
    OPCPackage pkg = OPCPackage.open(inputStream);
    XSSFReader r = new XSSFReader(pkg);
    SharedStringsTable sst = r.getSharedStringsTable();
    XMLReader parser = fetchSheetParser(sst, dataTable, tempOut);
    // rId2 found by processing the Workbook
    // Seems to either be rId# or rSheet#
    InputStream sheet1 = r.getSheet("rId1");
    InputSource sheetSource = new InputSource(sheet1);
    parser.parse(sheetSource);
    sheet1.close();
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 10 with SharedStringsTable

use of org.apache.poi.xssf.model.SharedStringsTable in project data-prep by Talend.

the class StreamingSheetTest method setUp.

@Before
public void setUp() throws Exception {
    OPCPackage pkg = OPCPackage.open(StreamingSheetTest.class.getResourceAsStream("../dates.xlsx"));
    XSSFReader reader = new XSSFReader(pkg);
    SharedStringsTable sst = reader.getSharedStringsTable();
    StylesTable styles = reader.getStylesTable();
    Iterator<InputStream> iter = reader.getSheetsData();
    XMLEventReader parser = XMLInputFactory.newInstance().createXMLEventReader(iter.next());
    final StreamingSheetReader streamingSheetReader = new StreamingSheetReader(sst, styles, parser, 10);
    streamingSheet = new StreamingSheet("name", streamingSheetReader);
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) StylesTable(org.apache.poi.xssf.model.StylesTable) XMLEventReader(javax.xml.stream.XMLEventReader) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) Before(org.junit.Before)

Aggregations

SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)13 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)8 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)7 InputStream (java.io.InputStream)5 InputSource (org.xml.sax.InputSource)5 XMLReader (org.xml.sax.XMLReader)5 StylesTable (org.apache.poi.xssf.model.StylesTable)4 Test (org.junit.Test)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Row (org.apache.poi.ss.usermodel.Row)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 CTRst (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)2 OpenException (com.monitorjbl.xlsx.exceptions.OpenException)1 ReadException (com.monitorjbl.xlsx.exceptions.ReadException)1 File (java.io.File)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1