use of org.apache.poi.xssf.model.StylesTable 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.StylesTable in project poi by apache.
the class TestXSSFWorkbook method getCellStyleAt.
@Test
public void getCellStyleAt() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
try {
short i = 0;
//get default style
CellStyle cellStyleAt = workbook.getCellStyleAt(i);
assertNotNull(cellStyleAt);
//get custom style
StylesTable styleSource = workbook.getStylesSource();
XSSFCellStyle customStyle = new XSSFCellStyle(styleSource);
XSSFFont font = new XSSFFont();
font.setFontName("Verdana");
customStyle.setFont(font);
int x = styleSource.putStyle(customStyle);
cellStyleAt = workbook.getCellStyleAt((short) x);
assertNotNull(cellStyleAt);
} finally {
workbook.close();
}
}
use of org.apache.poi.xssf.model.StylesTable in project poi by apache.
the class TestXSSFWorkbook method styles.
@Test
public void styles() throws IOException {
XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
StylesTable ss = wb1.getStylesSource();
assertNotNull(ss);
StylesTable st = ss;
// Has 8 number formats
assertEquals(8, st.getNumDataFormats());
// Has 2 fonts
assertEquals(2, st.getFonts().size());
// Has 2 fills
assertEquals(2, st.getFills().size());
// Has 1 border
assertEquals(1, st.getBorders().size());
// Add two more styles
assertEquals(StylesTable.FIRST_CUSTOM_STYLE_ID + 8, st.putNumberFormat("testFORMAT"));
assertEquals(StylesTable.FIRST_CUSTOM_STYLE_ID + 8, st.putNumberFormat("testFORMAT"));
assertEquals(StylesTable.FIRST_CUSTOM_STYLE_ID + 9, st.putNumberFormat("testFORMAT2"));
assertEquals(10, st.getNumDataFormats());
// Save, load back in again, and check
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
wb1.close();
ss = wb2.getStylesSource();
assertNotNull(ss);
assertEquals(10, st.getNumDataFormats());
assertEquals(2, st.getFonts().size());
assertEquals(2, st.getFills().size());
assertEquals(1, st.getBorders().size());
wb2.close();
}
use of org.apache.poi.xssf.model.StylesTable in project poi by apache.
the class XSSFEventBasedExcelExtractor method getText.
/**
* Processes the file and returns the text
*/
public String getText() {
try {
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(container, concatenatePhoneticRuns);
XSSFReader xssfReader = new XSSFReader(container);
StylesTable styles = xssfReader.getStylesTable();
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) xssfReader.getSheetsData();
StringBuffer text = new StringBuffer();
SheetTextExtractor sheetExtractor = new SheetTextExtractor();
while (iter.hasNext()) {
InputStream stream = iter.next();
if (includeSheetNames) {
text.append(iter.getSheetName());
text.append('\n');
}
CommentsTable comments = includeCellComments ? iter.getSheetComments() : null;
processSheet(sheetExtractor, styles, comments, strings, stream);
if (includeHeadersFooters) {
sheetExtractor.appendHeaderText(text);
}
sheetExtractor.appendCellText(text);
if (includeTextBoxes) {
processShapes(iter.getShapes(), text);
}
if (includeHeadersFooters) {
sheetExtractor.appendFooterText(text);
}
sheetExtractor.reset();
stream.close();
}
return text.toString();
} catch (IOException e) {
LOGGER.log(POILogger.WARN, e);
return null;
} catch (SAXException se) {
LOGGER.log(POILogger.WARN, se);
return null;
} catch (OpenXML4JException o4je) {
LOGGER.log(POILogger.WARN, o4je);
return null;
}
}
use of org.apache.poi.xssf.model.StylesTable in project poi by apache.
the class TestXSSFReader method testSheetWithNoRelationshipId.
/**
* NPE when sheet has no relationship id in the workbook
* 60825
*/
public void testSheetWithNoRelationshipId() throws Exception {
OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("60825.xlsx");
ReadOnlySharedStringsTable strings = new ReadOnlySharedStringsTable(pkg);
assertNotNull(strings);
XSSFReader reader = new XSSFReader(pkg);
StylesTable styles = reader.getStylesTable();
assertNotNull(styles);
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
assertNotNull(iter.next());
assertFalse(iter.hasNext());
pkg.close();
}
Aggregations