📄 workbook.java
字号:
* 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) { //the name array is smaller so searching through it should be faster than //using the findFirstXXXX methods NameRecord record = getSpecificBuiltinRecord(name, sheetIndex); if (record != null) { names.remove(record); } } 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; } /** * 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; } /** * 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 sheetnum the number of the sheet to set the positing of the bof for * @param pos the actual bof position */ public void setSheetBof(int sheetnum, int pos) { log.log(DEBUG, "setting bof for sheetnum =", new Integer(sheetnum), " at pos=", new Integer(pos)); checkSheets(sheetnum); (( BoundSheetRecord ) boundsheets.get(sheetnum)) .setPositionOfBof(pos); } /** * 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 its > 1 more than * we have, except * * @param sheetnum the sheet number (0 based) * @param sheetname the name for the sheet */ // for compatibility public void setSheetName(int sheetnum, String sheetname ) { setSheetName( sheetnum, sheetname, (byte)0 ); } public void setSheetName(int sheetnum, String sheetname, short encoding ) { checkSheets(sheetnum); BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum ); sheet.setSheetname(sheetname); sheet.setSheetnameLength( (byte)sheetname.length() ); sheet.setCompressedUnicodeFlag( (byte)encoding ); } /** * 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 sheetnum the sheet number (0 based) * @return sheetname the name for the sheet */ public String getSheetName(int sheetnum) { return (( BoundSheetRecord ) boundsheets.get(sheetnum)) .getSheetname(); } /** * 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 = (BoundSheetRecord ) createBoundSheet(sheetnum); records.add(records.getBspos()+1, bsr); records.setBspos( records.getBspos() + 1 ); boundsheets.add(bsr); fixTabIdRecord(); } } public void removeSheet(int sheetnum) { if (boundsheets.size() > sheetnum) { records.remove(records.getBspos() - (boundsheets.size() - 1) + sheetnum);// records.bspos--; boundsheets.remove(sheetnum); fixTabIdRecord(); } } /** * 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); } /** * returns the number of boundsheet objects contained in this workbook. * * @return number of BoundSheet records */ public int getNumSheets() { log.log(DEBUG, "getNumSheets=", new Integer(boundsheets.size())); return boundsheets.size(); } /** * get the number of ExtendedFormat records contained in this workbook. * * @return int count of ExtendedFormat records */ public int getNumExFormats() { log.log(DEBUG, "getXF=", new Integer(numxfs)); return numxfs; } /** * gets the ExtendedFormatRecord at the given 0-based index * * @param index of the Extended format record (0-based) * @return ExtendedFormatRecord at the given index */ public ExtendedFormatRecord getExFormatAt(int index) { int xfptr = records.getXfpos() - (numxfs - 1); xfptr += index; ExtendedFormatRecord retval = ( ExtendedFormatRecord ) records.get(xfptr); return retval; } /** * creates a new Cell-type Extneded Format Record and adds it to the end of * ExtendedFormatRecords collection * * @return ExtendedFormatRecord that was created */ public ExtendedFormatRecord createCellXF() { ExtendedFormatRecord xf = createExtendedFormat(); records.add(records.getXfpos()+1, xf); records.setXfpos( records.getXfpos() + 1 ); numxfs++; return xf; } /** * Adds a string to the SST table and returns its index (if its a duplicate * just returns its index and update the counts) * * @param string the string to be added to the SSTRecord * @param use16bits whether to use utf 16 or false for compressed unicode * @return index of the string within the SSTRecord */ public int addSSTString(String string, boolean use16bits) { log.log(DEBUG, "insert to sst string='", string, "' and use16bits= ", new Boolean(use16bits)); if (sst == null) { insertSST(); } return sst.addString(string, use16bits); } /** * Adds a string to the SST table and returns its index (if its a duplicate * just returns its index and update the counts) ASSUMES compressed unicode * (meaning 8bit) * * @param string the string to be added to the SSTRecord * * @return index of the string within the SSTRecord */ public int addSSTString(String string) { return addSSTString(string, false); } /** * given an index into the SST table, this function returns the corresponding String value * @return String containing the SST String */ public String getSSTString(int str) { if (sst == null) { insertSST(); } String retval = sst.getString(str); log.log(DEBUG, "Returning SST for index=", new Integer(str), " String= ", retval); return retval; } /** * use this function to add a Shared String Table to an existing sheet (say * generated by a different java api) without an sst.... * @see #createSST() * @see org.apache.poi.hssf.record.SSTRecord */ public void insertSST() { log.log(DEBUG, "creating new SST via insertSST!"); sst = ( SSTRecord ) createSST(); records.add(records.size() - 1, createExtendedSST()); records.add(records.size() - 2, sst); } /** * Serializes all records int the worksheet section into a big byte array. Use * this to write the Workbook out. * * @return byte array containing the HSSF-only portions of the POIFS file. */ // GJS: Not used so why keep it.// public byte [] serialize() {// log.log(DEBUG, "Serializing Workbook!");// byte[] retval = null;////// ArrayList bytes = new ArrayList(records.size());// int arraysize = getSize();// int pos = 0;//// retval = new byte[ arraysize ];// for (int k = 0; k < records.size(); k++) {//// Record record = records.get(k);//// Let's skip RECALCID records, as they are only use for optimization// if(record.getSid() != RecalcIdRecord.sid || ((RecalcIdRecord)record).isNeeded()) {// pos += record.serialize(pos, retval); // rec.length;// }// }// log.log(DEBUG, "Exiting serialize workbook");// return retval;// } /** * Serializes all records int the worksheet section into a big byte array. Use * this to write the Workbook out. * @param offset of the data to be written * @param data array of bytes to write this to */ public int serialize( int offset, byte[] data ) { log.log( DEBUG, "Serializing Workbook with offsets" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -