我目前正在使用Docx4Java 3.1.0。我的所有依赖项都包含在我的netbeans项目中。
当我运行这段代码时:
package pse;
/**
*
* @author Hrach
*/
import java.util.HashMap;
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Document;
public class VariableReplace {
public static void main(String[] args) throws Exception {
// Exclude context init from timing
org.docx4j.wml.ObjectFactory foo = Context.getWmlObjectFactory();
// Input docx has variables in it: ${colour}, ${icecream}
String inputfilepath = System.getProperty("user.dir") + "/documents/in.docx";
boolean save = false;
String outputfilepath = System.getProperty("user.dir")
+ "/rd/OUT_VariableReplace.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("colour", "green");
mappings.put("icecream", "chocolate");
long start = System.currentTimeMillis();
// Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):
documentPart.variableReplace(mappings);
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("Time: " + total);
// Save it
if (save) {
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
} else {
System.out.println(XmlUtils.marshaltoString(documentPart.getJaxbElement(), true,
true));
}
}
}
我得到以下输出:
run:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Time: 9
<w:document mc:Ignorable="w14 w15 wp14" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:odx="http://opendope.org/xpaths" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:odi="http://opendope.org/components" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:ns9="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:odq="http://opendope.org/questions" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:odc="http://opendope.org/conditions" xmlns:oda="http://opendope.org/answers">
<w:body>
<w:p w:rsidRPr="00B959F9" w:rsidR="00D133F0" w:rsidP="00B959F9" w:rsidRDefault="00B959F9">
<w:r w:rsidRPr="00B959F9">
<w:t></w:t>
</w:r>
<w:r>
<w:t>colour</w:t>
</w:r>
<w:r w:rsidRPr="00B959F9">
<w:t></w:t>
</w:r>
<w:r>
<w:t xml:space="preserve">, </w:t>
</w:r>
<w:r w:rsidRPr="00B959F9">
<w:t></w:t>
</w:r>
<w:r>
<w:t>icecream</w:t>
</w:r>
<w:bookmarkStart w:name="_GoBack" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
<w:r w:rsidRPr="00B959F9">
<w:t></w:t>
</w:r>
</w:p>
<w:sectPr w:rsidRPr="00B959F9" w:rsidR="00D133F0">
<w:pgSz w:w="11906" w:h="16838"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>
BUILD SUCCESSFUL (total time: 10 seconds)
用户目录是:C:\Users\Hrach\Desktop\Programming\JAVA Projects\PSE
这是文档文件夹的位置:C:\Users\Hrach\Desktop\Programming\JAVA Projects\PSE\documents
这是rd文件夹的位置:C:\Users\Hrach\Desktop\Programming\JAVA Projects\PSE\rd
我制作了一个简单的docx文件,其中只包含了这些变量:${colour}, ${icecream}
当我运行此文件时,我希望在目录rd中生成一个输出文件,并相应地替换这些变量。 出于某种原因,我无法让这个工作,可能是因为Microsoft Word正在拆分这些词?
请提前帮助和谢谢, Hrach。
答案 0 :(得分:0)
只需将&#39; save&#39;标记为true即可。默认情况下,示例将其包含为false。
boolean save = false;
更改为:
boolean save = true;