1 23 24 package org.apache.slide.extractor; 25 26 31 32 import java.io.*; 33 import java.util.Iterator ; 34 35 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 36 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 37 import org.apache.poi.hssf.usermodel.HSSFSheet; 38 import org.apache.poi.hssf.usermodel.HSSFRow; 39 import org.apache.poi.hssf.usermodel.HSSFCell; 40 41 public class MSExcelExtractor extends AbstractContentExtractor 42 { 43 public MSExcelExtractor(String uri, String contentType, String namespace) { 44 super(uri, contentType, namespace); 45 } 46 47 public Reader extract(InputStream content) throws ExtractorException 48 { 49 try 50 { 51 CharArrayWriter writer = new CharArrayWriter(); 52 53 POIFSFileSystem fs = new POIFSFileSystem(content); 54 HSSFWorkbook workbook = new HSSFWorkbook(fs); 55 56 for (int i = 0; i < workbook.getNumberOfSheets(); i++ ) 57 { 58 HSSFSheet sheet = workbook.getSheetAt(i); 59 60 Iterator rows = sheet.rowIterator(); 61 while( rows.hasNext() ) { 62 HSSFRow row = (HSSFRow) rows.next(); 63 64 Iterator cells = row.cellIterator(); 65 while( cells.hasNext() ) { 66 HSSFCell cell = (HSSFCell) cells.next(); 67 switch ( cell.getCellType() ) { 68 case HSSFCell.CELL_TYPE_NUMERIC: 69 String num = Double.toString(cell.getNumericCellValue()).trim(); 70 if(num.length() > 0) 71 writer.write(num + " "); 72 break; 73 case HSSFCell.CELL_TYPE_STRING: 74 String text = cell.getStringCellValue().trim(); 75 if(text.length() > 0) 76 writer.write(text + " "); 77 break; 78 } 79 } 80 } 81 } 82 83 return new CharArrayReader(writer.toCharArray()); 84 } 85 catch(Exception e ) 86 { 87 throw new ExtractorException(e.getMessage()); 88 } 89 } 90 91 public static void main(String [] args) throws Exception 92 { 93 FileInputStream in = new FileInputStream(args[0]); 94 95 MSExcelExtractor ex = new MSExcelExtractor(null, null, null); 96 97 Reader reader = ex.extract(in); 98 99 int c = 0; 100 do 101 { 102 c = reader.read(); 103 System.out.print((char)c); 104 } 105 while(c != -1); 106 } 107 } | Popular Tags |