2013-07-12 1 views
7

나는 의사와 DOCX 파일을 읽을하려고와 자바 문서 및 DOCX 파일을 읽는 방법. 나는 DOCX 파일을 읽고 싶을 때POI API를

static String distination="E:\\   
    static String docFileName="Requirements.docx"; 
public static void main(String[] args) throws FileNotFoundException, IOException { 
    // TODO code application logic here 
    ReadFile rf= new ReadFile(); 
    rf.ReadFileParagraph(distination+docFileName); 


    } 
    public void ReadFileParagraph(String path) throws FileNotFoundException, IOException 
    { 
     FileInputStream fis; 
     File file = new File(path); 
     fis=new FileInputStream(file.getAbsolutePath()); 
      String filename=file.getName(); 

     String fileExtension=fileExtension(path); 
     if(fileExtension.equals("doc")) 
     { 
      HWPFDocument document=new HWPFDocument(fis); 
      WordExtractor DocExtractor = new WordExtractor(document); 
      ReadDocFile(DocExtractor,filename); 

     } 
     else if(fileExtension.equals("docx")) 
     { 

      XWPFDocument documentX = new XWPFDocument(fis);    
      List<XWPFParagraph> pera =documentX.getParagraphs(); 
      ReadDocXFile(pera,filename); 
     } 
     else 
     { 
      System.out.println("format does not match"); 
     } 

    } 
    public void ReadDocFile(WordExtractor extractor,String filename) 
    { 

     for (String paragraph : extractor.getParagraphText()) { 
      System.out.println("Peragraph: "+paragraph); 
     } 
    } 
    public void ReadDocXFile(List<XWPFParagraph> extractor,String filename) 
    { 

     for (XWPFParagraph paragraph : extractor) { 
      System.out.println("Question: "+paragraph.getParagraphText()); 
     } 

    } 
    public String fileExtension(String filename) 
    { 

     String extension = filename.substring(filename.lastIndexOf(".") + 1, filename.length()); 
     return extension; 
    } 

이 코드가 예외를 제공합니다 : 나는 문서 파일을 읽고 싶을 때

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException 
    at l3s.readfiles.db.ReadFile.ReadFileParagraph(ReadFile.java:52) 
    at autometictagdetection.TagDetection.main(TagDetection.java:36) 
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlException 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    ... 2 more 
Java Result: 1 

또 다른 문제가 그것을, 여기에 코드입니다 어떤 파일을 잘 읽었지 만 일부 파일의 경우 예외가 있습니다

Exception in thread "main" org.apache.poi.hwpf.OldWordFileFormatException: The    document is too old - Word 95 or older. Try HWPFOldDocument instead? 
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:222) 
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:186) 
    at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174) 
    at l3s.readfiles.db.ReadFile.ReadFileParagraph(ReadFile.java:44) 
    at autometictagdetection.TagDetection.main(TagDetection.java:36) 
Java Result: 1 

POI AP http://poi.apache.org/hwpf/index.html에서 단어 6과 단어 95를 지원합니다. 누구든지이 두 가지 문제를 해결할 수 있습니까?

+0

거의 두 번째는 무엇이 잘못되었는지를 알려줍니다 ...

내가 필요한 모든 라이브러리를 나열 할 수 아니지만, 당신은 아마 받는다는 통해 알아낼 수 있습니다. 하지 말자 관심 장소 API를 알고 있지만 그것은 단지 HWPFDocument 단어 95보다 최신 Word 문서를 읽을 수 있으며, 대신 코드에서 HWPFOldDOcument를 사용해야합니다. –

+0

나는 그걸 시도했다. 그러나 내가 이것을 할 때 HWPFOldDocument document = new HWPFOldDocument (fis); 그것은 "HWPFOldDocument에 적합한 컨스트럭터가 없습니다"라고 말합니다. 또한 HWPFOldDocument에 대한 문서를 찾지 못했습니다. 내가 HWPFOldDocument 봤 때 – Khaled

+0

첫 번째 결과가 팝업 : https://poi.apache.org/apidocs/org/apache/poi/hwpf/HWPFOldDocument.html 당신이 아마 아파치 XML 빈스 jar 파일을 필요로하는 첫 번째 예외 생각 –

답변

0

첫 번째 문제를 다시, 나는 당신이 당신의 프로젝트에 depencencies를 참조해야 할 것 같아요.

즉 I 추측 : POI-OOXML-스키마 - 버전 yyyymmdd.jar합니다 ( Apache POI page)에서

POI-OOXML-스키마의 XML 빈스.

Here

는 아파치 XML 빈스 페이지입니다.