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();
}
}
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();
}
}
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();
}
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);
}
}
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());
}
Aggregations