2013-07-12 26 views
7

Próbuję odczytać pliki doc i docx. Oto kod:Jak czytać dokument i plik docx w java z api POI

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; 
    } 

ten kod dać wyjątek, gdy chcę odczytać plik docx:

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 

Innym problemem kiedy chcę odczytać plik doc, to odczytuje plik bardzo dobrze, ale dla jakiegoś pliku podaje wyjątek:

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 

Widziałem to POI AP Popieram słowo 6 i słowo 95 w http://poi.apache.org/hwpf/index.html. Czy ktoś może podać rozwiązanie tych dwóch problemów?

+0

Drugi dość dużo mówi, co jest nie tak. Nie znasz interfejsu API POI, ale możesz tylko czytać dokumenty Word nowsze niż Word 95 z HWPFDocument i powinieneś używać HWPFOldDOcument w swoim kodzie. –

+0

Próbowałem z tym. Ale kiedy robię ten dokument HWPFOldDocument = new HWPFOldDocument (fis); jest napisane "brak odpowiedniego konstruktora dla HWPFOldDocument". Nie znalazłem też żadnego dokumentu o HWPFOldDocument. – Khaled

+0

Pierwszy wynik pojawił się, gdy robiłem Google HWPFOldDocument: https://poi.apache.org/apidocs/org/apache/poi/hwpf/HWPFOldDocument.html –

Odpowiedz

0

Powróć do pierwszego numeru, myślę, że musisz odwołać się do zależności w projekcie.

Mianowicie Chyba: XMLBeans

poi-OOXML-Schematy, które jest w POI-OOXML-schematów-version-yyyymmdd.jar

(od Apache POI page).

Here to strona Apache XMLBeans.

nie jestem w stanie wymienić wszystkich biblioteka jest potrzebna, ale prawdopodobnie można dowiedzieć się poprzez Maven ...