我想使用dot / dotx和doc / docx格式文档在java中执行邮件合并功能。我尝试使用docx4j,但它从文档中删除了很多富文本缩进。
我也尝试从word文档中取出一些html内容,但无法在word文档中重新播放。
public static void readDocxFile1(String fileName) {
// this.file = file;
try {
File file = new File(fileName);
FileInputStream finStream=new FileInputStream(file.getAbsolutePath());
HWPFDocument doc=new HWPFDocument(finStream);
WordExtractor wordExtract=new WordExtractor(doc);
Document newDocument = DocumentBuilderFactory.newInstance() .newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument) ;
wordToHtmlConverter.processDocument(doc);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform(new DOMSource( wordToHtmlConverter.getDocument()), new StreamResult( stringWriter ) );
String html = stringWriter.toString();
System.out.println("html>>>>>>"+html);
}
catch(Exception e)
{
e.printStackTrace();
}
}
我的要求是我必须(1)阅读dot / dotx或doc / docx模板并且不需要。人们将其循环到(2)替换关键字然后(3)在新文档中重新制作。
请建议一种方法如何执行此功能。
另外请建议,如果JAVA的ASPOSE.WORDS API将为我这样做。