Search in sources :

Example 1 with SharedStringsTable

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

the class FromHowTo method processAllSheets.

public void processAllSheets(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();
        XMLReader parser = fetchSheetParser(sst);
        Iterator<InputStream> sheets = r.getSheetsData();
        while (sheets.hasNext()) {
            System.out.println("Processing new sheet:\n");
            InputStream sheet = sheets.next();
            InputSource sheetSource = new InputSource(sheet);
            parser.parse(sheetSource);
            sheet.close();
            System.out.println("");
        }
    } finally {
        pkg.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 2 with SharedStringsTable

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

the class FromHowTo method processFirstSheet.

public void processFirstSheet(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();
        XMLReader parser = fetchSheetParser(sst);
        // process the first sheet
        InputStream sheet2 = r.getSheetsData().next();
        InputSource sheetSource = new InputSource(sheet2);
        parser.parse(sheetSource);
        sheet2.close();
    } finally {
        pkg.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 3 with SharedStringsTable

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

the class TestSXSSFWorkbook method useSharedStringsTable.

@Test
public void useSharedStringsTable() throws Exception {
    SXSSFWorkbook wb = new SXSSFWorkbook(null, 10, false, true);
    SharedStringsTable sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource");
    assertNotNull(sss);
    Row row = wb.createSheet("S1").createRow(0);
    row.createCell(0).setCellValue("A");
    row.createCell(1).setCellValue("B");
    row.createCell(2).setCellValue("A");
    XSSFWorkbook xssfWorkbook = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);
    sss = POITestCase.getFieldValue(SXSSFWorkbook.class, wb, SharedStringsTable.class, "_sharedStringSource");
    assertEquals(2, sss.getUniqueCount());
    assertTrue(wb.dispose());
    Sheet sheet1 = xssfWorkbook.getSheetAt(0);
    assertEquals("S1", sheet1.getSheetName());
    assertEquals(1, sheet1.getPhysicalNumberOfRows());
    row = sheet1.getRow(0);
    assertNotNull(row);
    Cell cell = row.getCell(0);
    assertNotNull(cell);
    assertEquals("A", cell.getStringCellValue());
    cell = row.getCell(1);
    assertNotNull(cell);
    assertEquals("B", cell.getStringCellValue());
    cell = row.getCell(2);
    assertNotNull(cell);
    assertEquals("A", cell.getStringCellValue());
    xssfWorkbook.close();
    wb.close();
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Test(org.junit.Test)

Example 4 with SharedStringsTable

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

the class StreamingWorkbookReader method init.

// to override https://bz.apache.org/bugzilla/show_bug.cgi?id=57699
public void init(File f) {
    try {
        if (builder.getPassword() != null) {
            // Based on: https://poi.apache.org/encryption.html
            POIFSFileSystem poifs = new POIFSFileSystem(f);
            EncryptionInfo info = new EncryptionInfo(poifs);
            Decryptor d = Decryptor.getInstance(info);
            d.verifyPassword(builder.getPassword());
            pkg = OPCPackage.open(d.getDataStream(poifs));
        } else {
            pkg = OPCPackage.open(f);
        }
        XSSFReader reader = new XSSFReader(pkg);
        SharedStringsTable sst = reader.getSharedStringsTable();
        StylesTable styles = reader.getStylesTable();
        loadSheets(reader, sst, styles, builder.getRowCacheSize());
    } catch (IOException e) {
        throw new OpenException("Failed to open file", e);
    } catch (OpenXML4JException | XMLStreamException e) {
        throw new ReadException("Unable to read workbook", e);
    } catch (GeneralSecurityException e) {
        throw new ReadException("Unable to read workbook - Decryption failed", e);
    }
}
Also used : Decryptor(org.apache.poi.poifs.crypt.Decryptor) EncryptionInfo(org.apache.poi.poifs.crypt.EncryptionInfo) GeneralSecurityException(java.security.GeneralSecurityException) StylesTable(org.apache.poi.xssf.model.StylesTable) IOException(java.io.IOException) ReadException(com.monitorjbl.xlsx.exceptions.ReadException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) XMLStreamException(javax.xml.stream.XMLStreamException) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) OpenException(com.monitorjbl.xlsx.exceptions.OpenException) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 5 with SharedStringsTable

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

the class StaxPoiSheetTest method testNoUsedRangeSpecified.

// The row and column bounds of all cells in the worksheet are specified in ref attribute of Dimension tag in sheet
// xml
// But ref can be present as range: <dimension ref="A1:C2"/> or as just one start cell: <dimension ref="A1"/>.
// Below tests to validate correct work for such cases
@Test
public void testNoUsedRangeSpecified() throws Exception {
    final String sheetId = "1";
    final String sheetName = "Sheet 1";
    SharedStringsTable sharedStringsTableMock = mockSharedStringsTable("Report ID", "Report ID", "Approval Status", "Total Report Amount", "Policy", "ReportIdValue_1", "ReportIdValue_1", "ApprovalStatusValue_1", "PolicyValue_1");
    XSSFReader reader = mockXSSFReader(sheetId, SHEET_NO_USED_RANGE_SPECIFIED, sharedStringsTableMock, mock(StylesTable.class));
    StaxPoiSheet spSheet = new StaxPoiSheet(reader, sheetName, sheetId);
    // The first row is empty - it should have empty rowCells
    KCell[] rowCells = spSheet.getRow(0);
    assertEquals(0, rowCells.length);
    // The second row - is the header - just skip it
    rowCells = spSheet.getRow(1);
    assertEquals(0, rowCells.length);
    // The row3 - is the first row with data - validating it
    rowCells = spSheet.getRow(2);
    assertEquals(KCellType.LABEL, rowCells[0].getType());
    assertEquals("ReportIdValue_1", rowCells[0].getValue());
    assertEquals(KCellType.LABEL, rowCells[1].getType());
    assertEquals("ReportIdValue_1", rowCells[1].getValue());
    assertEquals(KCellType.LABEL, rowCells[2].getType());
    assertEquals("ApprovalStatusValue_1", rowCells[2].getValue());
    assertEquals(KCellType.NUMBER, rowCells[3].getType());
    assertEquals(2623.0, rowCells[3].getValue());
    assertEquals(KCellType.LABEL, rowCells[4].getType());
    assertEquals("PolicyValue_1", rowCells[4].getValue());
}
Also used : SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) StylesTable(org.apache.poi.xssf.model.StylesTable) KCell(org.pentaho.di.core.spreadsheet.KCell) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader) Test(org.junit.Test)

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