关于lucene处理word、excel、ppt、txt、pdf等格式文件

关于lucene处理word、excel、ppt、txt、pdf等格式文件的代码如下: 

Java代码   收藏代码
  1. public class ReadFile {  
  2.       
  3.     /** 
  4.      * 处理word2003 
  5.      * @param path 
  6.      * @return 
  7.      * @throws Exception 
  8.      */  
  9.     public static String readWord(String path) throws Exception {  
  10.         String bodyText = null;  
  11.         InputStream inputStream = new FileInputStream(path);  
  12.         WordExtractor extractor = new WordExtractor(inputStream);    
  13.         bodyText = extractor.getText();  
  14.         return bodyText;  
  15.     }  
  16.       
  17.     /** 
  18.      * 处理word2007 
  19.      * @param path 
  20.      * @return 
  21.      * @throws IOException 
  22.      * @throws OpenXML4JException 
  23.      * @throws XmlException 
  24.      */  
  25.     public static String readWord2007(String path) throws IOException, OpenXML4JException, XmlException {  
  26.         OPCPackage opcPackage = POIXMLDocument.openPackage(path);  
  27.         POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);  
  28.         return ex.getText();  
  29.     }  
  30.     /** 
  31.      * 处理excel2003 
  32.      * @param path 
  33.      * @return 
  34.      * @throws IOException 
  35.      */  
  36.     public static String ReadExcel(String path) throws IOException {  
  37.         InputStream inputStream = null;  
  38.         String content = null;  
  39.         try {  
  40.             inputStream = new FileInputStream(path);  
  41.             HSSFWorkbook wb = new HSSFWorkbook(inputStream);  
  42.             ExcelExtractor extractor = new ExcelExtractor(wb);  
  43.             extractor.setFormulasNotResults(true);  
  44.             extractor.setIncludeSheetNames(false);  
  45.             content = extractor.getText();  
  46.         } catch (FileNotFoundException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.         return content;  
  50.     }  
  51.     /** 
  52.      * 处理excel2007 
  53.      * @param path 
  54.      * @return 
  55.      * @throws IOException 
  56.      */  
  57.     public static String readExcel2007(String path) throws IOException {  
  58.         StringBuffer content = new StringBuffer();  
  59.         // 构造 XSSFWorkbook 对象,strPath 传入文件路径  
  60.         XSSFWorkbook xwb = new XSSFWorkbook(path);  
  61.         // 循环工作表Sheet  
  62.         for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {  
  63.             XSSFSheet xSheet = xwb.getSheetAt(numSheet);  
  64.             if (xSheet == null) {  
  65.                 continue;  
  66.             }  
  67.             // 循环行Row  
  68.             for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) {  
  69.                 XSSFRow xRow = xSheet.getRow(rowNum);  
  70.                 if (xRow == null) {  
  71.                     continue;  
  72.                 }  
  73.                 // 循环列Cell  
  74.                 for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) {  
  75.                     XSSFCell xCell = xRow.getCell(cellNum);  
  76.                     if (xCell == null) {  
  77.                         continue;  
  78.                     }  
  79.                     if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {  
  80.                         content.append(xCell.getBooleanCellValue());  
  81.                     } else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {  
  82.                         content.append(xCell.getNumericCellValue());  
  83.                     } else {  
  84.                         content.append(xCell.getStringCellValue());  
  85.                     }  
  86.                 }  
  87.             }  
  88.         }  
  89.   
  90.         return content.toString();  
  91.     }  
  92.     /** 
  93.      * 处理ppt 
  94.      * @param path 
  95.      * @return 
  96.      */  
  97.     public static String readPowerPoint(String path) {  
  98.         StringBuffer content = new StringBuffer("");  
  99.         InputStream inputStream = null;  
  100.         try {  
  101.             inputStream = new FileInputStream(path);  
  102.             SlideShow ss = new SlideShow(new HSLFSlideShow(new FileInputStream(path)));// is  
  103.             // 为文件的InputStream,建立SlideShow  
  104.             Slide[] slides = ss.getSlides();// 获得每一张幻灯片  
  105.             for (int i = 0; i < slides.length; i++) {  
  106.                 TextRun[] t = slides[i].getTextRuns();// 为了取得幻灯片的文字内容,建立TextRun  
  107.                 for (int j = 0; j < t.length; j++) {  
  108.                     content.append(t[j].getText());// 这里会将文字内容加到content中去  
  109.                 }  
  110.             }  
  111.         } catch (Exception ex) {  
  112.             System.out.println(ex.toString());  
  113.         }  
  114.         return content.toString();  
  115.     }  
  116.     /** 
  117.      * 处理pdf 
  118.      * @param path 
  119.      * @return 
  120.      * @throws IOException 
  121.      */  
  122.     public static String readPdf(String path) throws IOException {  
  123.         StringBuffer content = new StringBuffer("");// 文档内容  
  124.         PDDocument pdfDocument = null;  
  125.         try {  
  126.             FileInputStream fis = new FileInputStream(path);  
  127.             PDFTextStripper stripper = new PDFTextStripper();  
  128.             pdfDocument = PDDocument.load(fis);  
  129.             StringWriter writer = new StringWriter();  
  130.             stripper.writeText(pdfDocument, writer);  
  131.             content.append(writer.getBuffer().toString());  
  132.             fis.close();  
  133.         } catch (java.io.IOException e) {  
  134.             System.err.println("IOException=" + e);  
  135.             System.exit(1);  
  136.         } finally {  
  137.             if (pdfDocument != null) {  
  138.                 org.pdfbox.cos.COSDocument cos = pdfDocument.getDocument();  
  139.                 cos.close();  
  140.                 pdfDocument.close();  
  141.             }  
  142.         }  
  143.           
  144.         return content.toString();  
  145.   
  146.     }  
  147.     /** 
  148.      * 处理txt 
  149.      * @param path 
  150.      * @return 
  151.      * @throws IOException 
  152.      */  
  153.     public static String readTxt(String path) throws IOException {  
  154.         StringBuffer sb = new StringBuffer("");  
  155.         InputStream is = new FileInputStream(path);  
  156.         // 必须设置成GBK,否则将出现乱码  
  157.         BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));  
  158.         try {  
  159.             String line = "";  
  160.             while ((line = reader.readLine()) != null) {  
  161.                 sb.append(line + "\r");  
  162.             }  
  163.         } catch (FileNotFoundException e) {  
  164.             e.printStackTrace();  
  165.         }  
  166.         return sb.toString().trim();  
  167.     }  
  168.       
  169. }  



使用jar包如下: 

猜你喜欢

转载自xiaoming9920.iteye.com/blog/2304640