关于POI提取word中文本,除掉页眉页脚

需要相关七个jar

poi-3.7-20101029.jar

poi-ooxml-3.7-20101029.jar

poi-ooxml-schemas-3.7-20101029.jar

poi-scratchpad-3.7-20101029.jar

dom4j-1.6.1.jar

geronimo-stax-api_1.0_spec-1.0.jar

xmlbeans-2.3.0.jar

 

public static String wordExtractor(String fileName){
  try{
  InputStream in = new FileInputStream(fileName);
  String header,footer,allText;
  int end;
  if(getSuffix(fileName).equals("doc")){
  WordExtractor wordExtractor = new WordExtractor(in);
  header = wordExtractor.getHeaderText();
  footer = wordExtractor.getFooterText();
  allText = wordExtractor.getText();
  end = allText.indexOf(footer);
  return allText.substring(header.length(), end);
  }else if(getSuffix(fileName).equals("docx")){
   OPCPackage opcPackage =  POIXMLDocument.openPackage(fileName);   
   POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);         
   return ex.getText();
  }else{
   return null;
  }
  }catch(IOException e){
   e.printStackTrace();
   return null;
  } catch (XmlException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null;
  } catch (OpenXML4JException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null; 
  }
 }

 

看到网上大部分也就是这个例子,利用getText()获取文本信息,但是这个信息中都包括了页眉和页脚吧,

如何处理能够除掉这些页眉页脚?

上面的代码只是能够除掉word2003中的页眉和页脚,不知道word2007如何处理

你可能感兴趣的:(word)