POI3.8查询office

阅读更多
/**
* 读取word2003
*/
public String readExtDocContent(String fileName){
// 创建输入流读取DOC文件
InputStream  in = null;
    WordExtractor extractor = null;
    String text = null;
try {
in = new FileInputStream(new File(fileName));
extractor = new WordExtractor(in);
text = extractor.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return text;
}

/**
* 读取word2007-2013
*/
public String readExtDocxContent(String fileName){
// 创建输入流读取DOC文件
    String text = null;
    OPCPackage opcPackage = null;
try {
opcPackage = POIXMLDocument.openPackage(fileName);
            XWPFDocument xwpf = new XWPFDocument(opcPackage);
            POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
text = ex.getText();
} catch (Exception e) {
e.printStackTrace();
log.error("转换word异常:" + e);
} finally{
try {
if(opcPackage != null){
opcPackage.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return text;
}

/**
* 读取Excel2003
*/
public String readExtXlsContent(String fileName){
String content = "";
try {
   HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileName));
   ExcelExtractor extractor = new ExcelExtractor(workbook);
   extractor.setFormulasNotResults(true);
   extractor.setIncludeSheetNames(false);
   content =  extractor.getText();
   log.debug("content:" + content);
  } catch (Exception e) {
   e.printStackTrace();
   return "";
  }
return content;
}

/**
* 读取Excel2007+
*/
public String readExtXlsxContent(String fileName){
String content = "";
try {
   XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileName));
   XSSFExcelExtractor extractor = new XSSFExcelExtractor(workbook);
   extractor.setFormulasNotResults(true);
   extractor.setIncludeSheetNames(false);
   content =  extractor.getText();
  } catch (Exception e) {
   e.printStackTrace();
   return "";
  }

return content;
}

/**
* 读取ppt2003
*/
public String readExtPptContent(String fileName){
String text = "";
  try {
   SlideShow ss = new SlideShow(new HSLFSlideShow(new FileInputStream(fileName)));
   Slide[] slides = ss.getSlides();
   for (int i = 0; i < slides.length; i++) {
    TextRun[] t = slides[i].getTextRuns();
    for (int j = 0; j < t.length; j++) {
     text += t[j].getText();
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return text;
}

/**
* 读取ppt2007+
*/
public String readExtPptxContent(String fileName){
String text = "";
XMLSlideShow xmlslideshow = null;
try {
xmlslideshow = new XMLSlideShow(new FileInputStream(fileName));
XSLFPowerPointExtractor ppt = new XSLFPowerPointExtractor(xmlslideshow); 
text= ppt.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return text;
}

/**
* 读取pdf
*/
public String readExtPDFContent(String fileName){
String text = "";
try {
PDFParser parser = new PDFParser(new FileInputStream(fileName));
parser.parse();
PDFTextStripper stripper = new PDFTextStripper();
text = stripper.getText(parser.getPDDocument());
parser.getPDDocument().close();
} catch (Exception e) {
e.printStackTrace();
return "";
}
  return text;
}
  • poi3.8.rar (9.3 MB)
  • 下载次数: 12

你可能感兴趣的:(POI,office,微软,PDF)