1 2 17 18 19 package org.apache.poi.hssf.usermodel.contrib; 20 21 22 import org.apache.commons.beanutils.PropertyUtils; 23 import org.apache.commons.lang.StringUtils; 24 import org.apache.commons.lang.exception.NestableException; 25 import org.apache.poi.hssf.usermodel.*; 26 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 31 41 42 public class HSSFCellUtil 43 { 44 45 private static HashMap unicodeMappings = new HashMap (); 46 47 48 55 public static HSSFRow getRow( int rowCounter, HSSFSheet sheet ) 56 { 57 HSSFRow row = sheet.getRow( (short) rowCounter ); 58 if ( row == null ) 59 { 60 row = sheet.createRow( (short) rowCounter ); 61 } 62 63 return row; 64 } 65 66 67 74 public static HSSFCell getCell( HSSFRow row, int column ) 75 { 76 HSSFCell cell = row.getCell( (short) column ); 77 78 if ( cell == null ) 79 { 80 cell = row.createCell( (short) column ); 81 } 82 return cell; 83 } 84 85 86 95 96 public static HSSFCell createCell( HSSFRow row, int column, String value, HSSFCellStyle style ) 97 { 98 HSSFCell cell = getCell( row, column ); 99 100 cell.setCellValue( value ); 101 if ( style != null ) 102 { 103 cell.setCellStyle( style ); 104 } 105 106 return cell; 107 } 108 109 110 118 public static HSSFCell createCell( HSSFRow row, int column, String value ) 119 { 120 return createCell( row, column, value, null ); 121 } 122 123 124 134 public static void setAlignment( HSSFCell cell, HSSFWorkbook workbook, short align ) throws NestableException 135 { 136 setCellStyleProperty( cell, workbook, "alignment", new Short ( align ) ); 137 } 138 139 147 public static void setFont( HSSFCell cell, HSSFWorkbook workbook, HSSFFont font ) throws NestableException 148 { 149 setCellStyleProperty( cell, workbook, "font", font ); 150 } 151 152 167 public static void setCellStyleProperty( HSSFCell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue ) 168 throws NestableException 169 { 170 try 171 { 172 HSSFCellStyle originalStyle = cell.getCellStyle(); 173 HSSFCellStyle newStyle = null; 174 Map values = PropertyUtils.describe( originalStyle ); 175 values.put( propertyName, propertyValue ); 176 values.remove( "index" ); 177 178 short numberCellStyles = workbook.getNumCellStyles(); 181 182 for ( short i = 0; i < numberCellStyles; i++ ) 183 { 184 HSSFCellStyle wbStyle = workbook.getCellStyleAt( i ); 185 Map wbStyleMap = PropertyUtils.describe( wbStyle ); 186 wbStyleMap.remove( "index" ); 187 188 if ( wbStyleMap.equals( values ) ) 189 { 190 newStyle = wbStyle; 191 break; 192 } 193 } 194 195 if ( newStyle == null ) 196 { 197 newStyle = workbook.createCellStyle(); 198 newStyle.setFont( workbook.getFontAt( originalStyle.getFontIndex() ) ); 199 PropertyUtils.copyProperties( newStyle, originalStyle ); 200 PropertyUtils.setProperty( newStyle, propertyName, propertyValue ); 201 } 202 203 cell.setCellStyle( newStyle ); 204 } 205 catch ( Exception e ) 206 { 207 e.printStackTrace(); 208 209 throw new NestableException( "Couldn't setCellStyleProperty.", e ); 210 } 211 } 212 213 214 221 public static HSSFCell translateUnicodeValues( HSSFCell cell ) 222 { 223 224 String s = cell.getStringCellValue(); 225 boolean foundUnicode = false; 226 227 for ( Iterator i = unicodeMappings.entrySet().iterator(); i.hasNext(); ) 228 { 229 Map.Entry entry = (Map.Entry ) i.next(); 230 String key = (String ) entry.getKey(); 231 if ( s.toLowerCase().indexOf( key ) != -1 ) 232 { 233 s = StringUtils.replace( s, key, "" + entry.getValue().toString() + "" ); 234 foundUnicode = true; 235 } 236 } 237 if ( foundUnicode ) 238 { 239 cell.setEncoding( HSSFCell.ENCODING_UTF_16 ); 240 cell.setCellValue( s ); 241 } 242 return cell; 243 } 244 245 246 static { 247 unicodeMappings.put( "α", "\u03B1" ); 248 unicodeMappings.put( "β", "\u03B2" ); 249 unicodeMappings.put( "γ", "\u03B3" ); 250 unicodeMappings.put( "δ", "\u03B4" ); 251 unicodeMappings.put( "ε", "\u03B5" ); 252 unicodeMappings.put( "ζ", "\u03B6" ); 253 unicodeMappings.put( "η", "\u03B7" ); 254 unicodeMappings.put( "θ", "\u03B8" ); 255 unicodeMappings.put( "ι", "\u03B9" ); 256 unicodeMappings.put( "κ", "\u03BA" ); 257 unicodeMappings.put( "λ", "\u03BB" ); 258 unicodeMappings.put( "μ", "\u03BC" ); 259 unicodeMappings.put( "ν", "\u03BD" ); 260 unicodeMappings.put( "ξ", "\u03BE" ); 261 unicodeMappings.put( "ο", "\u03BF" ); 262 } 263 264 } 265 | Popular Tags |