workbook.java
来自「EXCEL read and write」· Java 代码 · 共 1,759 行 · 第 1/5 页
JAVA
1,759 行
if (log.check( POILogger.DEBUG )) log.log( DEBUG, "exit create new workbook from scratch" ); return retval; } /**Retrieves the Builtin NameRecord that matches the name and index * There shouldn't be too many names to make the sequential search too slow * @param name byte representation of the builtin name to match * @param sheetNumber 1-based sheet number * @return null if no builtin NameRecord matches */ public NameRecord getSpecificBuiltinRecord(byte name, int sheetNumber) { return getOrCreateLinkTable().getSpecificBuiltinRecord(name, sheetNumber); } /** * Removes the specified Builtin NameRecord that matches the name and index * @param name byte representation of the builtin to match * @param sheetIndex zero-based sheet reference */ public void removeBuiltinRecord(byte name, int sheetIndex) { linkTable.removeBuiltinRecord(name, sheetIndex); // TODO - do we need "this.records.remove(...);" similar to that in this.removeName(int namenum) {}? } public int getNumRecords() { return records.size(); } /** * gets the font record at the given index in the font table. Remember * "There is No Four" (someone at M$ must have gone to Rocky Horror one too * many times) * * @param idx the index to look at (0 or greater but NOT 4) * @return FontRecord located at the given index */ public FontRecord getFontRecordAt(int idx) { int index = idx; if (index > 4) { index -= 1; // adjust for "There is no 4" } if (index > (numfonts - 1)) { throw new ArrayIndexOutOfBoundsException( "There are only " + numfonts + " font records, you asked for " + idx); } FontRecord retval = ( FontRecord ) records.get((records.getFontpos() - (numfonts - 1)) + index); return retval; } /** * Retrieves the index of the given font */ public int getFontIndex(FontRecord font) { for(int i=0; i<=numfonts; i++) { FontRecord thisFont = ( FontRecord ) records.get((records.getFontpos() - (numfonts - 1)) + i); if(thisFont == font) { // There is no 4! if(i > 3) { return (i+1); } return i; } } throw new IllegalArgumentException("Could not find that font!"); } /** * creates a new font record and adds it to the "font table". This causes the * boundsheets to move down one, extended formats to move down (so this function moves * those pointers as well) * * @return FontRecord that was just created */ public FontRecord createNewFont() { FontRecord rec = ( FontRecord ) createFont(); records.add(records.getFontpos()+1, rec); records.setFontpos( records.getFontpos() + 1 ); numfonts++; return rec; } /** * Removes the given font record from the * file's list. This will make all * subsequent font indicies drop by one, * so you'll need to update those yourself! */ public void removeFontRecord(FontRecord rec) { records.remove(rec); // this updates FontPos for us numfonts--; } /** * gets the number of font records * * @return number of font records in the "font table" */ public int getNumberOfFontRecords() { return numfonts; } /** * Sets the BOF for a given sheet * * @param sheetIndex the number of the sheet to set the positing of the bof for * @param pos the actual bof position */ public void setSheetBof(int sheetIndex, int pos) { if (log.check( POILogger.DEBUG )) log.log(DEBUG, "setting bof for sheetnum =", new Integer(sheetIndex), " at pos=", new Integer(pos)); checkSheets(sheetIndex); getBoundSheetRec(sheetIndex) .setPositionOfBof(pos); } private BoundSheetRecord getBoundSheetRec(int sheetIndex) { return ((BoundSheetRecord) boundsheets.get(sheetIndex)); } /** * Returns the position of the backup record. */ public BackupRecord getBackupRecord() { return ( BackupRecord ) records.get(records.getBackuppos()); } /** * sets the name for a given sheet. If the boundsheet record doesn't exist and * its only one more than we have, go ahead and create it. If it's > 1 more than * we have, except * * @param sheetnum the sheet number (0 based) * @param sheetname the name for the sheet */ public void setSheetName(int sheetnum, String sheetname) { checkSheets(sheetnum); BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum ); sheet.setSheetname(sheetname); } /** * Determines whether a workbook contains the provided sheet name. For the purpose of * comparison, long names are truncated to 31 chars. * * @param name the name to test (case insensitive match) * @param excludeSheetIdx the sheet to exclude from the check or -1 to include all sheets in the check. * @return true if the sheet contains the name, false otherwise. */ public boolean doesContainsSheetName(String name, int excludeSheetIdx) { String aName = name; if (aName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) { aName = aName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN); } for (int i = 0; i < boundsheets.size(); i++) { BoundSheetRecord boundSheetRecord = getBoundSheetRec(i); if (excludeSheetIdx == i) { continue; } String bName = boundSheetRecord.getSheetname(); if (bName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) { bName = bName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN); } if (aName.equalsIgnoreCase(bName)) { return true; } } return false; } /** * 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) */ public void setSheetOrder(String sheetname, int pos ) { int sheetNumber = getSheetIndex(sheetname); //remove the sheet that needs to be reordered and place it in the spot we want boundsheets.add(pos, boundsheets.remove(sheetNumber)); } /** * gets the name for a given sheet. * * @param sheetIndex the sheet number (0 based) * @return sheetname the name for the sheet */ public String getSheetName(int sheetIndex) { return getBoundSheetRec(sheetIndex).getSheetname(); } /** * Gets the hidden flag for a given sheet. * Note that a sheet could instead be * set to be very hidden, which is different * ({@link #isSheetVeryHidden(int)}) * * @param sheetnum the sheet number (0 based) * @return True if sheet is hidden */ public boolean isSheetHidden(int sheetnum) { return getBoundSheetRec(sheetnum).isHidden(); } /** * Gets the very hidden flag for a given sheet. * This is different from the normal * hidden flag * ({@link #isSheetHidden(int)}) * * @param sheetnum the sheet number (0 based) * @return True if sheet is very hidden */ public boolean isSheetVeryHidden(int sheetnum) { return getBoundSheetRec(sheetnum).isVeryHidden(); } /** * Hide or unhide a sheet * * @param sheetnum The sheet number * @param hidden True to mark the sheet as hidden, false otherwise */ public void setSheetHidden(int sheetnum, boolean hidden) { getBoundSheetRec(sheetnum).setHidden(hidden); } /** * Hide or unhide a sheet. * 0 = not hidden * 1 = hidden * 2 = very hidden. * * @param sheetnum The sheet number * @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden */ public void setSheetHidden(int sheetnum, int hidden) { BoundSheetRecord bsr = getBoundSheetRec(sheetnum); boolean h = false; boolean vh = false; if(hidden == 0) { } else if(hidden == 1) { h = true; } else if(hidden == 2) { vh = true; } else { throw new IllegalArgumentException("Invalid hidden flag " + hidden + " given, must be 0, 1 or 2"); } bsr.setHidden(h); bsr.setVeryHidden(vh); } /** * get the sheet's index * @param name sheet name * @return sheet index or -1 if it was not found. */ public int getSheetIndex(String name) { int retval = -1; for (int k = 0; k < boundsheets.size(); k++) { String sheet = getSheetName(k); if (sheet.equalsIgnoreCase(name)) { retval = k; break; } } return retval; } /** * if we're trying to address one more sheet than we have, go ahead and add it! if we're * trying to address >1 more than we have throw an exception! */ private void checkSheets(int sheetnum) { if ((boundsheets.size()) <= sheetnum) { // if we're short one add another.. if ((boundsheets.size() + 1) <= sheetnum) { throw new RuntimeException("Sheet number out of bounds!"); } BoundSheetRecord bsr = createBoundSheet(sheetnum); records.add(records.getBspos()+1, bsr); records.setBspos( records.getBspos() + 1 ); boundsheets.add(bsr); getOrCreateLinkTable().checkExternSheet(sheetnum); fixTabIdRecord(); } } /** * @param sheetIndex zero based sheet index */ public void removeSheet(int sheetIndex) { if (boundsheets.size() > sheetIndex) { records.remove(records.getBspos() - (boundsheets.size() - 1) + sheetIndex); boundsheets.remove(sheetIndex); fixTabIdRecord(); } // Within NameRecords, it's ok to have the formula // part point at deleted sheets. It's also ok to // have the ExternSheetNumber point at deleted // sheets. // However, the sheet index must be adjusted, or // excel will break. (Sheet index is either 0 for // global, or 1 based index to sheet) int sheetNum1Based = sheetIndex + 1; for(int i=0; i<getNumNames(); i++) { NameRecord nr = getNameRecord(i); if(nr.getSheetNumber() == sheetNum1Based) { // Excel re-writes these to point to no sheet nr.setSheetNumber(0); } else if(nr.getSheetNumber() > sheetNum1Based) { // Bump down by one, so still points // at the same sheet nr.setSheetNumber(nr.getSheetNumber()-1); } } } /** * make the tabid record look like the current situation. * */ private void fixTabIdRecord() { TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos()); short[] tia = new short[ boundsheets.size() ]; for (short k = 0; k < tia.length; k++) { tia[ k ] = k; } tir.setTabIdArray(tia);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?