Java uses POI to read and write word files

Java uses POI to read and write word files

The operation of reading and writing word documents is a problem that most developers will encounter. Apache's POI is one of the solutions for word reading and writing. This article shows how to read and write files through HWPFDocument.

First, read the word content

 

public class HwpfTest {  
    public static void hwpfRead() throws IOException {
        File file = new File("C:\\Users\\Desktop\\test.doc");
        String doc1 = "";
        String doc2 = "";
        try {
            FileInputStream is= new FileInputStream(file);
            HWPFDocument doc = new HWPFDocument(is);
            String doc1 = doc.getDocumentText();
            System.out.println(doc1);
            Range rank = doc.getRange();
            String doc2 = rang.text();
            System.out.println(doc2);
            this.closeStream(is);
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
 /**
    * close the input stream
    * @param is
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace ();  
         }  
      }  
   }
}

 Second, write content to the word file

      When using POI to write word doc files, we must first have a doc file, because we write doc files through HWPFDocument , and HWPFDocument is attached to a doc file.

      In actual development, when we generate word files, we always generate a certain type of file. The format of this type of file is fixed, but some fields are different. So in practical applications, we don't need to generate the content of the entire word file through HWPFDocument . Instead, first create a new word document on the disk , the content of which is the content of the word file we need to generate , and then replace some of the content belonging to variables in a method similar to " ${paramName} ". In this way, when we generate a word file based on some information, we only need to obtain the HWPFDocument based on the word file , and then call the replaceText() method of Range to replace the corresponding variable with the corresponding value, and then write the current HWPFDocument . into a new output stream.

public class HwpfWriteTest {  
    
   public void testWrite() throws Exception {  
      String templatePath = "C:\\User\\Desktop\\template.doc";  
      InputStream is = new FileInputStream(templatePath);  
      HWPFDocument doc = new HWPFDocument(is);  
      Range range = doc.getRange();  
      //Replace "${helloword}" in the document with "Hi"
      range.replaceText("${helloword}", "Hi");  
      OutputStream os = new FileOutputStream("C:\\User\\Desktop\\write.doc");  
      // output the doc to the output stream  
      doc.write(os);  
      this.closeStream(os);  
      this.closeStream(is);  
   }  
    
   /**
    * close the input stream
    * @param is
    */  
   private void closeStream(InputStream is) {  
      if (is != null) {  
         try {  
            is.close();  
         } catch (IOException e) {  
            e.printStackTrace ();  
         }  
      }  
   }  
   
   /**
    * close the output stream
    * @param os
    */  
   private void closeStream(OutputStream os) {  
      if (os != null) {  
         try {  
            os.close();  
         } catch (IOException e) {  
            e.printStackTrace ();  
         }  
      }  
   }  
    
   
}  

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326887569&siteId=291194637