📄 hssfworkbook.java
字号:
if (name == null) name = workbook.createBuiltInName(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); //adding one here because 0 indicates a global named region; doesnt make sense for print areas short externSheetIndex = getWorkbook().checkExternSheet(sheetIndex); name.setExternSheetNumber(externSheetIndex); name.setAreaReference(reference); } /** * For the Convenience of Java Programmers maintaining pointers. * @see #setPrintArea(int, String) * @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 */ public void setPrintArea(int sheetIndex, int startColumn, int endColumn, int startRow, int endRow) { //using absolute references because they dont get copied and pasted anyway CellReference cell = new CellReference(startRow, startColumn, true, true); String reference = cell.toString(); cell = new CellReference(endRow, endColumn, true, true); reference = reference+":"+cell.toString(); setPrintArea(sheetIndex, reference); } /** * 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 */ public String getPrintArea(int sheetIndex) { NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); if (name == null) return null; //adding one here because 0 indicates a global named region; doesnt make sense for print areas return name.getAreaReference(workbook); } /** * Delete the printarea for the sheet specified * @param sheetIndex Zero-based sheet index (0 = First Sheet) */ public void removePrintArea(int sheetIndex) { getWorkbook().removeBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1); } /** creates a new named range and add it to the model * @return named range high level */ public HSSFName createName(){ NameRecord nameRecord = workbook.createName(); HSSFName newName = new HSSFName(workbook, nameRecord); names.add(newName); return newName; } /** gets the named range index by his name * <i>Note:</i>Excel named ranges are case-insensitive and * this method performs a case-insensitive search. * * @param name named range name * @return named range index */ public int getNameIndex(String name) { int retval = -1; for (int k = 0; k < names.size(); k++) { String nameName = getNameName(k); if (nameName.equalsIgnoreCase(name)) { retval = k; break; } } return retval; } /** remove the named range by his index * @param index named range index (0 based) */ public 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 */ public HSSFDataFormat createDataFormat() { if (formatter == null) formatter = new HSSFDataFormat(workbook); return formatter; } /** remove the named range by his name * @param name named range name */ public void removeName(String name){ int index = getNameIndex(name); removeName(index); } public HSSFPalette getCustomPalette() { return new HSSFPalette(workbook.getCustomPalette()); } /** * Copies nodes from one POIFS to the other minus the excepts * @param source is the source POIFS to copy from * @param target is the target POIFS to copy to * @param excepts is a list of Strings specifying what nodes NOT to copy */ private void copyNodes(POIFSFileSystem source, POIFSFileSystem target, List excepts) throws IOException { //System.err.println("CopyNodes called"); DirectoryEntry root = source.getRoot(); DirectoryEntry newRoot = target.getRoot(); Iterator entries = root.getEntries(); while (entries.hasNext()) { Entry entry = (Entry)entries.next(); if (!isInList(entry.getName(), excepts)) { copyNodeRecursively(entry,newRoot); } } } private boolean isInList(String entry, List list) { for (int k = 0; k < list.size(); k++) { if (list.get(k).equals(entry)) { return true; } } return false; } private void copyNodeRecursively(Entry entry, DirectoryEntry target) throws IOException { //System.err.println("copyNodeRecursively called with "+entry.getName()+ // ","+target.getName()); DirectoryEntry newTarget = null; if (entry.isDirectoryEntry()) { newTarget = target.createDirectory(entry.getName()); Iterator entries = ((DirectoryEntry)entry).getEntries(); while (entries.hasNext()) { copyNodeRecursively((Entry)entries.next(),newTarget); } } else { DocumentEntry dentry = (DocumentEntry)entry; DocumentInputStream dstream = new DocumentInputStream(dentry); target.createDocument(dentry.getName(),dstream); dstream.close(); } } /** 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, }; UnknownRecord r = new UnknownRecord((short)0x00EB, data); workbook.getRecords().add(loc, r); } /** * Spits out a list of all the drawing records in the workbook. */ public void dumpDrawingGroupRecords(boolean fat) { DrawingGroupRecord r = (DrawingGroupRecord) workbook.findFirstRecordBySid( DrawingGroupRecord.sid ); r.decode(); List escherRecords = r.getEscherRecords(); PrintWriter w = new PrintWriter(System.out); for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) { EscherRecord escherRecord = (EscherRecord) iterator.next(); if (fat) System.out.println(escherRecord.toString()); else escherRecord.display(w, 0); } w.flush(); } /** * 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> * * @return the index to this picture (1 based). */ public int addPicture(byte[] pictureData, int format) { byte[] uid = newUID(); EscherBitmapBlip blipRecord = new EscherBitmapBlip(); blipRecord.setRecordId( (short) ( EscherBitmapBlip.RECORD_ID_START + format ) ); switch (format) { case PICTURE_TYPE_EMF: blipRecord.setOptions(HSSFPictureData.MSOBI_EMF); break; case PICTURE_TYPE_WMF: blipRecord.setOptions(HSSFPictureData.MSOBI_WMF); break; case PICTURE_TYPE_PICT: blipRecord.setOptions(HSSFPictureData.MSOBI_PICT); break; case PICTURE_TYPE_PNG: blipRecord.setOptions(HSSFPictureData.MSOBI_PNG); break; case HSSFWorkbook.PICTURE_TYPE_JPEG: blipRecord.setOptions(HSSFPictureData.MSOBI_JPEG); break; case HSSFWorkbook.PICTURE_TYPE_DIB: blipRecord.setOptions(HSSFPictureData.MSOBI_DIB); break; } blipRecord.setUID( uid ); blipRecord.setMarker( (byte) 0xFF ); blipRecord.setPictureData( pictureData ); 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( (short) 0xFF ); r.setSize( pictureData.length + 25 ); r.setRef( 1 ); r.setOffset( 0 ); r.setBlipRecord( blipRecord ); return workbook.addBSERecord( r ); } /** * Gets all pictures from the Workbook. * * @return the list of pictures (a list of {@link HSSFPictureData} objects.) */ public List getAllPictures() { List pictures = new ArrayList(); Iterator recordIter = workbook.getRecords().iterator(); while (recordIter.hasNext()) { Object obj = recordIter.next(); if (obj instanceof AbstractEscherHolderRecord) { ((AbstractEscherHolderRecord) obj).decode(); List escherRecords = ((AbstractEscherHolderRecord) obj).getEscherRecords(); searchForPictures(escherRecords, pictures); } } return pictures; } /** * 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. */ private void searchForPictures(List escherRecords, List pictures) { Iterator recordIter = escherRecords.iterator(); while (recordIter.hasNext()) { Object obj = recordIter.next(); if (obj instanceof EscherRecord) { EscherRecord escherRecord = (EscherRecord) obj; if (escherRecord instanceof EscherBSERecord) { EscherBlipRecord blip = ((EscherBSERecord) escherRecord).getBlipRecord(); if (blip instanceof EscherBitmapBlip) { // TODO: Some kind of structure. pictures.add(new HSSFPictureData((EscherBitmapBlip) blip)); } } // Recursive call. searchForPictures(escherRecord.getChildRecords(), pictures); } } } private byte[] newUID() { byte[] bytes = new byte[16]; return bytes; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -