public static void main(String[] args) {
String tempPath = "C:\\Users\\d\\Desktop\\temp.doc";
String newFilePath = "C:\\Users\\d\\Desktop\\111-new.doc";
dealDoc(tempPath, newFilePath, dataList);
}
public static boolean dealDoc(String templateFilePath, String newFilePath){
try {
Document doc = new Document(templateFilePath);
DocumentBuilder builder = new DocumentBuilder(doc);
StyleCollection styles = doc.getStyles();
Style heading1 = styles.getByStyleIdentifier(StyleIdentifier.HEADING_1);//获取标题1
builder.moveToBookmark("OLE_LINK15");//移动到书签位置
Paragraph titlePara = builder.insertParagraph();
titlePara.getParagraphFormat().setStyle(heading1);
// 将标题内容添加到段落中
titlePara.appendChild(new Run(doc, "这是标题一"));
doc.updateFields();//更新目录
doc.save(newFilePath);
File file = new File(newFilePath);
return file.exists();
} catch (Exception e) {
System.out.println("error:"+e);
e.printStackTrace();
return false;
}
}