2015-08-13 303 views
2

我正在嘗試讀取一個word文檔模板,然後用模板替換變量,給用戶提供data.without不改變標題或風格,因爲在tempate上。我不確定那是什麼我做的是正確的方法或沒有,但這個是我開始的方式:使用Apache POI編輯Word文檔

'XWPFDocument docx = new XWPFDocument(
      new FileInputStream(
        "D://TestDocumentPrep/src/XXXXX_TestReport_URL_Document.docx")); 
    XWPFWordExtractor we = new XWPFWordExtractor(docx); 
    String textData = we.getText(); 
    String newTestData=textData.replace("$var_source_code$", list.get(1)) 
      .replace("$var_rsvp_code$", list.get(2)) 
      .replace("$var_ssn$", list.get(3)) 
      .replace("$var_zip_code$", list.get(4)) 
      .replace("$var_point_for_business$", 
        anotherData.getPointForBusiness()) 
      .replace("$var_E1_url$", anotherData.getE1url()) 
      .replace("$var_E2_url$", anotherData.getE2url()) 
      .replace("$var_E3_url$", anotherData.getE3url()); 
    System.out.println(newTestData);' 

這是我done.But它讀取Word文檔作爲一個字符串的內容和更換variables.Now如何將替換的字符串放在Word文檔中的模板格式中?

Here I found something but Not exactly my solution

Here also I found something but not exact solution

+1

請看看這個[鏈接](http://stackoverflow.com/questions/22268898/replacing-a-text-in-apache-poi-xwpf) – MrT

+0

@MrT這對錶數據但我想要在模板中使用相同的顏色和所有格式。 –

+0

爲什麼不提取每個段落,然後每次運行,然後替換運行中的文本?如果您在更換前提取文本,您將失去所有格式化... – Gagravarr

回答

2

嗨,我能夠找到解決方案

這裏是我用於編輯我的word文檔的代碼和它的罰款既doc和docx格式我想要編輯的文件並生成編輯後的新文檔文檔而不更改基本模板。

public void wordDocProcessor(AnotherVO anotherData, ArrayList<String> list, 
     String sourse, String destination) throws IOException, 
     InvalidFormatException { 
    XWPFDocument doc = new XWPFDocument(OPCPackage.open(sourse 
      + "XXXXX_TestReport_URL_Document.doc")); 
    for (XWPFTable tbl : doc.getTables()) { 
     for (XWPFTableRow row : tbl.getRows()) { 
      for (XWPFTableCell cell : row.getTableCells()) { 
       for (XWPFParagraph p : cell.getParagraphs()) { 
        for (XWPFRun r : p.getRuns()) { 
         String text = r.getText(0); 
         if (text != null 
           && text.contains("var_source_code")) { 
          text = text.replace("var_source_code", 
            list.get(1)); 
          r.setText(text, 0); 
         } 
         if (text != null && text.contains("var_rsvp_code")) { 
          text = text.replace("var_rsvp_code", 
            list.get(2)); 
          r.setText(text, 0); 
         } 
         if (text != null && text.contains("var_ssn")) { 
          text = text.replace("var_ssn", list.get(3)); 
          r.setText(text, 0); 
         } 
         if (text != null && text.contains("var_zip_code")) { 
          text = text 
            .replace("var_zip_code", list.get(4)); 
          r.setText(text, 0); 
         } 
         if (text != null 
           && text.contains("var_point_for_business")) { 
          text = text.replace("var_point_for_business", 
            anotherData.getPointForBusiness()); 
          r.setText(text, 0); 
         } 
         if (text != null && text.contains("var_E1_url")) { 
          text = text.replace("var_E1_url", 
            anotherData.getE1url()); 
          r.setText(text, 0); 
         } 
         if (text != null && text.contains("var_E2_url")) { 
          text = text.replace("var_E2_url", 
            anotherData.getE2url()); 
          r.setText(text, 0); 
         } 
         if (text != null && text.contains("var_E3_url")) { 
          text = text.replace("var_E3_url", 
            anotherData.getE3url()); 
          r.setText(text, 0); 
         } 
        } 
       } 
      } 
     } 
    } 
    doc.write(new FileOutputStream(destination + list.get(0) 
      + "_TestReport_URL_Document.doc")); 
} 
相關問題