use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSLFTextRun method getFontColor.
@Override
public PaintStyle getFontColor() {
final boolean hasPlaceholder = getParentParagraph().getParentShape().getPlaceholder() != null;
CharacterPropertyFetcher<PaintStyle> fetcher = new CharacterPropertyFetcher<PaintStyle>(_p.getIndentLevel()) {
public boolean fetch(CTTextCharacterProperties props) {
if (props == null) {
return false;
}
XSLFShape shape = _p.getParentShape();
CTShapeStyle style = shape.getSpStyle();
CTSchemeColor phClr = null;
if (style != null && style.getFontRef() != null) {
phClr = style.getFontRef().getSchemeClr();
}
XSLFFillProperties fp = XSLFPropertiesDelegate.getFillDelegate(props);
XSLFSheet sheet = shape.getSheet();
PackagePart pp = sheet.getPackagePart();
XSLFTheme theme = sheet.getTheme();
PaintStyle ps = XSLFShape.selectPaint(fp, phClr, pp, theme, hasPlaceholder);
if (ps != null) {
setValue(ps);
return true;
}
return false;
}
};
fetchCharacterProperty(fetcher);
return fetcher.getValue();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSSFSheet method commit.
@Override
protected void commit() throws IOException {
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
write(out);
out.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSSFSheet method createPivotTable.
/**
* Creates an empty XSSFPivotTable and sets up all its relationships
* including: pivotCacheDefinition, pivotCacheRecords
* @return returns a pivotTable
*/
@SuppressWarnings("resource")
@Beta
private XSSFPivotTable createPivotTable() {
XSSFWorkbook wb = getWorkbook();
List<XSSFPivotTable> pivotTables = wb.getPivotTables();
int tableId = getWorkbook().getPivotTables().size() + 1;
//Create relationship between pivotTable and the worksheet
XSSFPivotTable pivotTable = (XSSFPivotTable) createRelationship(XSSFRelation.PIVOT_TABLE, XSSFFactory.getInstance(), tableId);
pivotTable.setParentSheet(this);
pivotTables.add(pivotTable);
XSSFWorkbook workbook = getWorkbook();
//Create relationship between the pivot cache defintion and the workbook
XSSFPivotCacheDefinition pivotCacheDefinition = (XSSFPivotCacheDefinition) workbook.createRelationship(XSSFRelation.PIVOT_CACHE_DEFINITION, XSSFFactory.getInstance(), tableId);
String rId = workbook.getRelationId(pivotCacheDefinition);
//Create relationship between pivotTable and pivotCacheDefinition without creating a new instance
PackagePart pivotPackagePart = pivotTable.getPackagePart();
pivotPackagePart.addRelationship(pivotCacheDefinition.getPackagePart().getPartName(), TargetMode.INTERNAL, XSSFRelation.PIVOT_CACHE_DEFINITION.getRelation());
pivotTable.setPivotCacheDefinition(pivotCacheDefinition);
//Create pivotCache and sets up it's relationship with the workbook
pivotTable.setPivotCache(new XSSFPivotCache(workbook.addPivotCache(rId)));
//Create relationship between pivotcacherecord and pivotcachedefinition
XSSFPivotCacheRecords pivotCacheRecords = (XSSFPivotCacheRecords) pivotCacheDefinition.createRelationship(XSSFRelation.PIVOT_CACHE_RECORDS, XSSFFactory.getInstance(), tableId);
//Set relationships id for pivotCacheDefinition to pivotCacheRecords
pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().setId(pivotCacheDefinition.getRelationId(pivotCacheRecords));
wb.setPivotTables(pivotTables);
return pivotTable;
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XSSFWorkbook method getAllPictures.
/**
* Gets all pictures from the Workbook.
*
* @return the list of pictures (a list of {@link XSSFPictureData} objects.)
* @see #addPicture(byte[], int)
*/
@Override
public List<XSSFPictureData> getAllPictures() {
if (pictures == null) {
List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
pictures = new ArrayList<XSSFPictureData>(mediaParts.size());
for (PackagePart part : mediaParts) {
pictures.add(new XSSFPictureData(part));
}
}
//YK: should return Collections.unmodifiableList(pictures);
return pictures;
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class UpdateEmbeddedDoc method updateEmbeddedDoc.
/**
* Called to update the embedded Excel workbook. As the format and structire
* of the workbook are known in advance, all this code attempts to do is
* write a new value into the first cell on the first row of the first
* worksheet. Prior to executing this method, that cell will contain the
* value 1.
*
* @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
* Rather
* than use the specific classes (HSSF/XSSF) to handle the embedded
* workbook this method uses those defeined in the SS stream. As
* a result, it might be the case that a SpreadsheetML file is
* opened for processing, throwing this exception if that file is
* invalid.
* @throws java.io.IOException Thrown if a problem occurs in the underlying
* file system.
*/
public void updateEmbeddedDoc() throws OpenXML4JException, IOException {
List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
for (PackagePart pPart : embeddedDocs) {
String ext = pPart.getPartName().getExtension();
if (BINARY_EXTENSION.equals(ext) || OPENXML_EXTENSION.equals(ext)) {
// Get an InputStream from the package part and pass that
// to the create method of the WorkbookFactory class. Update
// the resulting Workbook and then stream that out again
// using an OutputStream obtained from the same PackagePart.
InputStream is = pPart.getInputStream();
Workbook workbook = null;
OutputStream os = null;
try {
workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(SHEET_NUM);
Row row = sheet.getRow(ROW_NUM);
Cell cell = row.getCell(CELL_NUM);
cell.setCellValue(NEW_VALUE);
os = pPart.getOutputStream();
workbook.write(os);
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(is);
}
}
}
if (!embeddedDocs.isEmpty()) {
// Finally, write the newly modified Word document out to file.
FileOutputStream fos = new FileOutputStream(this.docFile);
this.doc.write(fos);
fos.close();
}
}
Aggregations