2011-08-18 6 views
15

मैं जावा में एक शब्द फ़ाइल को पढ़ने के लिएजावा में डॉक्टर या डॉक्स फ़ाइल कैसे पढ़ी जाती है?

import org.apache.poi.poifs.filesystem.*; 
import org.apache.poi.hpsf.DocumentSummaryInformation; 
import org.apache.poi.hwpf.*; 
import org.apache.poi.hwpf.extractor.*; 
import org.apache.poi.hwpf.usermodel.HeaderStories; 

import java.io.*; 

public class ReadDocFileFromJava { 

    public static void main(String[] args) { 
     /**This is the document that you want to read using Java.**/ 
     String fileName = "C:\\Path to file\\Test.doc"; 

     /**Method call to read the document (demonstrate some useage of POI)**/ 
     readMyDocument(fileName); 

    } 
    public static void readMyDocument(String fileName){ 
     POIFSFileSystem fs = null; 
     try { 
      fs = new POIFSFileSystem(new FileInputStream(fileName)); 
      HWPFDocument doc = new HWPFDocument(fs); 

      /** Read the content **/ 
      readParagraphs(doc); 

      int pageNumber=1; 

      /** We will try reading the header for page 1**/ 
      readHeader(doc, pageNumber); 

      /** Let's try reading the footer for page 1**/ 
      readFooter(doc, pageNumber); 

      /** Read the document summary**/ 
      readDocumentSummary(doc); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void readParagraphs(HWPFDocument doc) throws Exception{ 
     WordExtractor we = new WordExtractor(doc); 

     /**Get the total number of paragraphs**/ 
     String[] paragraphs = we.getParagraphText(); 
     System.out.println("Total Paragraphs: "+paragraphs.length); 

     for (int i = 0; i < paragraphs.length; i++) { 

      System.out.println("Length of paragraph "+(i +1)+": "+ paragraphs[i].length()); 
      System.out.println(paragraphs[i].toString()); 

     } 

    } 

    public static void readHeader(HWPFDocument doc, int pageNumber){ 
     HeaderStories headerStore = new HeaderStories(doc); 
     String header = headerStore.getHeader(pageNumber); 
     System.out.println("Header Is: "+header); 

    } 

    public static void readFooter(HWPFDocument doc, int pageNumber){ 
     HeaderStories headerStore = new HeaderStories(doc); 
     String footer = headerStore.getFooter(pageNumber); 
     System.out.println("Footer Is: "+footer); 

    } 

    public static void readDocumentSummary(HWPFDocument doc) { 
     DocumentSummaryInformation summaryInfo=doc.getDocumentSummaryInformation(); 
     String category = summaryInfo.getCategory(); 
     String company = summaryInfo.getCompany(); 
     int lineCount=summaryInfo.getLineCount(); 
     int sectionCount=summaryInfo.getSectionCount(); 
     int slideCount=summaryInfo.getSlideCount(); 


    enter code here 
     System.out.println("---------------------------"); 
     System.out.println("Category: "+category); 
     System.out.println("Company: "+company); 
     System.out.println("Line Count: "+lineCount); 
     System.out.println("Section Count: "+sectionCount); 
     System.out.println("Slide Count: "+slideCount); 

    } 

} 

http://sanjaal.com/java/tag/java-and-docx-format/

मैं जावा

+1

आपने वास्तव में यहां एक प्रश्न नहीं पूछा है। बिना विस्तार के, इस सवाल को बंद होने की संभावना है। आपका लक्ष्य क्या है (देखना, प्रसंस्करण, संपादन, प्रिंटिंग)? अब तक तुमने क्या प्रयास किये हैं? क्या काम नहीं कर रहा है क्या आपको त्रुटियां मिल रही हैं? – OverZealous

उत्तर

19

यहाँ में दस्तावेज़ या docx फ़ाइल पढ़ना चाहते हैं ReadDoc/docx.java का कोड है चाहता हूँ : यह एक डॉक्स/डॉक्क्स फ़ाइल पढ़ेगा और कंसोल पर अपनी सामग्री प्रिंट करेगा। आप इसे अपने तरीके से अनुकूलित कर सकते हैं।

import java.io.*; 
import org.apache.poi.hwpf.HWPFDocument; 
import org.apache.poi.hwpf.extractor.WordExtractor; 

public class ReadDocFile 
{ 
    public static void main(String[] args) 
    { 
     File file = null; 
     WordExtractor extractor = null; 
     try 
     { 

      file = new File("c:\\New.doc"); 
      FileInputStream fis = new FileInputStream(file.getAbsolutePath()); 
      HWPFDocument document = new HWPFDocument(fis); 
      extractor = new WordExtractor(document); 
      String[] fileData = extractor.getParagraphText(); 
      for (int i = 0; i < fileData.length; i++) 
      { 
       if (fileData[i] != null) 
        System.out.println(fileData[i]); 
      } 
     } 
     catch (Exception exep) 
     { 
      exep.printStackTrace(); 
     } 
    } 
} 
+9

आप उस अपवाद के साथ कुछ करना चाहते हैं। इस मामले में –

+1

शब्द निकालने वाला केवल दस्तावेज़ फ़ाइल का पाठ दे सकता है। यह भी उल्लेख नहीं करता है कि अनुच्छेद कहां शुरू हो रहा है या समाप्त हो रहा है ... – Wolverine

+1

टेक्स्ट (छवि, बार कोड, ..) के अलावा सामग्री के बारे में क्या। क्या आप पूर्ण डेटा पढ़ने के लिए कोड को और संपादित कर सकते हैं? –

संबंधित मुद्दे