1 16 17 18 package org.apache.poi.hssf.dev; 19 20 import java.io.FileInputStream ; 21 import java.io.FileOutputStream ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 25 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 26 import org.apache.poi.hssf.record.*; 27 import org.apache.poi.hssf.eventmodel.*; 28 import org.apache.poi.hssf.eventusermodel.*; 29 import org.apache.poi.hssf.usermodel.*; 30 31 import org.apache.poi.hssf.eventusermodel.HSSFRequest; 32 import org.apache.poi.hssf.eventusermodel.HSSFListener; 33 import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; 34 35 39 40 public class EFHSSF 41 { 42 String infile; 43 String outfile; 44 HSSFWorkbook workbook = null; 45 HSSFSheet cursheet = null; 46 47 48 49 public EFHSSF() 50 { 51 } 52 53 public void setInputFile(String infile) 54 { 55 this.infile = infile; 56 } 57 58 public void setOutputFile(String outfile) 59 { 60 this.outfile = outfile; 61 } 62 63 public void run() 64 throws IOException 65 { 66 FileInputStream fin = new FileInputStream (infile); 67 POIFSFileSystem poifs = new POIFSFileSystem(fin); 68 InputStream din = poifs.createDocumentInputStream("Workbook"); 69 HSSFRequest req = new HSSFRequest(); 70 71 req.addListenerForAllRecords(new EFHSSFListener(this)); 72 HSSFEventFactory factory = new HSSFEventFactory(); 73 74 factory.processEvents(req, din); 75 fin.close(); 76 din.close(); 77 FileOutputStream fout = new FileOutputStream (outfile); 78 79 workbook.write(fout); 80 fout.close(); 81 System.out.println("done."); 82 } 83 84 public void recordHandler(Record record) 85 { 86 HSSFRow row = null; 87 HSSFCell cell = null; 88 int sheetnum = -1; 89 90 switch (record.getSid()) 91 { 92 93 case BOFRecord.sid : 94 BOFRecord bof = ( BOFRecord ) record; 95 96 if (bof.getType() == bof.TYPE_WORKBOOK) 97 { 98 workbook = new HSSFWorkbook(); 99 } 100 else if (bof.getType() == bof.TYPE_WORKSHEET) 101 { 102 sheetnum++; 103 cursheet = workbook.getSheetAt(sheetnum); 104 } 105 break; 106 107 case BoundSheetRecord.sid : 108 BoundSheetRecord bsr = ( BoundSheetRecord ) record; 109 110 workbook.createSheet(bsr.getSheetname()); 111 break; 112 113 case RowRecord.sid : 114 RowRecord rowrec = ( RowRecord ) record; 115 116 cursheet.createRow(rowrec.getRowNumber()); 117 break; 118 119 case NumberRecord.sid : 120 NumberRecord numrec = ( NumberRecord ) record; 121 122 row = cursheet.getRow(numrec.getRow()); 123 cell = row.createCell(numrec.getColumn(), 124 HSSFCell.CELL_TYPE_NUMERIC); 125 cell.setCellValue(numrec.getValue()); 126 break; 127 128 case SSTRecord.sid : 129 SSTRecord sstrec = ( SSTRecord ) record; 130 131 for (int k = 0; k < sstrec.getNumUniqueStrings(); k++) 132 { 133 workbook.addSSTString(sstrec.getString(k)); 134 } 135 break; 136 137 case LabelSSTRecord.sid : 138 LabelSSTRecord lrec = ( LabelSSTRecord ) record; 139 140 row = cursheet.getRow(lrec.getRow()); 141 cell = row.createCell(lrec.getColumn(), 142 HSSFCell.CELL_TYPE_STRING); 143 cell.setCellValue(workbook.getSSTString(lrec.getSSTIndex())); 144 break; 145 } 146 } 147 148 public static void main(String [] args) 149 { 150 if ((args.length < 2) || !args[ 0 ].equals("--help")) 151 { 152 try 153 { 154 EFHSSF viewer = new EFHSSF(); 155 156 viewer.setInputFile(args[ 0 ]); 157 viewer.setOutputFile(args[ 1 ]); 158 viewer.run(); 159 } 160 catch (IOException e) 161 { 162 e.printStackTrace(); 163 } 164 } 165 else 166 { 167 System.out.println("EFHSSF"); 168 System.out.println( 169 "General testbed for HSSFEventFactory based testing and " 170 + "Code examples"); 171 System.out.println("Usage: java org.apache.poi.hssf.dev.EFHSSF " 172 + "file1 file2"); 173 System.out.println( 174 " --will rewrite the file reading with the event api"); 175 System.out.println("and writing with the standard API"); 176 } 177 } 178 } 179 180 class EFHSSFListener 181 implements HSSFListener 182 { 183 EFHSSF efhssf; 184 185 public EFHSSFListener(EFHSSF efhssf) 186 { 187 this.efhssf = efhssf; 188 } 189 190 public void processRecord(Record record) 191 { 192 efhssf.recordHandler(record); 193 } 194 } 195 | Popular Tags |