diff options
author | PJ Fanning <fanningpj@apache.org> | 2020-06-27 09:33:06 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2020-06-27 09:33:06 +0000 |
commit | 38e58f1ead7ff22c7095d1f9f0c71196739467cd (patch) | |
tree | 8608618c0e4e29980336a920a05336f469ae5a59 /src/java/org | |
parent | f99533c56e8b5926f2a448d075365c8b9716ea6a (diff) | |
download | poi-38e58f1ead7ff22c7095d1f9f0c71196739467cd.tar.gz poi-38e58f1ead7ff22c7095d1f9f0c71196739467cd.zip |
remove some deprecated Name methods from Workbook
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879255 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org')
-rw-r--r-- | src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java | 811 | ||||
-rw-r--r-- | src/java/org/apache/poi/ss/usermodel/Workbook.java | 45 |
2 files changed, 397 insertions, 459 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 16d990f348..31fc8d045e 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -141,7 +141,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * The maximum number of cell styles in a .xls workbook. * The 'official' limit is 4,000, but POI allows a slightly larger number. * This extra delta takes into account built-in styles that are automatically - * + * <p> * See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx */ private static final int MAX_STYLES = 4030; @@ -155,7 +155,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * since you're never allowed to have more or less than three sheets! */ - public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFWorkbook.SheetInitialCapacity",3); + public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFWorkbook.SheetInitialCapacity", 3); /** * this is the reference to the low level Workbook object @@ -179,7 +179,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * this holds the HSSFFont objects attached to this workbook. * We only create these from the low level records as required. */ - private Map<Integer,HSSFFont> fonts; + private Map<Integer, HSSFFont> fonts; /** * holds whether or not to preserve other nodes in the POIFS. Used @@ -197,7 +197,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * The policy to apply in the event of missing or - * blank cells when fetching from a row. + * blank cells when fetching from a row. * See {@link MissingCellPolicy} */ private MissingCellPolicy missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK; @@ -211,18 +211,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss private UDFFinder _udfFinder = new IndexedUDFFinder(AggregatingUDFFinder.DEFAULT); public static HSSFWorkbook create(InternalWorkbook book) { - return new HSSFWorkbook(book); + return new HSSFWorkbook(book); } + /** * Creates new HSSFWorkbook from scratch (start here!) - * */ public HSSFWorkbook() { this(InternalWorkbook.createWorkbook()); } private HSSFWorkbook(InternalWorkbook book) { - super((DirectoryNode)null); + super((DirectoryNode) null); workbook = book; _sheets = new ArrayList<>(INITIAL_CAPACITY); names = new ArrayList<>(INITIAL_CAPACITY); @@ -230,29 +230,29 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Given a POI POIFSFileSystem object, read in its Workbook along - * with all related nodes, and populate the high and low level models. + * with all related nodes, and populate the high and low level models. * <p>This calls {@link #HSSFWorkbook(POIFSFileSystem, boolean)} with - * preserve nodes set to true. + * preserve nodes set to true. * + * @throws IOException if the stream cannot be read * @see #HSSFWorkbook(POIFSFileSystem, boolean) * @see org.apache.poi.poifs.filesystem.POIFSFileSystem - * @exception IOException if the stream cannot be read */ public HSSFWorkbook(POIFSFileSystem fs) throws IOException { - this(fs,true); + this(fs, true); } /** * Given a POI POIFSFileSystem object, read in its Workbook and populate * the high and low level models. If you're reading in a workbook... start here! * - * @param fs the POI filesystem that contains the Workbook stream. + * @param fs the POI filesystem that contains the Workbook stream. * @param preserveNodes whether to preserve other nodes, such as - * macros. This takes more memory, so only say yes if you - * need to. If set, will store all of the POIFSFileSystem - * in memory + * macros. This takes more memory, so only say yes if you + * need to. If set, will store all of the POIFSFileSystem + * in memory + * @throws IOException if the stream cannot be read * @see org.apache.poi.poifs.filesystem.POIFSFileSystem - * @exception IOException if the stream cannot be read */ public HSSFWorkbook(POIFSFileSystem fs, boolean preserveNodes) throws IOException { @@ -260,69 +260,67 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } public static String getWorkbookDirEntryName(DirectoryNode directory) { - for(String wbName : WORKBOOK_DIR_ENTRY_NAMES) { - if(directory.hasEntry(wbName)) { + for (String wbName : WORKBOOK_DIR_ENTRY_NAMES) { + if (directory.hasEntry(wbName)) { return wbName; } } // check for an encrypted .xlsx file - they get OLE2 wrapped - if(directory.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { - throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " + - "It must be decrypted before use by XSSF, it cannot be used by HSSF"); + if (directory.hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { + throw new EncryptedDocumentException("The supplied spreadsheet seems to be an Encrypted .xlsx file. " + + "It must be decrypted before use by XSSF, it cannot be used by HSSF"); } // check for previous version of file format - if(directory.hasEntry(OLD_WORKBOOK_DIR_ENTRY_NAME)) { + if (directory.hasEntry(OLD_WORKBOOK_DIR_ENTRY_NAME)) { throw new OldExcelFormatException("The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. " + "POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)"); } // throw more useful exceptions for known wrong file-extensions - if(directory.hasEntry("WordDocument")) { + if (directory.hasEntry("WordDocument")) { throw new IllegalArgumentException("The document is really a DOC file"); } throw new IllegalArgumentException("The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. " - + "Is it really an excel file? Had: " + directory.getEntryNames()); + + "Is it really an excel file? Had: " + directory.getEntryNames()); } /** * given a POI POIFSFileSystem object, and a specific directory - * within it, read in its Workbook and populate the high and - * low level models. If you're reading in a workbook...start here. + * within it, read in its Workbook and populate the high and + * low level models. If you're reading in a workbook...start here. * - * @param directory the POI filesystem directory to process from - * @param fs the POI filesystem that contains the Workbook stream. + * @param directory the POI filesystem directory to process from + * @param fs the POI filesystem that contains the Workbook stream. * @param preserveNodes whether to preserve other nodes, such as - * macros. This takes more memory, so only say yes if you - * need to. If set, will store all of the POIFSFileSystem - * in memory + * macros. This takes more memory, so only say yes if you + * need to. If set, will store all of the POIFSFileSystem + * in memory + * @throws IOException if the stream cannot be read * @see org.apache.poi.poifs.filesystem.POIFSFileSystem - * @exception IOException if the stream cannot be read */ public HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes) - throws IOException - { - this(directory, preserveNodes); + throws IOException { + this(directory, preserveNodes); } /** * given a POI POIFSFileSystem object, and a specific directory - * within it, read in its Workbook and populate the high and - * low level models. If you're reading in a workbook...start here. + * within it, read in its Workbook and populate the high and + * low level models. If you're reading in a workbook...start here. * - * @param directory the POI filesystem directory to process from + * @param directory the POI filesystem directory to process from * @param preserveNodes whether to preserve other nodes, such as - * macros. This takes more memory, so only say yes if you - * need to. If set, will store all of the POIFSFileSystem - * in memory + * macros. This takes more memory, so only say yes if you + * need to. If set, will store all of the POIFSFileSystem + * in memory + * @throws IOException if the stream cannot be read * @see org.apache.poi.poifs.filesystem.POIFSFileSystem - * @exception IOException if the stream cannot be read */ public HSSFWorkbook(DirectoryNode directory, boolean preserveNodes) - throws IOException - { + throws IOException { super(directory); String workbookName = getWorkbookDirEntryName(directory); @@ -330,12 +328,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // If we're not preserving nodes, don't track the // POIFS any more - if(! preserveNodes) { + if (!preserveNodes) { clearDirectory(); } _sheets = new ArrayList<>(INITIAL_CAPACITY); - names = new ArrayList<>(INITIAL_CAPACITY); + names = new ArrayList<>(INITIAL_CAPACITY); // Grab the data from the workbook stream, however // it happens to be spelled. @@ -360,7 +358,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } } - for (int i = 0 ; i < workbook.getNumNames() ; ++i){ + for (int i = 0; i < workbook.getNumNames(); ++i) { NameRecord nameRecord = workbook.getNameRecord(i); HSSFName name = new HSSFName(this, nameRecord, workbook.getNameCommentRecord(nameRecord)); names.add(name); @@ -369,35 +367,34 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Companion to HSSFWorkbook(POIFSFileSystem), this constructs the - * POI filesystem around your {@link InputStream}, including all nodes. + * POI filesystem around your {@link InputStream}, including all nodes. * <p>This calls {@link #HSSFWorkbook(InputStream, boolean)} with - * preserve nodes set to true. + * preserve nodes set to true. * + * @throws IOException if the stream cannot be read * @see #HSSFWorkbook(InputStream, boolean) * @see #HSSFWorkbook(POIFSFileSystem) * @see org.apache.poi.poifs.filesystem.POIFSFileSystem - * @exception IOException if the stream cannot be read */ public HSSFWorkbook(InputStream s) throws IOException { - this(s,true); + this(s, true); } /** * Companion to HSSFWorkbook(POIFSFileSystem), this constructs the * POI filesystem around your {@link InputStream}. * - * @param s the POI filesystem that contains the Workbook stream. + * @param s the POI filesystem that contains the Workbook stream. * @param preserveNodes whether to preserve other nodes, such as - * macros. This takes more memory, so only say yes if you - * need to. + * macros. This takes more memory, so only say yes if you + * need to. + * @throws IOException if the stream cannot be read * @see org.apache.poi.poifs.filesystem.POIFSFileSystem * @see #HSSFWorkbook(POIFSFileSystem) - * @exception IOException if the stream cannot be read */ @SuppressWarnings("resource") // POIFSFileSystem always closes the stream public HSSFWorkbook(InputStream s, boolean preserveNodes) - throws IOException - { + throws IOException { this(new POIFSFileSystem(s).getRoot(), preserveNodes); } @@ -405,65 +402,61 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * used internally to set the workbook properties. */ - private void setPropertiesFromWorkbook(InternalWorkbook book) - { + private void setPropertiesFromWorkbook(InternalWorkbook book) { this.workbook = book; // none currently } /** - * This is basically a kludge to deal with the now obsolete Label records. If - * you have to read in a sheet that contains Label records, be aware that the rest - * of the API doesn't deal with them, the low level structure only provides read-only - * semi-immutable structures (the sets are there for interface conformance with NO - * Implementation). In short, you need to call this function passing it a reference - * to the Workbook object. All labels will be converted to LabelSST records and their - * contained strings will be written to the Shared String table (SSTRecord) within - * the Workbook. - * - * @param records a collection of sheet's records. - * @param offset the offset to search at - * @see org.apache.poi.hssf.record.LabelRecord - * @see org.apache.poi.hssf.record.LabelSSTRecord - * @see org.apache.poi.hssf.record.SSTRecord - */ - - private void convertLabelRecords(List<org.apache.poi.hssf.record.Record> records, int offset) - { - if (log.check( POILogger.DEBUG )) { + * This is basically a kludge to deal with the now obsolete Label records. If + * you have to read in a sheet that contains Label records, be aware that the rest + * of the API doesn't deal with them, the low level structure only provides read-only + * semi-immutable structures (the sets are there for interface conformance with NO + * Implementation). In short, you need to call this function passing it a reference + * to the Workbook object. All labels will be converted to LabelSST records and their + * contained strings will be written to the Shared String table (SSTRecord) within + * the Workbook. + * + * @param records a collection of sheet's records. + * @param offset the offset to search at + * @see org.apache.poi.hssf.record.LabelRecord + * @see org.apache.poi.hssf.record.LabelSSTRecord + * @see org.apache.poi.hssf.record.SSTRecord + */ + + private void convertLabelRecords(List<org.apache.poi.hssf.record.Record> records, int offset) { + if (log.check(POILogger.DEBUG)) { log.log(POILogger.DEBUG, "convertLabelRecords called"); } - for (int k = offset; k < records.size(); k++) - { - Record rec = records.get(k); - - if (rec.getSid() == LabelRecord.sid) - { - LabelRecord oldrec = ( LabelRecord ) rec; - - records.remove(k); - LabelSSTRecord newrec = new LabelSSTRecord(); - int stringid = - workbook.addSSTString(new UnicodeString(oldrec.getValue())); - - newrec.setRow(oldrec.getRow()); - newrec.setColumn(oldrec.getColumn()); - newrec.setXFIndex(oldrec.getXFIndex()); - newrec.setSSTIndex(stringid); - records.add(k, newrec); - } - } - if (log.check( POILogger.DEBUG )) { + for (int k = offset; k < records.size(); k++) { + Record rec = records.get(k); + + if (rec.getSid() == LabelRecord.sid) { + LabelRecord oldrec = (LabelRecord) rec; + + records.remove(k); + LabelSSTRecord newrec = new LabelSSTRecord(); + int stringid = + workbook.addSSTString(new UnicodeString(oldrec.getValue())); + + newrec.setRow(oldrec.getRow()); + newrec.setColumn(oldrec.getColumn()); + newrec.setXFIndex(oldrec.getXFIndex()); + newrec.setSSTIndex(stringid); + records.add(k, newrec); + } + } + if (log.check(POILogger.DEBUG)) { log.log(POILogger.DEBUG, "convertLabelRecords exit"); } - } + } /** * Retrieves the current policy on what to do when - * getting missing or blank cells from a row. + * getting missing or blank cells from a row. * The default is to return blank and null cells. - * {@link MissingCellPolicy} + * {@link MissingCellPolicy} */ @Override public MissingCellPolicy getMissingCellPolicy() { @@ -472,13 +465,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Sets the policy on what to do when - * getting missing or blank cells from a row. + * getting missing or blank cells from a row. * This will then apply to all calls to - * {@link HSSFRow#getCell(int)}}. See - * {@link MissingCellPolicy}. + * {@link HSSFRow#getCell(int)}}. See + * {@link MissingCellPolicy}. * Note that this has no effect on any - * iterators, only on when fetching Cells - * by their column index. + * iterators, only on when fetching Cells + * by their column index. */ @Override public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) { @@ -489,18 +482,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * sets the order of appearance for a given sheet. * * @param sheetname the name of the sheet to reorder - * @param pos the position that we want to insert the sheet into (0 based) + * @param pos the position that we want to insert the sheet into (0 based) */ @Override - public void setSheetOrder(String sheetname, int pos ) { + public void setSheetOrder(String sheetname, int pos) { int oldSheetIndex = getSheetIndex(sheetname); - _sheets.add(pos,_sheets.remove(oldSheetIndex)); + _sheets.add(pos, _sheets.remove(oldSheetIndex)); workbook.setSheetOrder(sheetname, pos); FormulaShifter shifter = FormulaShifter.createForSheetShift(oldSheetIndex, pos); for (HSSFSheet sheet : _sheets) { - sheet.getSheet().updateFormulasAfterCellShift(shifter, /* not used */ -1 ); + sheet.getSheet().updateFormulasAfterCellShift(shifter, /* not used */ -1); } workbook.updateNamesAfterCellShift(shifter); @@ -511,7 +504,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * copy-pasted from XSSFWorkbook#updateNamedRangesAfterSheetReorder(int, int) - * + * <p> * update sheet-scoped named ranges in this workbook after changing the sheet order * of a sheet at oldIndex to newIndex. * Sheets between these indices will move left or right by 1. @@ -531,11 +524,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } // if oldIndex > newIndex then this sheet moved left and sheets between newIndex and oldIndex moved right else if (newIndex <= i && i < oldIndex) { - name.setSheetIndex(i+1); + name.setSheetIndex(i + 1); } // if oldIndex < newIndex then this sheet moved right and sheets between oldIndex and newIndex moved left else if (oldIndex < i && i <= newIndex) { - name.setSheetIndex(i-1); + name.setSheetIndex(i - 1); } } } @@ -545,7 +538,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss private void updateActiveSheetAfterSheetReorder(int oldIndex, int newIndex) { // adjust active sheet if necessary int active = getActiveSheetIndex(); - if(active == oldIndex) { + if (active == oldIndex) { // moved sheet was the active one setActiveSheet(newIndex); } else if ((active < oldIndex && active < newIndex) || @@ -553,22 +546,22 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // not affected } else if (newIndex > oldIndex) { // moved sheet was below before and is above now => active is one less - setActiveSheet(active-1); + setActiveSheet(active - 1); } else { // remaining case: moved sheet was higher than active before and is lower now => active is one more - setActiveSheet(active+1); + setActiveSheet(active + 1); } } private void validateSheetIndex(int index) { int lastSheetIx = _sheets.size() - 1; if (index < 0 || index > lastSheetIx) { - String range = "(0.." + lastSheetIx + ")"; + String range = "(0.." + lastSheetIx + ")"; if (lastSheetIx == -1) { range = "(no sheets)"; } throw new IllegalArgumentException("Sheet index (" - + index +") is out of range " + range); + + index + ") is out of range " + range); } } @@ -581,10 +574,10 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss validateSheetIndex(index); int nSheets = _sheets.size(); - for (int i=0; i<nSheets; i++) { - getSheetAt(i).setSelected(i == index); + for (int i = 0; i < nSheets; i++) { + getSheetAt(i).setSelected(i == index); } - workbook.getWindowOne().setNumSelectedTabs((short)1); + workbook.getWindowOne().setNumSelectedTabs((short) 1); } /** @@ -617,7 +610,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // ignore duplicates Set<Integer> set = new HashSet<>(indexes); int nSheets = _sheets.size(); - for (int i=0; i<nSheets; i++) { + for (int i = 0; i < nSheets; i++) { boolean bSelect = set.contains(i); getSheetAt(i).setSelected(bSelect); } @@ -634,7 +627,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss public Collection<Integer> getSelectedTabs() { Collection<Integer> indexes = new ArrayList<>(); int nSheets = _sheets.size(); - for (int i=0; i<nSheets; i++) { + for (int i = 0; i < nSheets; i++) { HSSFSheet sheet = getSheetAt(i); if (sheet.isSelected()) { indexes.add(i); @@ -653,8 +646,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss validateSheetIndex(index); int nSheets = _sheets.size(); - for (int i=0; i<nSheets; i++) { - getSheetAt(i).setActive(i == index); + for (int i = 0; i < nSheets; i++) { + getSheetAt(i).setActive(i == index); } workbook.getWindowOne().setActiveSheetIndex(index); } @@ -664,6 +657,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * This may be different from the "selected sheet" since excel seems to * allow you to show the data of one sheet when another is seen "selected" * in the tabs (at the bottom). + * * @see org.apache.poi.hssf.usermodel.HSSFSheet#setSelected(boolean) */ @Override @@ -697,7 +691,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * * @param sheetIx number (0 based) * @throws IllegalArgumentException if the name is null or invalid - * or workbook already contains a sheet with this name + * or workbook already contains a sheet with this name * @see #createSheet(String) * @see org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal) */ @@ -761,16 +755,20 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss workbook.setSheetHidden(sheetIx, visibility); } - /** Returns the index of the sheet by his name + /** + * Returns the index of the sheet by his name + * * @param name the sheet name * @return index of the sheet (0 based) */ @Override - public int getSheetIndex(String name){ + public int getSheetIndex(String name) { return workbook.getSheetIndex(name); } - /** Returns the index of the given sheet + /** + * Returns the index of the given sheet + * * @param sheet the sheet to look up * @return index of the sheet (0 based). <tt>-1</tt> if not found */ @@ -787,8 +785,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss */ @Override - public HSSFSheet createSheet() - { + public HSSFSheet createSheet() { HSSFSheet sheet = new HSSFSheet(this); _sheets.add(sheet); @@ -841,7 +838,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss try { uniqueIndex = Integer.parseInt(suffix.trim()); uniqueIndex++; - baseName=srcName.substring(0, bracketPos).trim(); + baseName = srcName.substring(0, bracketPos).trim(); } catch (NumberFormatException e) { // contents of brackets not numeric } @@ -858,7 +855,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss //If the sheet name is unique, then set it otherwise move on to the next number. if (workbook.getSheetIndex(name) == -1) { - return name; + return name; } } } @@ -868,21 +865,21 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * Use this to create new sheets. * * <p> - * Note that Excel allows sheet names up to 31 chars in length but other applications - * (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars, - * others - truncate such names to 31 character. + * Note that Excel allows sheet names up to 31 chars in length but other applications + * (such as OpenOffice) allow more. Some versions of Excel crash with names longer than 31 chars, + * others - truncate such names to 31 character. * </p> * <p> - * POI's SpreadsheetAPI silently truncates the input argument to 31 characters. - * Example: + * POI's SpreadsheetAPI silently truncates the input argument to 31 characters. + * Example: * - * <pre><code> + * <pre><code> * Sheet sheet = workbook.createSheet("My very long sheet name which is longer than 31 chars"); // will be truncated * assert 31 == sheet.getSheetName().length(); * assert "My very long sheet name which i" == sheet.getSheetName(); * </code></pre> * </p> - * + * <p> * Except the 31-character constraint, Excel applies some other rules: * <p> * Sheet name MUST be unique in the workbook and MUST NOT contain the any of the following characters: @@ -900,20 +897,19 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * The string MUST NOT begin or end with the single quote (') character. * </p> * - * @param sheetname sheetname to set for the sheet. + * @param sheetname sheetname to set for the sheet. * @return Sheet representing the new sheet. * @throws IllegalArgumentException if the name is null or invalid - * or workbook already contains a sheet with this name + * or workbook already contains a sheet with this name * @see org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal) */ @Override - public HSSFSheet createSheet(String sheetname) - { + public HSSFSheet createSheet(String sheetname) { if (sheetname == null) { throw new IllegalArgumentException("sheetName must not be null"); } - if (workbook.doesContainsSheetName( sheetname, _sheets.size() )) { + if (workbook.doesContainsSheetName(sheetname, _sheets.size())) { throw new IllegalArgumentException("The workbook already contains a sheet named '" + sheetname + "'"); } @@ -928,8 +924,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } /** - * Returns an iterator of the sheets in the workbook - * in sheet order. Includes hidden and very hidden sheets. + * Returns an iterator of the sheets in the workbook + * in sheet order. Includes hidden and very hidden sheets. * * @return an iterator of the sheets. */ @@ -950,19 +946,23 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss private final class SheetIterator<T extends Sheet> implements Iterator<T> { final private Iterator<T> it; private T cursor; + @SuppressWarnings("unchecked") public SheetIterator() { it = (Iterator<T>) _sheets.iterator(); } + @Override public boolean hasNext() { return it.hasNext(); } + @Override public T next() throws NoSuchElementException { cursor = it.next(); return cursor; } + /** * Unexpected behavior may occur if sheets are reordered after iterator * has been created. Support for the remove method may be added in the future @@ -970,18 +970,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss */ @Override public void remove() throws IllegalStateException { - throw new UnsupportedOperationException("remove method not supported on HSSFWorkbook.iterator(). "+ - "Use Sheet.removeSheetAt(int) instead."); + throw new UnsupportedOperationException("remove method not supported on HSSFWorkbook.iterator(). " + + "Use Sheet.removeSheetAt(int) instead."); } } /** * get the number of spreadsheets in the workbook (this will be three after serialization) + * * @return number of sheets */ @Override - public int getNumberOfSheets() - { + public int getNumberOfSheets() { return _sheets.size(); } @@ -993,35 +993,33 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Get the HSSFSheet object at the given index. + * * @param index of the sheet number (0-based physical & logical) * @return HSSFSheet at the provided index * @throws IllegalArgumentException if the index is out of range (index - * < 0 || index >= getNumberOfSheets()). + * < 0 || index >= getNumberOfSheets()). */ @Override - public HSSFSheet getSheetAt(int index) - { + public HSSFSheet getSheetAt(int index) { validateSheetIndex(index); return _sheets.get(index); } /** * Get sheet with the given name (case insensitive match) + * * @param name of the sheet * @return HSSFSheet with the name provided or <code>null</code> if it does not exist */ @Override - public HSSFSheet getSheet(String name) - { + public HSSFSheet getSheet(String name) { HSSFSheet retval = null; - for (int k = 0; k < _sheets.size(); k++) - { + for (int k = 0; k < _sheets.size(); k++) { String sheetname = workbook.getSheetName(k); - if (sheetname.equalsIgnoreCase(name)) - { + if (sheetname.equalsIgnoreCase(name)) { retval = _sheets.get(k); } } @@ -1030,11 +1028,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Removes sheet at the given index.<p> - * + * <p> * Care must be taken if the removed sheet is the currently active or only selected sheet in * the workbook. There are a few situations when Excel must have a selection and/or active * sheet. (For example when printing - see Bug 40414).<br> - * + * <p> * This method makes sure that if the removed sheet was active, another sheet will become * active in its place. Furthermore, if the removed sheet was the only selected sheet, another * sheet will become selected. The newly active/selected sheet will have the same index, or @@ -1059,12 +1057,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // the index of the closest remaining sheet to the one just deleted int newSheetIndex = index; if (newSheetIndex >= nSheets) { - newSheetIndex = nSheets-1; + newSheetIndex = nSheets - 1; } if (wasSelected) { boolean someOtherSheetIsStillSelected = false; - for (int i =0; i < nSheets; i++) { + for (int i = 0; i < nSheets; i++) { if (getSheetAt(i).isSelected()) { someOtherSheetIsStillSelected = true; break; @@ -1077,23 +1075,22 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // adjust active sheet int active = getActiveSheetIndex(); - if(active == index) { + if (active == index) { // removed sheet was the active one, reset active sheet if there is still one left now setActiveSheet(newSheetIndex); } else if (active > index) { // removed sheet was below the active one => active is one less now - setActiveSheet(active-1); + setActiveSheet(active - 1); } } /** * determine whether the Excel GUI will backup the workbook when saving. * - * @param backupValue true to indicate a backup will be performed. + * @param backupValue true to indicate a backup will be performed. */ - public void setBackupFlag(boolean backupValue) - { + public void setBackupFlag(boolean backupValue) { BackupRecord backupRecord = workbook.getBackupRecord(); backupRecord.setBackup(backupValue ? (short) 1 @@ -1106,15 +1103,14 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * @return the current setting for backups. */ - public boolean getBackupFlag() - { + public boolean getBackupFlag() { BackupRecord backupRecord = workbook.getBackupRecord(); return backupRecord.getBackup() != 0; } int findExistingBuiltinNameRecordIdx(int sheetIndex, byte builtinCode) { - for(int defNameIndex =0; defNameIndex<names.size(); defNameIndex++) { + for (int defNameIndex = 0; defNameIndex < names.size(); defNameIndex++) { NameRecord r = workbook.getNameRecord(defNameIndex); if (r == null) { throw new RuntimeException("Unable to find all defined names to iterate over"); @@ -1122,7 +1118,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss if (!r.isBuiltInName() || r.getBuiltInName() != builtinCode) { continue; } - if (r.getSheetNumber() -1 == sheetIndex) { + if (r.getSheetNumber() - 1 == sheetIndex) { return defNameIndex; } } @@ -1131,36 +1127,36 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss HSSFName createBuiltInName(byte builtinCode, int sheetIndex) { - NameRecord nameRecord = - workbook.createBuiltInName(builtinCode, sheetIndex + 1); - HSSFName newName = new HSSFName(this, nameRecord, null); - names.add(newName); - return newName; + NameRecord nameRecord = + workbook.createBuiltInName(builtinCode, sheetIndex + 1); + HSSFName newName = new HSSFName(this, nameRecord, null); + names.add(newName); + return newName; } HSSFName getBuiltInName(byte builtinCode, int sheetIndex) { - int index = findExistingBuiltinNameRecordIdx(sheetIndex, builtinCode); - return (index < 0) ? null : names.get(index); + int index = findExistingBuiltinNameRecordIdx(sheetIndex, builtinCode); + return (index < 0) ? null : names.get(index); } /** * create a new Font and add it to the workbook's font table + * * @return new font object */ @Override - public HSSFFont createFont() - { - /*FontRecord font =*/ workbook.createNewFont(); + public HSSFFont createFont() { + /*FontRecord font =*/ + workbook.createNewFont(); int fontindex = getNumberOfFontsAsInt() - 1; - if (fontindex > 3) - { + if (fontindex > 3) { fontindex++; // THERE IS NO FOUR!! } - if(fontindex >= Short.MAX_VALUE){ + if (fontindex >= Short.MAX_VALUE) { throw new IllegalArgumentException("Maximum number of fonts was exceeded"); } @@ -1175,12 +1171,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss @Override public HSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, - short typeOffset, byte underline) - { + short typeOffset, byte underline) { int numberOfFonts = getNumberOfFontsAsInt(); for (int i = 0; i <= numberOfFonts; i++) { // Remember - there is no 4! - if(i == 4) { + if (i == 4) { continue; } @@ -1192,8 +1187,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss && hssfFont.getItalic() == italic && hssfFont.getStrikeout() == strikeout && hssfFont.getTypeOffset() == typeOffset - && hssfFont.getUnderline() == underline) - { + && hssfFont.getUnderline() == underline) { return hssfFont; } } @@ -1204,7 +1198,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss @Override @Deprecated public short getNumberOfFonts() { - return (short)getNumberOfFontsAsInt(); + return (short) getNumberOfFontsAsInt(); } @Override @@ -1215,12 +1209,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss @Override @Deprecated public HSSFFont getFontAt(short idx) { - return getFontAt((int)idx); + return getFontAt((int) idx); } @Override public HSSFFont getFontAt(int idx) { - if(fonts == null) { + if (fonts == null) { fonts = new HashMap<>(); } @@ -1228,7 +1222,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // the same object every time, but create // them lazily Integer sIdx = idx; - if(fonts.containsKey(sIdx)) { + if (fonts.containsKey(sIdx)) { return fonts.get(sIdx); } @@ -1241,9 +1235,9 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Reset the fonts cache, causing all new calls - * to getFontAt() to create new objects. + * to getFontAt() to create new objects. * Should only be called after deleting fonts, - * and that's not something you should normally do + * and that's not something you should normally do */ void resetFontCache() { fonts = new HashMap<>(); @@ -1257,9 +1251,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * @throws IllegalStateException if the number of cell styles exceeded the limit for this type of Workbook. */ @Override - public HSSFCellStyle createCellStyle() - { - if(workbook.getNumExFormats() == MAX_STYLES) { + public HSSFCellStyle createCellStyle() { + if (workbook.getNumExFormats() == MAX_STYLES) { throw new IllegalStateException("The maximum number of cell styles was exceeded. " + "You can define up to 4000 styles in a .xls workbook"); } @@ -1270,33 +1263,33 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * get the number of styles the workbook contains + * * @return count of cell styles */ @Override - public int getNumCellStyles() - { + public int getNumCellStyles() { return workbook.getNumExFormats(); } /** * get the cell style object at the given index - * @param idx index within the set of styles + * + * @param idx index within the set of styles * @return HSSFCellStyle object at the index */ @Override - public HSSFCellStyle getCellStyleAt(int idx) - { + public HSSFCellStyle getCellStyleAt(int idx) { ExtendedFormatRecord xfr = workbook.getExFormatAt(idx); - return new HSSFCellStyle((short)idx, xfr, this); + return new HSSFCellStyle((short) idx, xfr, this); } /** * Closes the underlying {@link POIFSFileSystem} from which - * the Workbook was read, if any. + * the Workbook was read, if any. * * <p>Once this has been called, no further - * operations, updates or reads should be performed on the - * Workbook. + * operations, updates or reads should be performed on the + * Workbook. */ @Override public void close() throws IOException { @@ -1305,13 +1298,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Write out this workbook to the currently open {@link File} via the - * writeable {@link POIFSFileSystem} it was opened as. + * writeable {@link POIFSFileSystem} it was opened as. * * <p>This will fail (with an {@link IllegalStateException} if the - * Workbook was opened read-only, opened from an {@link InputStream} - * instead of a File, or if this is not the root document. For those cases, - * you must use {@link #write(OutputStream)} or {@link #write(File)} to - * write to a brand new document. + * Workbook was opened read-only, opened from an {@link InputStream} + * instead of a File, or if this is not the root document. For those cases, + * you must use {@link #write(OutputStream)} or {@link #write(File)} to + * write to a brand new document. */ @Override public void write() throws IOException { @@ -1319,7 +1312,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss final DirectoryNode dir = getDirectory(); // Update the Workbook stream in the file - DocumentNode workbookNode = (DocumentNode)dir.getEntry( + DocumentNode workbookNode = (DocumentNode) dir.getEntry( getWorkbookDirEntryName(dir)); POIFSDocument workbookDoc = new POIFSDocument(workbookNode); workbookDoc.replaceContents(new ByteArrayInputStream(getBytes())); @@ -1336,14 +1329,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * a new POI POIFSFileSystem, passes in the workbook binary representation and * writes it out. If the file exists, it will be replaced, otherwise a new one * will be created. - * + * <p> * Note that you cannot write to the currently open File using this method. * If you opened your Workbook from a File, you <i>must</i> use the {@link #write()} * method instead! * * @param newFile The new File you wish to write the XLS to - * - * @exception IOException if anything can't be written. + * @throws IOException if anything can't be written. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem */ @Override @@ -1358,26 +1350,27 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * Method write - write out this workbook to an {@link OutputStream}. Constructs * a new POI POIFSFileSystem, passes in the workbook binary representation and * writes it out. - * + * <p> * If {@code stream} is a {@link java.io.FileOutputStream} on a networked drive * or has a high cost/latency associated with each written byte, * consider wrapping the OutputStream in a {@link java.io.BufferedOutputStream} * to improve write performance. * * @param stream - the java OutputStream you wish to write the XLS to - * - * @exception IOException if anything can't be written. + * @throws IOException if anything can't be written. * @see org.apache.poi.poifs.filesystem.POIFSFileSystem */ @Override - public void write(OutputStream stream) throws IOException { + public void write(OutputStream stream) throws IOException { try (POIFSFileSystem fs = new POIFSFileSystem()) { write(fs); fs.writeFilesystem(stream); } } - /** Writes the workbook out to a brand new, empty POIFS */ + /** + * Writes the workbook out to a brand new, empty POIFS + */ private void write(POIFSFileSystem fs) throws IOException { // For tracking what we've written out, used if we're // going to be preserving nodes @@ -1398,16 +1391,16 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // summary information has been already written via writeProperties and might go in a // different stream, if the file is cryptoapi encrypted excepts.addAll(Arrays.asList( - DocumentSummaryInformation.DEFAULT_STREAM_NAME, - SummaryInformation.DEFAULT_STREAM_NAME, - getEncryptedPropertyStreamName() + DocumentSummaryInformation.DEFAULT_STREAM_NAME, + SummaryInformation.DEFAULT_STREAM_NAME, + getEncryptedPropertyStreamName() )); // Copy over all the other nodes to our new poifs EntryUtils.copyNodes( new FilteringDirectoryNode(getDirectory(), excepts) , new FilteringDirectoryNode(fs.getRoot(), excepts) - ); + ); // YK: preserve StorageClsid, it is important for embedded workbooks, // see Bugzilla 47920 @@ -1427,15 +1420,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss _totalSize = 0; _list = new ArrayList<>(128); } + public int getTotalSize() { return _totalSize; } + @Override public void visitRecord(org.apache.poi.hssf.record.Record r) { _list.add(r); - _totalSize+=r.getRecordSize(); + _totalSize += r.getRecordSize(); } + public int serialize(int offset, byte[] data) { int result = 0; for (org.apache.poi.hssf.record.Record rec : _list) { @@ -1450,12 +1446,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * Method getBytes - get the bytes of just the HSSF portions of the XLS file. * Use this to construct a POI POIFSFileSystem yourself. * - * * @return byte[] array containing the binary representation of this workbook and all contained - * sheets, rows, cells, etc. + * sheets, rows, cells, etc. */ public byte[] getBytes() { - if (log.check( POILogger.DEBUG )) { + if (log.check(POILogger.DEBUG)) { log.log(DEBUG, "HSSFWorkbook.getBytes()"); } @@ -1535,7 +1530,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss byte[] bsrBuf = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH); plain.readFully(bsrBuf); os.writePlain(bsrBuf, 0, 4); - os.write(bsrBuf, 4, len-4); + os.write(bsrBuf, 4, len - 4); } else { int todo = len; while (todo > 0) { @@ -1562,7 +1557,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } @Override - public int getNumberOfNames(){ + public int getNumberOfNames() { return names.size(); } @@ -1578,8 +1573,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss @Override public List<HSSFName> getNames(String name) { List<HSSFName> nameList = new ArrayList<>(); - for(HSSFName nr : names) { - if(nr.getNameName().equals(name)) { + for (HSSFName nr : names) { + if (nr.getNameName().equals(name)) { nameList.add(nr); } } @@ -1587,15 +1582,14 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return Collections.unmodifiableList(nameList); } - @Override - public HSSFName getNameAt(int nameIndex) { + HSSFName getNameAt(int nameIndex) { int nNames = names.size(); if (nNames < 1) { throw new IllegalStateException("There are no defined names in this workbook"); } if (nameIndex < 0 || nameIndex > nNames) { throw new IllegalArgumentException("Specified name index " + nameIndex - + " is outside the allowable range (0.." + (nNames-1) + ")."); + + " is outside the allowable range (0.." + (nNames - 1) + ")."); } return names.get(nameIndex); } @@ -1609,11 +1603,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return getWorkbook().getNameRecord(nameIndex); } - /** gets the named range name + /** + * gets the named range name + * * @param index the named range index (0 based) * @return named range name */ - public String getNameName(int index){ + public String getNameName(int index) { return getNameAt(index).getNameName(); } @@ -1621,23 +1617,23 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * Sets the printarea for the sheet provided * <p> * i.e. Reference = $A$1:$B$2 + * * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java) - * @param reference Valid name Reference for the Print Area + * @param reference Valid name Reference for the Print Area */ @Override - public void setPrintArea(int sheetIndex, String reference) - { - NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); + public void setPrintArea(int sheetIndex, String reference) { + NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1); if (name == null) { - name = workbook.createBuiltInName(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); + name = workbook.createBuiltInName(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1); // adding one here because 0 indicates a global named region; doesn't make sense for print areas } String[] parts = COMMA_PATTERN.split(reference); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < parts.length; i++) { - if(i>0) { + if (i > 0) { sb.append(","); } SheetNameFormatter.appendFormat(sb, getSheetName(sheetIndex)); @@ -1649,23 +1645,24 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * For the Convenience of Java Programmers maintaining pointers. - * @see #setPrintArea(int, String) - * @param sheetIndex Zero-based sheet index (0 = First Sheet) + * + * @param sheetIndex Zero-based sheet index (0 = First Sheet) * @param startColumn Column to begin printarea - * @param endColumn Column to end the printarea - * @param startRow Row to begin the printarea - * @param endRow Row to end the printarea + * @param endColumn Column to end the printarea + * @param startRow Row to begin the printarea + * @param endRow Row to end the printarea + * @see #setPrintArea(int, String) */ @Override public void setPrintArea(int sheetIndex, int startColumn, int endColumn, - int startRow, int endRow) { + int startRow, int endRow) { //using absolute references because they don't get copied and pasted anyway CellReference cell = new CellReference(startRow, startColumn, true, true); String reference = cell.formatAsString(); cell = new CellReference(endRow, endColumn, true, true); - reference = reference+":"+cell.formatAsString(); + reference = reference + ":" + cell.formatAsString(); setPrintArea(sheetIndex, reference); } @@ -1673,12 +1670,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Retrieves the reference for the printarea of the specified sheet, the sheet name is appended to the reference even if it was not specified. + * * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java) * @return String Null if no print area has been defined */ @Override public String getPrintArea(int sheetIndex) { - NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); + NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1); //adding one here because 0 indicates a global named region; doesn't make sense for print areas if (name == null) { return null; @@ -1689,18 +1687,21 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Delete the printarea for the sheet specified + * * @param sheetIndex Zero-based sheet index (0 = First Sheet) */ @Override public void removePrintArea(int sheetIndex) { - getWorkbook().removeBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); + getWorkbook().removeBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex + 1); } - /** creates a new named range and add it to the model + /** + * creates a new named range and add it to the model + * * @return named range high level */ @Override - public HSSFName createName(){ + public HSSFName createName() { NameRecord nameRecord = workbook.createName(); HSSFName newName = new HSSFName(this, nameRecord); @@ -1710,8 +1711,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return newName; } - @Override - public int getNameIndex(String name) { + int getNameIndex(String name) { for (int k = 0; k < names.size(); k++) { String nameName = getNameName(k); @@ -1729,48 +1729,39 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * (name + sheet index is unique), this method is more accurate. * * @param name the name whose index in the list of names of this workbook - * should be looked up. + * should be looked up. * @return an index value >= 0 if the name was found; -1, if the name was - * not found + * not found */ int getNameIndex(HSSFName name) { - for (int k = 0; k < names.size(); k++) { - if (name == names.get(k)) { - return k; + for (int k = 0; k < names.size(); k++) { + if (name == names.get(k)) { + return k; + } } - } - return -1; + return -1; } - - @Override - public void removeName(int index){ + void removeName(int index) { names.remove(index); workbook.removeName(index); } /** * Returns the instance of HSSFDataFormat for this workbook. + * * @return the HSSFDataFormat object * @see org.apache.poi.hssf.record.FormatRecord * @see org.apache.poi.hssf.record.Record */ @Override public HSSFDataFormat createDataFormat() { - if (formatter == null) { - formatter = new HSSFDataFormat(workbook); - } - return formatter; - } - - - @Override - public void removeName(String name) { - int index = getNameIndex(name); - removeName(index); + if (formatter == null) { + formatter = new HSSFDataFormat(workbook); + } + return formatter; } - /** * As {@link #removeName(String)} is not necessarily unique * (name + sheet index is unique), this method is more accurate. @@ -1779,40 +1770,40 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss */ @Override public void removeName(Name name) { - int index = getNameIndex((HSSFName) name); - removeName(index); + int index = getNameIndex((HSSFName) name); + removeName(index); } - public HSSFPalette getCustomPalette() - { + public HSSFPalette getCustomPalette() { return new HSSFPalette(workbook.getCustomPalette()); } - /** Test only. Do not use */ - public void insertChartRecord() - { + /** + * Test only. Do not use + */ + public void insertChartRecord() { int loc = workbook.findFirstRecordLocBySid(SSTRecord.sid); byte[] data = { - (byte)0x0F, (byte)0x00, (byte)0x00, (byte)0xF0, (byte)0x52, - (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, - (byte)0x06, (byte)0xF0, (byte)0x18, (byte)0x00, (byte)0x00, - (byte)0x00, (byte)0x01, (byte)0x08, (byte)0x00, (byte)0x00, - (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, - (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, - (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, - (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, - (byte)0x33, (byte)0x00, (byte)0x0B, (byte)0xF0, (byte)0x12, - (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xBF, (byte)0x00, - (byte)0x08, (byte)0x00, (byte)0x08, (byte)0x00, (byte)0x81, - (byte)0x01, (byte)0x09, (byte)0x00, (byte)0x00, (byte)0x08, - (byte)0xC0, (byte)0x01, (byte)0x40, (byte)0x00, (byte)0x00, - (byte)0x08, (byte)0x40, (byte)0x00, (byte)0x1E, (byte)0xF1, - (byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x0D, - (byte)0x00, (byte)0x00, (byte)0x08, (byte)0x0C, (byte)0x00, - (byte)0x00, (byte)0x08, (byte)0x17, (byte)0x00, (byte)0x00, - (byte)0x08, (byte)0xF7, (byte)0x00, (byte)0x00, (byte)0x10, + (byte) 0x0F, (byte) 0x00, (byte) 0x00, (byte) 0xF0, (byte) 0x52, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x06, (byte) 0xF0, (byte) 0x18, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x01, (byte) 0x08, (byte) 0x00, (byte) 0x00, + (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x33, (byte) 0x00, (byte) 0x0B, (byte) 0xF0, (byte) 0x12, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xBF, (byte) 0x00, + (byte) 0x08, (byte) 0x00, (byte) 0x08, (byte) 0x00, (byte) 0x81, + (byte) 0x01, (byte) 0x09, (byte) 0x00, (byte) 0x00, (byte) 0x08, + (byte) 0xC0, (byte) 0x01, (byte) 0x40, (byte) 0x00, (byte) 0x00, + (byte) 0x08, (byte) 0x40, (byte) 0x00, (byte) 0x1E, (byte) 0xF1, + (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0D, + (byte) 0x00, (byte) 0x00, (byte) 0x08, (byte) 0x0C, (byte) 0x00, + (byte) 0x00, (byte) 0x08, (byte) 0x17, (byte) 0x00, (byte) 0x00, + (byte) 0x08, (byte) 0xF7, (byte) 0x00, (byte) 0x00, (byte) 0x10, }; - UnknownRecord r = new UnknownRecord((short)0x00EB, data); + UnknownRecord r = new UnknownRecord((short) 0x00EB, data); workbook.getRecords().add(loc, r); } @@ -1820,7 +1811,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * Spits out a list of all the drawing records in the workbook. */ public void dumpDrawingGroupRecords(boolean fat) { - DrawingGroupRecord r = (DrawingGroupRecord) workbook.findFirstRecordBySid( DrawingGroupRecord.sid ); + DrawingGroupRecord r = (DrawingGroupRecord) workbook.findFirstRecordBySid(DrawingGroupRecord.sid); if (r == null) { return; } @@ -1837,10 +1828,10 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss w.flush(); } - void initDrawings(){ + void initDrawings() { DrawingManager2 mgr = workbook.findDrawingGroup(); - if(mgr != null) { - for(HSSFSheet sh : _sheets) { + if (mgr != null) { + for (HSSFSheet sh : _sheets) { sh.getDrawingPatriarch(); } } else { @@ -1851,9 +1842,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Adds a picture to the workbook. * - * @param pictureData The bytes of the picture - * @param format The format of the picture. One of <code>PICTURE_TYPE_*</code> - * + * @param pictureData The bytes of the picture + * @param format The format of the picture. One of <code>PICTURE_TYPE_*</code> * @return the index to this picture (1 based). * @see #PICTURE_TYPE_WMF * @see #PICTURE_TYPE_EMF @@ -1864,8 +1854,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss */ @SuppressWarnings("fallthrough") @Override - public int addPicture(byte[] pictureData, int format) - { + public int addPicture(byte[] pictureData, int format) { initDrawings(); byte[] uid = DigestUtils.md5(pictureData); @@ -1885,24 +1874,23 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss blipRecordMeta.setUID(uid); blipRecordMeta.setPictureData(pictureData); // taken from libre office export, it won't open, if this is left to 0 - blipRecordMeta.setFilter((byte)-2); + blipRecordMeta.setFilter((byte) -2); blipSize = blipRecordMeta.getCompressedSize() + 58; escherTag = 0; break; default: EscherBitmapBlip blipRecordBitmap = new EscherBitmapBlip(); blipRecord = blipRecordBitmap; - blipRecordBitmap.setUID( uid ); - blipRecordBitmap.setMarker( (byte) 0xFF ); - blipRecordBitmap.setPictureData( pictureData ); + blipRecordBitmap.setUID(uid); + blipRecordBitmap.setMarker((byte) 0xFF); + blipRecordBitmap.setPictureData(pictureData); blipSize = pictureData.length + 25; escherTag = (short) 0xFF; - break; + break; } blipRecord.setRecordId((short) (EscherBlipRecord.RECORD_ID_START + format)); - switch (format) - { + switch (format) { case PICTURE_TYPE_EMF: blipRecord.setOptions(HSSFPictureData.MSOBI_EMF); break; @@ -1926,18 +1914,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } EscherBSERecord r = new EscherBSERecord(); - r.setRecordId( EscherBSERecord.RECORD_ID ); - r.setOptions( (short) ( 0x0002 | ( format << 4 ) ) ); - r.setBlipTypeMacOS( (byte) format ); - r.setBlipTypeWin32( (byte) format ); - r.setUid( uid ); - r.setTag( escherTag ); - r.setSize( blipSize ); - r.setRef( 0 ); - r.setOffset( 0 ); - r.setBlipRecord( blipRecord ); + r.setRecordId(EscherBSERecord.RECORD_ID); + r.setOptions((short) (0x0002 | (format << 4))); + r.setBlipTypeMacOS((byte) format); + r.setBlipTypeWin32((byte) format); + r.setUid(uid); + r.setTag(escherTag); + r.setSize(blipSize); + r.setRef(0); + r.setOffset(0); + r.setBlipRecord(blipRecord); - return workbook.addBSERecord( r ); + return workbook.addBSERecord(r); } /** @@ -1946,8 +1934,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * @return the list of pictures (a list of {@link HSSFPictureData} objects.) */ @Override - public List<HSSFPictureData> getAllPictures() - { + public List<HSSFPictureData> getAllPictures() { // The drawing group record always exists at the top level, so we won't need to do this recursively. List<HSSFPictureData> pictures = new ArrayList<>(); for (org.apache.poi.hssf.record.Record r : workbook.getRecords()) { @@ -1964,20 +1951,17 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * Performs a recursive search for pictures in the given list of escher records. * * @param escherRecords the escher records. - * @param pictures the list to populate with the pictures. + * @param pictures the list to populate with the pictures. */ - private void searchForPictures(List<EscherRecord> escherRecords, List<HSSFPictureData> pictures) - { - for(EscherRecord escherRecord : escherRecords) { + private void searchForPictures(List<EscherRecord> escherRecords, List<HSSFPictureData> pictures) { + for (EscherRecord escherRecord : escherRecords) { - if (escherRecord instanceof EscherBSERecord) - { + if (escherRecord instanceof EscherBSERecord) { EscherBlipRecord blip = ((EscherBSERecord) escherRecord).getBlipRecord(); - if (blip != null) - { + if (blip != null) { // TODO: Some kind of structure. HSSFPictureData picture = new HSSFPictureData(blip); - pictures.add(picture); + pictures.add(picture); } @@ -1989,55 +1973,55 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } - static Map<String,ClassID> getOleMap() { - Map<String,ClassID> olemap = new HashMap<>(); - olemap.put("PowerPoint Document", ClassIDPredefined.POWERPOINT_V8.getClassID()); - for (String str : WORKBOOK_DIR_ENTRY_NAMES) { - olemap.put(str, ClassIDPredefined.EXCEL_V7_WORKBOOK.getClassID()); - } - // ... to be continued - return olemap; + static Map<String, ClassID> getOleMap() { + Map<String, ClassID> olemap = new HashMap<>(); + olemap.put("PowerPoint Document", ClassIDPredefined.POWERPOINT_V8.getClassID()); + for (String str : WORKBOOK_DIR_ENTRY_NAMES) { + olemap.put(str, ClassIDPredefined.EXCEL_V7_WORKBOOK.getClassID()); + } + // ... to be continued + return olemap; } /** * Adds an OLE package manager object with the given POIFS to the sheet * - * @param poiData an POIFS containing the embedded document, to be added - * @param label the label of the payload + * @param poiData an POIFS containing the embedded document, to be added + * @param label the label of the payload * @param fileName the original filename - * @param command the command to open the payload + * @param command the command to open the payload * @return the index of the added ole object * @throws IOException if the object can't be embedded */ public int addOlePackage(POIFSFileSystem poiData, String label, String fileName, String command) - throws IOException { - DirectoryNode root = poiData.getRoot(); - Map<String,ClassID> olemap = getOleMap(); - for (Map.Entry<String,ClassID> entry : olemap.entrySet()) { - if (root.hasEntry(entry.getKey())) { - root.setStorageClsid(entry.getValue()); - break; - } - } - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - poiData.writeFilesystem(bos); + throws IOException { + DirectoryNode root = poiData.getRoot(); + Map<String, ClassID> olemap = getOleMap(); + for (Map.Entry<String, ClassID> entry : olemap.entrySet()) { + if (root.hasEntry(entry.getKey())) { + root.setStorageClsid(entry.getValue()); + break; + } + } + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + poiData.writeFilesystem(bos); return addOlePackage(bos.toByteArray(), label, fileName, command); } @Override public int addOlePackage(byte[] oleData, String label, String fileName, String command) - throws IOException { - // check if we were created by POIFS otherwise create a new dummy POIFS for storing the package data - if (initDirectory()) { - preserveNodes = true; - } + throws IOException { + // check if we were created by POIFS otherwise create a new dummy POIFS for storing the package data + if (initDirectory()) { + preserveNodes = true; + } // get free MBD-Node int storageId = 0; DirectoryEntry oleDir = null; do { - String storageStr = "MBD"+ HexDump.toHex(++storageId); + String storageStr = "MBD" + HexDump.toHex(++storageId); if (!getDirectory().hasEntry(storageStr)) { oleDir = getDirectory().createDirectory(storageStr); oleDir.setStorageClsid(ClassIDPredefined.OLE_V1_PACKAGE.getClassID()); @@ -2051,16 +2035,16 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss oleNative.writeOut(bos); oleDir.createDocument(Ole10Native.OLE10_NATIVE, new ByteArrayInputStream(bos.toByteArray())); - return storageId; + return storageId; } /** * Adds the LinkTable records required to allow formulas referencing - * the specified external workbook to be added to this one. Allows - * formulas such as "[MyOtherWorkbook]Sheet3!$A$5" to be added to the - * file, for workbooks not already referenced. + * the specified external workbook to be added to this one. Allows + * formulas such as "[MyOtherWorkbook]Sheet3!$A$5" to be added to the + * file, for workbooks not already referenced. * - * @param name The name the workbook will be referenced as in formulas + * @param name The name the workbook will be referenced as in formulas * @param workbook The open workbook to fetch the link required information from */ @Override @@ -2078,17 +2062,18 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * protect a workbook with a password (not encypted, just sets writeprotect * flags and the password. + * * @param password to set */ - public void writeProtectWorkbook( String password, String username ) { - this.workbook.writeProtectWorkbook(password, username); + public void writeProtectWorkbook(String password, String username) { + this.workbook.writeProtectWorkbook(password, username); } /** * removes the write protect flag */ public void unwriteProtectWorkbook() { - this.workbook.unwriteProtectWorkbook(); + this.workbook.unwriteProtectWorkbook(); } /** @@ -2096,11 +2081,9 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * * @return the list of embedded objects (a list of {@link HSSFObjectData} objects.) */ - public List<HSSFObjectData> getAllEmbeddedObjects() - { + public List<HSSFObjectData> getAllEmbeddedObjects() { List<HSSFObjectData> objects = new ArrayList<>(); - for (HSSFSheet sheet : _sheets) - { + for (HSSFSheet sheet : _sheets) { getAllEmbeddedObjects(sheet, objects); } return Collections.unmodifiableList(objects); @@ -2109,25 +2092,24 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss /** * Gets all embedded OLE2 objects from the Workbook. * - * @param sheet embedded object attached to + * @param sheet embedded object attached to * @param objects the list of embedded objects to populate. */ - private void getAllEmbeddedObjects(HSSFSheet sheet, List<HSSFObjectData> objects) - { + private void getAllEmbeddedObjects(HSSFSheet sheet, List<HSSFObjectData> objects) { HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); - if (null == patriarch){ + if (null == patriarch) { return; } getAllEmbeddedObjects(patriarch, objects); } + /** * Recursively iterates a shape container to get all embedded objects. * - * @param parent the parent. + * @param parent the parent. * @param objects the list of embedded objects to populate. */ - private void getAllEmbeddedObjects(HSSFShapeContainer parent, List<HSSFObjectData> objects) - { + private void getAllEmbeddedObjects(HSSFShapeContainer parent, List<HSSFObjectData> objects) { for (HSSFShape shape : parent.getChildren()) { if (shape instanceof HSSFObjectData) { objects.add((HSSFObjectData) shape); @@ -2136,19 +2118,19 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } } } + @Override public HSSFCreationHelper getCreationHelper() { return new HSSFCreationHelper(this); } /** - * * Returns the locator of user-defined functions. * The default instance extends the built-in functions with the Analysis Tool Pack * * @return the locator of user-defined functions */ - /*package*/ UDFFinder getUDFFinder(){ + /*package*/ UDFFinder getUDFFinder() { return _udfFinder; } @@ -2158,8 +2140,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * @param toopack the toolpack to register */ @Override - public void addToolPack(UDFFinder toopack){ - AggregatingUDFFinder udfs = (AggregatingUDFFinder)_udfFinder; + public void addToolPack(UDFFinder toopack) { + AggregatingUDFFinder udfs = (AggregatingUDFFinder) _udfFinder; udfs.add(toopack); } @@ -2177,11 +2159,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * </p> * * @param value true if the application will perform a full recalculation of - * workbook values when the workbook is opened + * workbook values when the workbook is opened * @since 3.8 */ @Override - public void setForceFormulaRecalculation(boolean value){ + public void setForceFormulaRecalculation(boolean value) { InternalWorkbook iwb = getWorkbook(); RecalcIdRecord recalc = iwb.getRecalcId(); recalc.setEngineId(0); @@ -2193,23 +2175,24 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * @since 3.8 */ @Override - public boolean getForceFormulaRecalculation(){ + public boolean getForceFormulaRecalculation() { InternalWorkbook iwb = getWorkbook(); - RecalcIdRecord recalc = (RecalcIdRecord)iwb.findFirstRecordBySid(RecalcIdRecord.sid); + RecalcIdRecord recalc = (RecalcIdRecord) iwb.findFirstRecordBySid(RecalcIdRecord.sid); return recalc != null && recalc.getEngineId() != 0; } - /** - * Changes an external referenced file to another file. - * A formula in Excel which references a cell in another file is saved in two parts: - * The referenced file is stored in an reference table. the row/cell information is saved separate. - * This method invocation will only change the reference in the lookup-table itself. - * @param oldUrl The old URL to search for and which is to be replaced - * @param newUrl The URL replacement - * @return true if the oldUrl was found and replaced with newUrl. Otherwise false - */ + /** + * Changes an external referenced file to another file. + * A formula in Excel which references a cell in another file is saved in two parts: + * The referenced file is stored in an reference table. the row/cell information is saved separate. + * This method invocation will only change the reference in the lookup-table itself. + * + * @param oldUrl The old URL to search for and which is to be replaced + * @param newUrl The URL replacement + * @return true if the oldUrl was found and replaced with newUrl. Otherwise false + */ public boolean changeExternalReference(String oldUrl, String newUrl) { - return workbook.changeExternalReference(oldUrl, newUrl); + return workbook.changeExternalReference(oldUrl, newUrl); } @Internal @@ -2230,7 +2213,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss @Override public EncryptionInfo getEncryptionInfo() { - FilePassRecord fpr = (FilePassRecord)workbook.findFirstRecordBySid(FilePassRecord.sid); + FilePassRecord fpr = (FilePassRecord) workbook.findFirstRecordBySid(FilePassRecord.sid); return (fpr != null) ? fpr.getEncryptionInfo() : null; } @@ -2238,7 +2221,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss private void updateEncryptionInfo() { // make sure, that we've read all the streams ... readProperties(); - FilePassRecord fpr = (FilePassRecord)workbook.findFirstRecordBySid(FilePassRecord.sid); + FilePassRecord fpr = (FilePassRecord) workbook.findFirstRecordBySid(FilePassRecord.sid); String password = Biff8EncryptionKey.getCurrentUserPassword(); WorkbookRecordList wrl = workbook.getWorkbookRecordList(); diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index 3b6a3ea11f..31c6896471 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -366,16 +366,6 @@ public interface Workbook extends Closeable, Iterable<Sheet> { List<? extends Name> getAllNames(); /** - * @param nameIndex position of the named range (0-based) - * @return the defined name at the specified index - * @throws IllegalArgumentException if the supplied index is invalid - * @deprecated 4.0.0. New projects should avoid accessing named ranges by index. - */ - @Deprecated - @Removal(version="5.0.0") - Name getNameAt(int nameIndex); - - /** * Creates a new (uninitialised) defined name in this workbook * * @return new defined name object @@ -383,41 +373,6 @@ public interface Workbook extends Closeable, Iterable<Sheet> { Name createName(); /** - * Gets the defined name index by name<br> - * <i>Note:</i> Excel defined names are case-insensitive and - * this method performs a case-insensitive search. - * - * @param name the name of the defined name - * @return zero based index of the defined name. <tt>-1</tt> if not found. - * @deprecated 3.18. New projects should avoid accessing named ranges by index. - * Use {@link #getName(String)} instead. - */ - @Deprecated - @Removal(version="3.20") - int getNameIndex(String name); - - /** - * Remove the defined name at the specified index - * - * @param index named range index (0 based) - * - * @deprecated 3.18. New projects should use {@link #removeName(Name)}. - */ - @Deprecated - @Removal(version="3.20") - void removeName(int index); - - /** - * Remove a defined name by name - * - * @param name the name of the defined name - * @deprecated 3.18. New projects should use {@link #removeName(Name)}. - */ - @Deprecated - @Removal(version="3.20") - void removeName(String name); - - /** * Remove a defined name * * @param name the name of the defined name |