use of org.apache.poi.POIXMLException in project poi by apache.
the class XMLSlideShow method addPicture.
/**
* Adds a picture to the workbook.
*
* @param pictureData The bytes of the picture
* @param format The format of the picture.
*
* @return the picture data
*/
@Override
public XSLFPictureData addPicture(byte[] pictureData, PictureType format) {
XSLFPictureData img = findPictureData(pictureData);
if (img != null) {
return img;
}
int imageNumber = _pictures.size();
XSLFRelation relType = XSLFPictureData.getRelationForType(format);
if (relType == null) {
throw new IllegalArgumentException("Picture type " + format + " is not supported.");
}
img = (XSLFPictureData) createRelationship(relType, XSLFFactory.getInstance(), imageNumber + 1, true).getDocumentPart();
img.setIndex(imageNumber);
_pictures.add(img);
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
out.close();
} catch (IOException e) {
throw new POIXMLException(e);
}
return img;
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XSSFWorkbook method addPicture.
/**
* Adds a picture to the workbook.
*
* @param pictureData The bytes of the picture
* @param format The format of the picture.
*
* @return the index to this picture (0 based), the added picture can be obtained from {@link #getAllPictures()} .
* @see Workbook#PICTURE_TYPE_EMF
* @see Workbook#PICTURE_TYPE_WMF
* @see Workbook#PICTURE_TYPE_PICT
* @see Workbook#PICTURE_TYPE_JPEG
* @see Workbook#PICTURE_TYPE_PNG
* @see Workbook#PICTURE_TYPE_DIB
* @see #getAllPictures()
*/
@Override
public int addPicture(byte[] pictureData, int format) {
int imageNumber = getAllPictures().size() + 1;
XSSFPictureData img = (XSSFPictureData) createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart();
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
out.close();
} catch (IOException e) {
throw new POIXMLException(e);
}
pictures.add(img);
return imageNumber - 1;
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XSSFWorkbook method setVBAProject.
/**
* Adds a vbaProject.bin file to the workbook. This will change the workbook
* type if necessary.
*
* @throws IOException
*/
public void setVBAProject(InputStream vbaProjectStream) throws IOException {
if (!isMacroEnabled()) {
setWorkbookType(XSSFWorkbookType.XLSM);
}
PackagePartName ppName;
try {
ppName = PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName());
} catch (InvalidFormatException e) {
throw new POIXMLException(e);
}
OPCPackage opc = getPackage();
OutputStream outputStream;
if (!opc.containPart(ppName)) {
POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
outputStream = relationship.getPackagePart().getOutputStream();
} else {
PackagePart part = opc.getPart(ppName);
outputStream = part.getOutputStream();
}
try {
IOUtils.copy(vbaProjectStream, outputStream);
} finally {
IOUtils.closeQuietly(outputStream);
}
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XSSFWorkbook method cloneSheet.
/**
* Create an XSSFSheet from an existing sheet in the XSSFWorkbook.
* The cloned sheet is a deep copy of the original but with a new given
* name.
*
* @param sheetNum The index of the sheet to clone
* @param newName The name to set for the newly created sheet
* @return XSSFSheet representing the cloned sheet.
* @throws IllegalArgumentException if the sheet index or the sheet
* name is invalid
* @throws POIXMLException if there were errors when cloning
*/
public XSSFSheet cloneSheet(int sheetNum, String newName) {
validateSheetIndex(sheetNum);
XSSFSheet srcSheet = sheets.get(sheetNum);
if (newName == null) {
String srcName = srcSheet.getSheetName();
newName = getUniqueSheetName(srcName);
} else {
validateSheetName(newName);
}
XSSFSheet clonedSheet = createSheet(newName);
// copy sheet's relations
List<RelationPart> rels = srcSheet.getRelationParts();
// if the sheet being cloned has a drawing then rememebr it and re-create it too
XSSFDrawing dg = null;
for (RelationPart rp : rels) {
POIXMLDocumentPart r = rp.getDocumentPart();
// do not copy the drawing relationship, it will be re-created
if (r instanceof XSSFDrawing) {
dg = (XSSFDrawing) r;
continue;
}
addRelation(rp, clonedSheet);
}
try {
for (PackageRelationship pr : srcSheet.getPackagePart().getRelationships()) {
if (pr.getTargetMode() == TargetMode.EXTERNAL) {
clonedSheet.getPackagePart().addExternalRelationship(pr.getTargetURI().toASCIIString(), pr.getRelationshipType(), pr.getId());
}
}
} catch (InvalidFormatException e) {
throw new POIXMLException("Failed to clone sheet", e);
}
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
srcSheet.write(out);
clonedSheet.read(new ByteArrayInputStream(out.toByteArray()));
} catch (IOException e) {
throw new POIXMLException("Failed to clone sheet", e);
}
CTWorksheet ct = clonedSheet.getCTWorksheet();
if (ct.isSetLegacyDrawing()) {
logger.log(POILogger.WARN, "Cloning sheets with comments is not yet supported.");
ct.unsetLegacyDrawing();
}
if (ct.isSetPageSetup()) {
logger.log(POILogger.WARN, "Cloning sheets with page setup is not yet supported.");
ct.unsetPageSetup();
}
clonedSheet.setSelected(false);
// clone the sheet drawing alongs with its relationships
if (dg != null) {
if (ct.isSetDrawing()) {
// unset the existing reference to the drawing,
// so that subsequent call of clonedSheet.createDrawingPatriarch() will create a new one
ct.unsetDrawing();
}
XSSFDrawing clonedDg = clonedSheet.createDrawingPatriarch();
// copy drawing contents
clonedDg.getCTDrawing().set(dg.getCTDrawing());
clonedDg = clonedSheet.createDrawingPatriarch();
// Clone drawing relations
List<RelationPart> srcRels = srcSheet.createDrawingPatriarch().getRelationParts();
for (RelationPart rp : srcRels) {
addRelation(rp, clonedDg);
}
}
return clonedSheet;
}
use of org.apache.poi.POIXMLException in project poi by apache.
the class XSSFSheet method read.
protected void read(InputStream is) throws IOException {
try {
worksheet = WorksheetDocument.Factory.parse(is, DEFAULT_XML_OPTIONS).getWorksheet();
} catch (XmlException e) {
throw new POIXMLException(e);
}
initRows(worksheet);
columnHelper = new ColumnHelper(worksheet);
// Look for bits we're interested in
for (RelationPart rp : getRelationParts()) {
POIXMLDocumentPart p = rp.getDocumentPart();
if (p instanceof CommentsTable) {
sheetComments = (CommentsTable) p;
}
if (p instanceof XSSFTable) {
tables.put(rp.getRelationship().getId(), (XSSFTable) p);
}
if (p instanceof XSSFPivotTable) {
getWorkbook().getPivotTables().add((XSSFPivotTable) p);
}
}
// Process external hyperlinks for the sheet, if there are any
initHyperlinks();
}
Aggregations