2015-07-13 38 views
1

我試圖替換文件頭和表中的變量,但我不知道如何繼續。我設法替換文檔正文中的變量,但此方法(使用$ {})不適用於標題和表格。如何用docx4j替換標題和表格中的變量?

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.HashMap; 
import java.util.List; 

import org.docx4j.XmlUtils; 
import org.docx4j.customxml.ObjectFactory; 
import org.docx4j.dml.wordprocessingDrawing.Inline; 
import org.docx4j.jaxb.Context; 
import org.docx4j.model.datastorage.migration.VariablePrepare; 
import org.docx4j.model.structure.HeaderFooterPolicy; 
import org.docx4j.model.structure.SectionWrapper; 
import org.docx4j.openpackaging.exceptions.Docx4JException; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePart; 
import org.docx4j.openpackaging.parts.Part; 
import org.docx4j.openpackaging.parts.PartName; 
import org.docx4j.openpackaging.parts.Parts; 
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; 
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; 
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; 
import org.docx4j.utils.BufferUtil; 
import org.docx4j.wml.Hdr; 
import org.docx4j.wml.HdrFtrRef; 
import org.docx4j.wml.HeaderReference; 

import java.util.Locale; 

import javax.xml.bind.JAXBElement; 

import java.text.DateFormat; 
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; 
import org.docx4j.wml.HdrFtrRef; 

public class EditInvoice { 

    private static WordprocessingMLPackage template; 
    private static ObjectFactory factory; 

    public static void main (String[] args) throws Exception { 

     boolean save = true; 
     String outputfilepath = System.getProperty("user.dir")+ "/InvoiceEdited.docx"; 

     java.util.Date uDate = new java.util.Date(); 
     java.sql.Date sDate = new java.sql.Date(System.currentTimeMillis()); 
     sDate = new java.sql.Date(uDate.getTime()); 
     uDate = new java.util.Date(sDate.getTime()); 
     Locale locale = Locale.getDefault(); 
     DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale); 

     //System.out.println(dateFormat.format(sDate)); 


     template = WordprocessingMLPackage.load(new FileInputStream(new File("invoice_template_sample.docx"))); 
     VariablePrepare.prepare(template); 

     List<SectionWrapper> sectionWrappers = template.getDocumentModel().getSections(); 

     MainDocumentPart documentPart = template.getMainDocumentPart(); 

     HashMap<String, String> variables = new HashMap<String, String>(); 

     // populate doc variables 
     variables.put("Name", "John Doe"); 
     variables.put("Phone", "(123) 456 78 90"); 
     variables.put("CompanyName", "BSI Business Systems Integration AG"); 
     variables.put("Email", "[email protected]"); 
     variables.put("CompanyAddress", "Täfernstrasse 16a, 5405 Baden"); 
     variables.put("InvoiceNo", "No. 2013-007"); 
     variables.put("InvoiceDate", dateFormat.format(sDate)); 
     variables.put("BillingName", "Jane Smith"); 
     variables.put("PayableToName", "John Doe, BSI"); 
     variables.put("SubTotal", "$1,530.00"); 
     variables.put("SalesTax", "$229.50"); 
     variables.put("Shipping", "$250.00"); 
     variables.put("Total", "$2,009.50"); 

     // and content for embedded table 
     Object[][] orderItems = new Object[][]{ 
      new Object[]{"1", "Table", "$800.00", "$800.00"}, 
      new Object[]{"4", "Chair", "$150.00", "$600.00"}, 
      new Object[]{"1", "Assembling", "$130.00", "$130.00"}, 
     }; 


     try 
     { 

      documentPart.variableReplace(variables); 
      //documentPart.addObject(orderItems); 

     } 
     catch (Exception e) 
     { 
      System.out.println(e); 
     } 


     if (save) { 
      template.save(new java.io.File(outputfilepath)); 
     } else { 
      System.out.println(XmlUtils.marshaltoString(documentPart.getContents(), true, true)); 
     } 


    } 
} 
+0

發佈您的代碼?沒有理由不能在標題和表格中工作。 – JasonPlutext

+0

我加了我的代碼。 – Lola

+0

'docx'是contents.xml的zip文件。使用'jar:file:// ...'URL可以使用java中的zip文件系統來簡單地替換新的content.xml。 –

回答

0

要替換標題中的變量,您需要對相關標題部分進行變量替換。在這裏,你只在主文檔部分做這件事。

關於表格,變量替換內容並非設計爲重複行(例如,每個發票行項目一行)。換句話說,它不會插入行。所以如果沒有更多的代碼,你的Object [] [] orderItems將不會執行任何操作。

(相比之下,docx4j的XML數據綁定沒有搞定,使用OpenDoPE OD:重複)