hssfcellutil.java
来自「EXCEL read and write」· Java 代码 · 共 406 行 · 第 1/2 页
JAVA
406 行
Map values = getFormatProperties( originalStyle ); values.put( propertyName, propertyValue ); // index seems like what index the cellstyle is in the list of styles for a workbook. // not good to compare on! short numberCellStyles = workbook.getNumCellStyles(); for ( short i = 0; i < numberCellStyles; i++ ) { HSSFCellStyle wbStyle = workbook.getCellStyleAt( i ); Map wbStyleMap = getFormatProperties( wbStyle ); if ( wbStyleMap.equals( values ) ) { newStyle = wbStyle; break; } } if ( newStyle == null ) { newStyle = workbook.createCellStyle(); setFormatProperties( newStyle, workbook, values ); } cell.setCellStyle( newStyle ); } /** * Returns a map containing the format properties of the given cell style. * * @param style cell style * @return map of format properties (String -> Object) * @see #setFormatProperties(HSSFCellStyle, Map) */ private static Map getFormatProperties(HSSFCellStyle style) { Map properties = new HashMap(); putShort( properties, ALIGNMENT, style.getAlignment() ); putShort( properties, BORDER_BOTTOM, style.getBorderBottom() ); putShort( properties, BORDER_LEFT, style.getBorderLeft() ); putShort( properties, BORDER_RIGHT, style.getBorderRight() ); putShort( properties, BORDER_TOP, style.getBorderTop() ); putShort( properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor() ); putShort( properties, DATA_FORMAT, style.getDataFormat() ); putShort( properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor() ); putShort( properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor() ); putShort( properties, FILL_PATTERN, style.getFillPattern() ); putShort( properties, FONT, style.getFontIndex() ); putBoolean( properties, HIDDEN, style.getHidden() ); putShort( properties, INDENTION, style.getIndention() ); putShort( properties, LEFT_BORDER_COLOR, style.getLeftBorderColor() ); putBoolean( properties, LOCKED, style.getLocked() ); putShort( properties, RIGHT_BORDER_COLOR, style.getRightBorderColor() ); putShort( properties, ROTATION, style.getRotation() ); putShort( properties, TOP_BORDER_COLOR, style.getTopBorderColor() ); putShort( properties, VERTICAL_ALIGNMENT, style.getVerticalAlignment() ); putBoolean( properties, WRAP_TEXT, style.getWrapText() ); return properties; } /** * Sets the format properties of the given style based on the given map. * * @param style cell style * @param workbook parent workbook * @param properties map of format properties (String -> Object) * @see #getFormatProperties(HSSFCellStyle) */ private static void setFormatProperties( HSSFCellStyle style, HSSFWorkbook workbook, Map properties) { style.setAlignment( getShort( properties, ALIGNMENT ) ); style.setBorderBottom( getShort( properties, BORDER_BOTTOM ) ); style.setBorderLeft( getShort( properties, BORDER_LEFT ) ); style.setBorderRight( getShort( properties, BORDER_RIGHT ) ); style.setBorderTop( getShort( properties, BORDER_TOP ) ); style.setBottomBorderColor( getShort( properties, BOTTOM_BORDER_COLOR ) ); style.setDataFormat( getShort( properties, DATA_FORMAT ) ); style.setFillBackgroundColor( getShort( properties, FILL_BACKGROUND_COLOR ) ); style.setFillForegroundColor( getShort( properties, FILL_FOREGROUND_COLOR ) ); style.setFillPattern( getShort( properties, FILL_PATTERN ) ); style.setFont( workbook.getFontAt( getShort( properties, FONT ) ) ); style.setHidden( getBoolean( properties, HIDDEN ) ); style.setIndention( getShort( properties, INDENTION ) ); style.setLeftBorderColor( getShort( properties, LEFT_BORDER_COLOR ) ); style.setLocked( getBoolean( properties, LOCKED ) ); style.setRightBorderColor( getShort( properties, RIGHT_BORDER_COLOR ) ); style.setRotation( getShort( properties, ROTATION ) ); style.setTopBorderColor( getShort( properties, TOP_BORDER_COLOR ) ); style.setVerticalAlignment( getShort( properties, VERTICAL_ALIGNMENT ) ); style.setWrapText( getBoolean( properties, WRAP_TEXT ) ); } /** * Utility method that returns the named short value form the given map. * Returns zero if the property does not exist, or is not a {@link Short}. * * @param properties map of named properties (String -> Object) * @param name property name * @return property value, or zero */ private static short getShort(Map properties, String name) { Object value = properties.get( name ); if ( value instanceof Short ) { return ((Short) value).shortValue(); } else { return 0; } } /** * Utility method that returns the named boolean value form the given map. * Returns false if the property does not exist, or is not a {@link Boolean}. * * @param properties map of properties (String -> Object) * @param name property name * @return property value, or false */ private static boolean getBoolean(Map properties, String name) { Object value = properties.get( name ); if ( value instanceof Boolean ) { return ((Boolean) value).booleanValue(); } else { return false; } } /** * Utility method that puts the named short value to the given map. * * @param properties map of properties (String -> Object) * @param name property name * @param value property value */ private static void putShort(Map properties, String name, short value) { properties.put( name, new Short( value ) ); } /** * Utility method that puts the named boolean value to the given map. * * @param properties map of properties (String -> Object) * @param name property name * @param value property value */ private static void putBoolean(Map properties, String name, boolean value) { properties.put( name, new Boolean( value ) ); } /** * Looks for text in the cell that should be unicode, like α and provides the * unicode version of it. * *@param cell The cell to check for unicode values *@return transalted to unicode */ public static HSSFCell translateUnicodeValues( HSSFCell cell ) { String s = cell.getRichStringCellValue().getString(); boolean foundUnicode = false; String lowerCaseStr = s.toLowerCase(); for (int i = 0; i < unicodeMappings.length; i++) { UnicodeMapping entry = unicodeMappings[i]; String key = entry.entityName; if ( lowerCaseStr.indexOf( key ) != -1 ) { s = s.replaceAll(key, entry.resolvedValue); foundUnicode = true; } } if ( foundUnicode ) { cell.setCellValue(new HSSFRichTextString(s)); } return cell; } static { unicodeMappings = new UnicodeMapping[] { um("alpha", "\u03B1" ), um("beta", "\u03B2" ), um("gamma", "\u03B3" ), um("delta", "\u03B4" ), um("epsilon", "\u03B5" ), um("zeta", "\u03B6" ), um("eta", "\u03B7" ), um("theta", "\u03B8" ), um("iota", "\u03B9" ), um("kappa", "\u03BA" ), um("lambda", "\u03BB" ), um("mu", "\u03BC" ), um("nu", "\u03BD" ), um("xi", "\u03BE" ), um("omicron", "\u03BF" ), }; } private static UnicodeMapping um(String entityName, String resolvedValue) { return new UnicodeMapping(entityName, resolvedValue); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?