📄 hssfworkbook.java
字号:
*/ public short getSelectedTab() { return workbook.getWindowOne().getSelectedTab(); } /** * sets the first tab that is displayed in the list of tabs * in excel. * @param index */ public void setDisplayedTab(short index) { workbook.getWindowOne().setDisplayedTab(index); } /** * sets the first tab that is displayed in the list of tabs * in excel. * @return */ public short getDisplayedTab() { return workbook.getWindowOne().getDisplayedTab(); } /** * @deprecated POI will now properly handle unicode strings without * forceing an encoding */ public final static byte ENCODING_COMPRESSED_UNICODE = 0; /** * @deprecated POI will now properly handle unicode strings without * forceing an encoding */ public final static byte ENCODING_UTF_16 = 1; /** * set the sheet name. * Will throw IllegalArgumentException if the name is greater than 31 chars * or contains /\?*[] * @param sheet number (0 based) */ public void setSheetName(int sheet, String name) { if (workbook.doesContainsSheetName( name, sheet )) throw new IllegalArgumentException( "The workbook already contains a sheet with this name" ); if (sheet > (sheets.size() - 1)) { throw new RuntimeException("Sheet out of bounds"); } workbook.setSheetName( sheet, name); } /** * set the sheet name forcing the encoding. Forcing the encoding IS A BAD IDEA!!! * @deprecated 3-Jan-2006 POI now automatically detects unicode and sets the encoding * appropriately. Simply use setSheetName(int sheet, String encoding) * @throws IllegalArgumentException if the name is greater than 31 chars * or contains /\?*[] * @param sheet number (0 based) */ public void setSheetName( int sheet, String name, short encoding ) { if (workbook.doesContainsSheetName( name, sheet )) throw new IllegalArgumentException( "The workbook already contains a sheet with this name" ); if (sheet > (sheets.size() - 1)) { throw new RuntimeException("Sheet out of bounds"); } switch ( encoding ) { case ENCODING_COMPRESSED_UNICODE: case ENCODING_UTF_16: break; default: // TODO java.io.UnsupportedEncodingException throw new RuntimeException( "Unsupported encoding" ); } workbook.setSheetName( sheet, name, encoding ); } /** * get the sheet name * @param sheet Number * @return Sheet name */ public String getSheetName(int sheet) { if (sheet > (sheets.size() - 1)) { throw new RuntimeException("Sheet out of bounds"); } return workbook.getSheetName(sheet); } /* * get the sheet's index * @param name sheet name * @return sheet index or -1 if it was not found. */ /** Returns the index of the sheet by his name * @param name the sheet name * @return index of the sheet (0 based) */ public int getSheetIndex(String name) { int retval = workbook.getSheetIndex(name); return retval; } /** * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns * the high level representation. Use this to create new sheets. * * @return HSSFSheet representing the new sheet. */ public HSSFSheet createSheet() {// if (getNumberOfSheets() == 3)// throw new RuntimeException("You cannot have more than three sheets in HSSF 1.0"); HSSFSheet sheet = new HSSFSheet(workbook); sheets.add(sheet); workbook.setSheetName(sheets.size() - 1, "Sheet" + (sheets.size() - 1)); WindowTwoRecord windowTwo = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid); windowTwo.setSelected(sheets.size() == 1); windowTwo.setPaged(sheets.size() == 1); return sheet; } /** * create an HSSFSheet from an existing sheet in the HSSFWorkbook. * * @return HSSFSheet representing the cloned sheet. */ public HSSFSheet cloneSheet(int sheetNum) { HSSFSheet srcSheet = (HSSFSheet)sheets.get(sheetNum); String srcName = workbook.getSheetName(sheetNum); if (srcSheet != null) { HSSFSheet clonedSheet = srcSheet.cloneSheet(workbook); WindowTwoRecord windowTwo = (WindowTwoRecord) clonedSheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid); windowTwo.setSelected(sheets.size() == 1); windowTwo.setPaged(sheets.size() == 1); sheets.add(clonedSheet); int i=1; while (true) { //Try and find the next sheet name that is unique String name = srcName; String index = Integer.toString(i++); if (name.length()+index.length()+2<31) name = name + "("+index+")"; else name = name.substring(0, 31-index.length()-2)+"("+index+")"; //If the sheet name is unique, then set it otherwise move on to the next number. if (workbook.getSheetIndex(name) == -1) { workbook.setSheetName(sheets.size()-1, name); break; } } return clonedSheet; } return null; } /** * create an HSSFSheet for this HSSFWorkbook, adds it to the sheets and returns * the high level representation. Use this to create new sheets. * * @param sheetname sheetname to set for the sheet. * @return HSSFSheet representing the new sheet. */ public HSSFSheet createSheet(String sheetname) { if (workbook.doesContainsSheetName( sheetname, sheets.size() )) throw new IllegalArgumentException( "The workbook already contains a sheet of this name" ); HSSFSheet sheet = new HSSFSheet(workbook); sheets.add(sheet); workbook.setSheetName(sheets.size() - 1, sheetname); WindowTwoRecord windowTwo = (WindowTwoRecord) sheet.getSheet().findFirstRecordBySid(WindowTwoRecord.sid); windowTwo.setSelected(sheets.size() == 1); windowTwo.setPaged(sheets.size() == 1); return sheet; } /** * get the number of spreadsheets in the workbook (this will be three after serialization) * @return number of sheets */ public int getNumberOfSheets() { return sheets.size(); } /** * Get the HSSFSheet object at the given index. * @param index of the sheet number (0-based physical & logical) * @return HSSFSheet at the provided index */ public HSSFSheet getSheetAt(int index) { return (HSSFSheet) sheets.get(index); } /** * Get sheet with the given name * @param name of the sheet * @return HSSFSheet with the name provided or null if it does not exist */ public HSSFSheet getSheet(String name) { HSSFSheet retval = null; for (int k = 0; k < sheets.size(); k++) { String sheetname = workbook.getSheetName(k); if (sheetname.equals(name)) { retval = (HSSFSheet) sheets.get(k); } } return retval; } /** * removes sheet at the given index * @param index of the sheet (0-based) */ public void removeSheetAt(int index) { sheets.remove(index); workbook.removeSheet(index); } /** * determine whether the Excel GUI will backup the workbook when saving. * * @param backupValue true to indicate a backup will be performed. */ public void setBackupFlag(boolean backupValue) { BackupRecord backupRecord = workbook.getBackupRecord(); backupRecord.setBackup(backupValue ? (short) 1 : (short) 0); } /** * determine whether the Excel GUI will backup the workbook when saving. * * @return the current setting for backups. */ public boolean getBackupFlag() { BackupRecord backupRecord = workbook.getBackupRecord(); return (backupRecord.getBackup() == 0) ? false : true; } /** * Sets the repeating rows and columns for a sheet (as found in * File->PageSetup->Sheet). This is function is included in the workbook * because it creates/modifies name records which are stored at the * workbook level. * <p> * To set just repeating columns: * <pre> * workbook.setRepeatingRowsAndColumns(0,0,1,-1-1); * </pre> * To set just repeating rows: * <pre> * workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4); * </pre> * To remove all repeating rows and columns for a sheet. * <pre> * workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1); * </pre> * * @param sheetIndex 0 based index to sheet. * @param startColumn 0 based start of repeating columns. * @param endColumn 0 based end of repeating columns. * @param startRow 0 based start of repeating rows. * @param endRow 0 based end of repeating rows. */ public void setRepeatingRowsAndColumns(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) { // Check arguments if (startColumn == -1 && endColumn != -1) throw new IllegalArgumentException("Invalid column range specification"); if (startRow == -1 && endRow != -1) throw new IllegalArgumentException("Invalid row range specification"); if (startColumn < -1 || startColumn >= 0xFF) throw new IllegalArgumentException("Invalid column range specification"); if (endColumn < -1 || endColumn >= 0xFF) throw new IllegalArgumentException("Invalid column range specification"); if (startRow < -1 || startRow > 65535) throw new IllegalArgumentException("Invalid row range specification"); if (endRow < -1 || endRow > 65535) throw new IllegalArgumentException("Invalid row range specification"); if (startColumn > endColumn) throw new IllegalArgumentException("Invalid column range specification"); if (startRow > endRow) throw new IllegalArgumentException("Invalid row range specification"); HSSFSheet sheet = getSheetAt(sheetIndex); short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex); boolean settingRowAndColumn = startColumn != -1 && endColumn != -1 && startRow != -1 && endRow != -1; boolean removingRange = startColumn == -1 && endColumn == -1 && startRow == -1 && endRow == -1; boolean isNewRecord = false; NameRecord nameRecord; nameRecord = findExistingRowColHeaderNameRecord(sheetIndex); if (removingRange ) { if (nameRecord != null) workbook.removeName(findExistingRowColHeaderNameRecordIdx(sheetIndex+1)); return; } if ( nameRecord == null ) { nameRecord = workbook.createBuiltInName(NameRecord.BUILTIN_PRINT_TITLE, sheetIndex+1); //does a lot of the house keeping for builtin records, like setting lengths to zero etc isNewRecord = true; } short definitionTextLength = settingRowAndColumn ? (short)0x001a : (short)0x000b;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -