* @deprecated POI 3.17 beta 1
* @see Units#DEFAULT_CHARACTER_WIDTH
*/
- @Removal(version="3.19")
+ @Removal(version="4.1")
public static final float DEFAULT_CHARACTER_WIDTH = Units.DEFAULT_CHARACTER_WIDTH;
/**
* TODO
*/
private CalculationChain calcChain;
-
+
/**
* External Links, for referencing names or cells in other workbooks.
*/
private List<XSSFPivotTable> pivotTables;
private List<CTPivotCache> pivotCaches;
+ private final XSSFFactory xssfFactory;
/**
* Create a new SpreadsheetML workbook.
this(XSSFWorkbookType.XLSX);
}
+ @Internal
+ public XSSFWorkbook(XSSFFactory factory) {
+ this(XSSFWorkbookType.XLSX, factory);
+ }
+
/**
* Create a new SpreadsheetML workbook.
* @param workbookType The type of workbook to make (.xlsx or .xlsm).
*/
public XSSFWorkbook(XSSFWorkbookType workbookType) {
+ this(workbookType, null);
+ }
+
+ private XSSFWorkbook(XSSFWorkbookType workbookType, XSSFFactory factory) {
super(newPackage(workbookType));
+ this.xssfFactory = (factory == null) ? XSSFFactory.getInstance() : factory;
onWorkbookCreate();
}
/**
* Constructs a XSSFWorkbook object given a OpenXML4J <code>Package</code> object,
* see <a href="http://poi.apache.org/oxml4j/">http://poi.apache.org/oxml4j/</a>.
- *
+ *
* <p>Once you have finished working with the Workbook, you should close the package
- * by calling either {@link #close()} or {@link OPCPackage#close()}, to avoid
+ * by calling either {@link #close()} or {@link OPCPackage#close()}, to avoid
* leaving file handles open.
- *
+ *
* <p>Creating a XSSFWorkbook from a file-backed OPC Package has a lower memory
* footprint than an InputStream backed one.
*
*/
public XSSFWorkbook(OPCPackage pkg) throws IOException {
super(pkg);
-
+ this.xssfFactory = XSSFFactory.getInstance();
+
beforeDocumentRead();
-
+
// Build a tree of POIXMLDocumentParts, this workbook being the root
- load(XSSFFactory.getInstance());
-
+ load(this.xssfFactory);
+
// some broken Workbooks miss this...
setBookViewsIfMissing();
}
/**
* Constructs a XSSFWorkbook object, by buffering the whole stream into memory
* and then opening an {@link OPCPackage} object for it.
- *
+ *
* <p>Using an {@link InputStream} requires more memory than using a File, so
* if a {@link File} is available then you should instead do something like
* <pre><code>
/**
* Constructs a XSSFWorkbook object from a given file.
- *
- * <p>Once you have finished working with the Workbook, you should close
- * the package by calling {@link #close()}, to avoid leaving file
+ *
+ * <p>Once you have finished working with the Workbook, you should close
+ * the package by calling {@link #close()}, to avoid leaving file
* handles open.
- *
- * <p>Opening a XSSFWorkbook from a file has a lower memory footprint
+ *
+ * <p>Opening a XSSFWorkbook from a file has a lower memory footprint
* than opening from an InputStream
- *
+ *
* @param file the file to open
*/
public XSSFWorkbook(File file) throws IOException, InvalidFormatException {
this(OPCPackage.open(file));
}
-
+
/**
* Constructs a XSSFWorkbook object given a file name.
*
- *
- * <p>Once you have finished working with the Workbook, you should close
- * the package by calling {@link #close()}, to avoid leaving file
+ *
+ * <p>Once you have finished working with the Workbook, you should close
+ * the package by calling {@link #close()}, to avoid leaving file
* handles open.
- *
- * <p>Opening a XSSFWorkbook from a file has a lower memory footprint
+ *
+ * <p>Opening a XSSFWorkbook from a file has a lower memory footprint
* than opening from an InputStream
*
* @param path the file name.
public XSSFWorkbook(String path) throws IOException {
this(openPackage(path));
}
-
+
/**
* Constructs a XSSFWorkbook object using Package Part.
* @param part package part
public XSSFWorkbook(PackagePart part) throws IOException {
this(part.getInputStream());
}
-
+
protected void beforeDocumentRead() {
// Ensure it isn't a XLSB file, which we don't support
if (getCorePart().getContentType().equals(XSSFRelation.XLSB_BINARY_WORKBOOK.getContentType())) {
}
}
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 = (StylesTable)createRelationship(XSSFRelation.STYLES, this.xssfFactory);
}
}
stylesSource.setWorkbook(this);
if (packageReadOnly) {
sharedStringSource = new SharedStringsTable();
} else {
- sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+ sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, this.xssfFactory);
}
}
-
+
// Load individual sheets. The order of sheets is defined by the order
// of CTSheet elements in the workbook
sheets = new ArrayList<>(shIdMap.size());
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
parseSheet(shIdMap, ctSheet);
}
-
- // Load the external links tables. Their order is defined by the order
+
+ // Load the external links tables. Their order is defined by the order
// of CTExternalReference elements in the workbook
externalLinks = new ArrayList<>(elIdMap.size());
if (this.workbook.isSetExternalReferences()) {
externalLinks.add(el);
}
}
-
+
// Process the named ranges
reprocessNamedRanges();
} catch (XmlException e) {
POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties();
expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR);
- sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
- stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
+ sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, this.xssfFactory);
+ stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, this.xssfFactory);
stylesSource.setWorkbook(this);
namedRanges = new ArrayList<>();
sheets = new ArrayList<>();
pivotTables = new ArrayList<>();
}
-
+
private void setBookViewsIfMissing() {
if(!workbook.isSetBookViews()) {
CTBookViews bvs = workbook.addNewBookViews();
@Override
public int addPicture(byte[] pictureData, int format) {
int imageNumber = getAllPictures().size() + 1;
- XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart();
+ XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], this.xssfFactory, imageNumber, true).getDocumentPart();
try (OutputStream out = img.getPackagePart().getOutputStream()) {
out.write(pictureData);
} catch (IOException e){
*/
public int addPicture(InputStream is, int format) throws IOException {
int imageNumber = getAllPictures().size() + 1;
- XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart();
+ XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], this.xssfFactory, imageNumber, true).getDocumentPart();
try (OutputStream out = img.getPackagePart().getOutputStream()) {
IOUtils.copy(is, out);
}
return cloneSheet(sheetNum, null);
}
+ @Override
+ public void close() throws IOException {
+ super.close();
+ sharedStringSource.close();
+ }
+
/**
* 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
for(PackageRelationship pr : srcSheet.getPackagePart().getRelationships()) {
if (pr.getTargetMode() == TargetMode.EXTERNAL) {
clonedSheet.getPackagePart().addExternalRelationship
- (pr.getTargetURI().toASCIIString(), pr.getRelationshipType(), pr.getId());
+ (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);
try (ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray())) {
clonedDg.getCTDrawing().set(dg.getCTDrawing());
clonedDg = clonedSheet.createDrawingPatriarch();
-
+
// Clone drawing relations
List<RelationPart> srcRels = srcSheet.createDrawingPatriarch().getRelationParts();
for (RelationPart rp : srcRels) {
}
return clonedSheet;
}
-
+
/**
* @since 3.14-Beta1
*/
PackageRelationship rel = rp.getRelationship();
if (rel.getTargetMode() == TargetMode.EXTERNAL) {
target.getPackagePart().addRelationship(
- rel.getTargetURI(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
- } else {
+ rel.getTargetURI(), rel.getTargetMode(), rel.getRelationshipType(), rel.getId());
+ } else {
XSSFRelation xssfRel = XSSFRelation.getInstance(rel.getRelationshipType());
if (xssfRel == null) {
// Don't copy all relations blindly, but only the ones we know about
for(XSSFSheet sh : sheets) {
sheetNumber = (int)Math.max(sh.sheet.getSheetId() + 1, sheetNumber);
}
-
+
// Bug 57165: We also need to check that the resulting file name is not already taken
// this can happen when moving/cloning sheets
String sheetName = XSSFRelation.WORKSHEET.getFileName(sheetNumber);
for(POIXMLDocumentPart relation : getRelations()) {
- if(relation.getPackagePart() != null &&
+ if(relation.getPackagePart() != null &&
sheetName.equals(relation.getPackagePart().getPartName().getName())) {
// name is taken => try next one
sheetNumber++;
break;
}
- RelationPart rp = createRelationship(XSSFRelation.WORKSHEET, XSSFFactory.getInstance(), sheetNumber, false);
+ RelationPart rp = createRelationship(XSSFRelation.WORKSHEET, this.xssfFactory, sheetNumber, false);
XSSFSheet wrapper = rp.getDocumentPart();
wrapper.sheet = sheet;
sheet.setId(rp.getRelationship().getId());
sheet.setName(sheetname);
return sheet;
}
-
+
/**
* Finds a font that matches the one with the supplied attributes
*/
public Iterator<Sheet> sheetIterator() {
return new SheetIterator<>();
}
-
+
/**
* Alias for {@link #sheetIterator()} to allow
* foreach loops
- *
+ *
* Note: remove() is not supported on this iterator.
* Use {@link #removeSheetAt(int)} to remove sheets instead.
- *
+ *
* @return an iterator of the sheets.
*/
@Override
public Iterator<Sheet> iterator() {
return sheetIterator();
}
-
+
private final class SheetIterator<T extends Sheet> implements Iterator<T> {
final private Iterator<T> it;
@SuppressWarnings("unchecked")
"Use Sheet.removeSheetAt(int) instead.");
}
}
-
+
/**
* Are we a normal workbook (.xlsx), or a
* macro enabled workbook (.xlsm)?
/**
* Remove the first named range found with the given name.
- *
+ *
* Note: names of named ranges are not unique (name + sheet
* index is unique), so {@link #removeName(Name)} should
* be used if possible.
- *
+ *
* @param name the named range name to remove
*
* @throws IllegalArgumentException if no named range could be found
final XSSFSheet sheet = getSheetAt(index);
sheet.onSheetDelete();
-
+
//delete the CTSheet reference from workbook.xml
workbook.getSheets().removeSheet(index);
* @param index the index to validate
* @throws IllegalArgumentException if the index is out of range (index
* < 0 || index >= getNumberOfSheets()).
- */
+ */
private void validateSheetIndex(int index) {
int lastSheetIx = sheets.size() - 1;
if (index < 0 || index > lastSheetIx) {
if (sheetname == null) {
throw new IllegalArgumentException( "sheetName must not be null" );
}
-
+
validateSheetIndex(sheetIndex);
String oldSheetName = getSheetName(sheetIndex);
if (sheetname.equals(oldSheetName)) {
return;
}
-
+
// Check it isn't already taken
if (containsSheet(sheetname, sheetIndex )) {
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
for(int i=0; i < sheetArray.length; i++) {
sheets.get(i).sheet = sheetArray[i];
}
-
+
updateNamedRangesAfterSheetReorder(idx, pos);
updateActiveSheetAfterSheetReorder(idx, pos);
}
-
+
/**
* update sheet-scoped named ranges in this workbook after changing the sheet order
* of a sheet at oldIndex to newIndex.
}
}
}
-
+
private void updateActiveSheetAfterSheetReorder(int oldIndex, int newIndex) {
// adjust active sheet if necessary
int active = getActiveSheetIndex();
workbook.unsetDefinedNames();
}
workbook.setDefinedNames(names);
-
+
// Re-process the named ranges
reprocessNamedRanges();
} else {
}
}
}
-
+
private void reprocessNamedRanges() {
namedRangesByName = new ArrayListValuedHashMap<>();
namedRanges = new ArrayList<>();
workbook.save(out, xmlOptions);
}
}
-
+
/**
* Returns SharedStringsTable - tha cache of string for this workbook
*
}
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation())) {
- embedds.add( sheet.getPackagePart().getRelatedPart(rel) );
+ embedds.add( sheet.getPackagePart().getRelatedPart(rel) );
}
}
return embedds;
CTSheet ctSheet = sheets.get(sheetIx).sheet;
return ctSheet.getState() == STSheetState.VERY_HIDDEN;
}
-
+
@Override
public SheetVisibility getSheetVisibility(int sheetIx) {
validateSheetIndex(sheetIx);
@Override
public void setSheetVisibility(int sheetIx, SheetVisibility visibility) {
validateSheetIndex(sheetIx);
-
+
final CTSheet ctSheet = sheets.get(sheetIx).sheet;
switch (visibility) {
case VISIBLE:
throw new IllegalArgumentException("This should never happen");
}
}
-
-
-
+
+
+
/**
* Fired when a formula is deleted from this workbook,
public CalculationChain getCalculationChain() {
return calcChain;
}
-
+
/**
* Returns the list of {@link ExternalLinksTable} object for this workbook
- *
+ *
* <p>The external links table specifies details of named ranges etc
* that are referenced from other workbooks, along with the last seen
* values of what they point to.</p>
}
/**
- * Adds the External Link Table part and relations required to allow formulas
+ * Adds the External Link Table part and relations required to allow formulas
* referencing the specified external workbook to be added to this one. Allows
- * formulas such as "[MyOtherWorkbook.xlsx]Sheet3!$A$5" to be added to the
+ * formulas such as "[MyOtherWorkbook.xlsx]Sheet3!$A$5" to be added to the
* file, for workbooks not already linked / referenced.
*
* Note: this is not implemented and thus currently throws an Exception stating this.
}
/**
- * Sets the workbook password.
- *
+ * Sets the workbook password.
+ *
* @param password if null, the password will be removed
* @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
* otherwise the given algorithm is used for calculating the hash password (Excel 2013)
/**
* Sets the revisions password.
- *
+ *
* @param password if null, the password will be removed
* @param hashAlgo if null, the password will be set as XOR password (Excel 2010 and earlier)
* otherwise the given algorithm is used for calculating the hash password (Excel 2013)
}
return validatePassword(safeGetWorkbookProtection(), password, "revisions");
}
-
+
/**
* Removes the workbook protection settings
*/
workbook.unsetWorkbookProtection();
}
}
-
+
private boolean workbookProtectionPresent() {
return workbook.isSetWorkbookProtection();
}
}
return workbook.getWorkbookProtection();
}
-
+
/**
*
* Returns the locator of user-defined functions.
OPCPackage opc = getPackage();
OutputStream outputStream;
if (!opc.containPart(ppName)) {
- POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, XSSFFactory.getInstance());
+ POIXMLDocumentPart relationship = createRelationship(XSSFRelation.VBA_MACROS, this.xssfFactory);
outputStream = relationship.getPackagePart().getOutputStream();
} else {
PackagePart part = opc.getPart(ppName);
/**
* Returns the spreadsheet version (EXCLE2007) of this workbook
- *
+ *
* @return EXCEL2007 SpreadsheetVersion enum
* @since 3.14 beta 2
*/
public SpreadsheetVersion getSpreadsheetVersion() {
return SpreadsheetVersion.EXCEL2007;
}
-
+
/**
* Returns the data table with the given name (case insensitive).
- *
+ *
* @param name the data table name (case-insensitive)
* @return The Data table in the workbook named <tt>name</tt>, or <tt>null</tt> if no table is named <tt>name</tt>.
* @since 3.15 beta 2
@Override
public int addOlePackage(byte[] oleData, String label, String fileName, String command)
- throws IOException {
+ throws IOException {
// find an unused part name
OPCPackage opc = getPackage();
PackagePartName pnOLE;
} while (opc.containPart(pnOLE));
PackagePart pp = opc.createPart( pnOLE, "application/vnd.openxmlformats-officedocument.oleObject" );
-
+
Ole10Native ole10 = new Ole10Native(label, fileName, command, oleData);
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(oleData.length+500)) {