1 16 17 18 package org.apache.poi.hssf.dev; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 25 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 26 import org.apache.poi.hssf.record.*; 27 import org.apache.poi.hssf.usermodel.*; 28 import org.apache.poi.hssf.util.*; 29 30 39 40 public class HSSF 41 { 42 private String filename = null; 43 44 private InputStream stream = null; 46 private Record[] records = null; 47 protected HSSFWorkbook hssfworkbook = null; 48 49 59 60 public HSSF(String filename) 61 throws IOException 62 { 63 this.filename = filename; 64 POIFSFileSystem fs = 65 new POIFSFileSystem(new FileInputStream (filename)); 66 67 hssfworkbook = new HSSFWorkbook(fs); 68 69 } 71 72 83 84 public HSSF(String filename, boolean write) 85 throws IOException 86 { 87 short rownum = 0; 88 FileOutputStream out = new FileOutputStream (filename); 89 HSSFWorkbook wb = new HSSFWorkbook(); 90 HSSFSheet s = wb.createSheet(); 91 HSSFRow r = null; 92 HSSFCell c = null; 93 HSSFCellStyle cs = wb.createCellStyle(); 94 HSSFCellStyle cs2 = wb.createCellStyle(); 95 HSSFCellStyle cs3 = wb.createCellStyle(); 96 HSSFFont f = wb.createFont(); 97 HSSFFont f2 = wb.createFont(); 98 99 f.setFontHeightInPoints(( short ) 12); 100 f.setColor(( short ) 0xA); 101 f.setBoldweight(f.BOLDWEIGHT_BOLD); 102 f2.setFontHeightInPoints(( short ) 10); 103 f2.setColor(( short ) 0xf); 104 f2.setBoldweight(f2.BOLDWEIGHT_BOLD); 105 cs.setFont(f); 106 cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); 107 cs2.setBorderBottom(cs2.BORDER_THIN); 108 cs2.setFillPattern(( short ) 1); cs2.setFillForegroundColor(( short ) 0xA); 110 cs2.setFont(f2); 111 wb.setSheetName(0, "HSSF Test"); 112 for (rownum = ( short ) 0; rownum < 300; rownum++) 113 { 114 r = s.createRow(rownum); 115 if ((rownum % 2) == 0) 116 { 117 r.setHeight(( short ) 0x249); 118 } 119 120 for (short cellnum = ( short ) 0; cellnum < 50; cellnum += 2) 122 { 123 c = r.createCell(cellnum, HSSFCell.CELL_TYPE_NUMERIC); 124 c.setCellValue(rownum * 10000 + cellnum 125 + ((( double ) rownum / 1000) 126 + (( double ) cellnum / 10000))); 127 if ((rownum % 2) == 0) 128 { 129 c.setCellStyle(cs); 130 } 131 c = r.createCell(( short ) (cellnum + 1), 132 HSSFCell.CELL_TYPE_STRING); 133 c.setCellValue("TEST"); 134 s.setColumnWidth(( short ) (cellnum + 1), 135 ( short ) ((50 * 8) / (( double ) 1 / 20))); 136 if ((rownum % 2) == 0) 137 { 138 c.setCellStyle(cs2); 139 } 140 } } 142 143 rownum++; 145 rownum++; 146 r = s.createRow(rownum); 147 cs3.setBorderBottom(cs3.BORDER_THICK); 148 for (short cellnum = ( short ) 0; cellnum < 50; cellnum++) 149 { 150 c = r.createCell(cellnum, HSSFCell.CELL_TYPE_BLANK); 151 152 c.setCellStyle(cs3); 154 } 155 s.addMergedRegion(new Region(( short ) 0, ( short ) 0, ( short ) 3, 156 ( short ) 3)); 157 s.addMergedRegion(new Region(( short ) 100, ( short ) 100, 158 ( short ) 110, ( short ) 110)); 159 160 s = wb.createSheet(); 163 wb.setSheetName(1, "DeletedSheet"); 164 wb.removeSheetAt(1); 165 166 wb.write(out); 168 out.close(); 169 } 170 171 182 183 public HSSF(String infile, String outfile, boolean write) 184 throws IOException 185 { 186 this.filename = filename; 187 POIFSFileSystem fs = 188 new POIFSFileSystem(new FileInputStream (filename)); 189 190 hssfworkbook = new HSSFWorkbook(fs); 191 192 } 194 195 218 219 public static void main(String [] args) 220 { 221 if (args.length < 2) 222 { 223 224 278 } 279 else if (args.length == 2) 280 { 281 if (args[ 1 ].toLowerCase().equals("write")) 282 { 283 System.out.println("Write mode"); 284 try 285 { 286 long time = System.currentTimeMillis(); 287 HSSF hssf = new HSSF(args[ 0 ], true); 288 289 System.out 290 .println("" + (System.currentTimeMillis() - time) 291 + " ms generation time"); 292 } 293 catch (Exception e) 294 { 295 e.printStackTrace(); 296 } 297 } 298 else 299 { 300 System.out.println("readwrite test"); 301 try 302 { 303 HSSF hssf = new HSSF(args[ 0 ]); 304 305 HSSFWorkbook wb = hssf.hssfworkbook; 307 FileOutputStream stream = new FileOutputStream (args[ 1 ]); 308 309 wb.write(stream); 315 stream.close(); 316 } 317 catch (Exception e) 318 { 319 e.printStackTrace(); 320 } 321 } 322 } 323 else if ((args.length == 3) 324 && args[ 2 ].toLowerCase().equals("modify1")) 325 { 326 try { 328 HSSF hssf = new HSSF(args[ 0 ]); 329 330 HSSFWorkbook wb = hssf.hssfworkbook; 332 FileOutputStream stream = new FileOutputStream (args[ 1 ]); 333 HSSFSheet sheet = wb.getSheetAt(0); 334 335 for (int k = 0; k < 25; k++) 336 { 337 HSSFRow row = sheet.getRow(k); 338 339 sheet.removeRow(row); 340 } 341 for (int k = 74; k < 100; k++) 342 { 343 HSSFRow row = sheet.getRow(k); 344 345 sheet.removeRow(row); 346 } 347 HSSFRow row = sheet.getRow(39); 348 HSSFCell cell = row.getCell(( short ) 3); 349 350 cell.setCellType(HSSFCell.CELL_TYPE_STRING); 351 cell.setCellValue("MODIFIED CELL!!!!!"); 352 353 wb.write(stream); 359 stream.close(); 360 } 361 catch (Exception e) 362 { 363 e.printStackTrace(); 364 } 365 } 366 } 367 } 368 | Popular Tags |