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