我最近一直在制作一个自动系统来制作和打印信件。系统的工作原理如下:
我在互联网上找到了一些教程,并找到了符合我需求的代码。不幸的是,此代码仅适用于早于2007的Word版本(.doc文件)。对于2007+兼容性(.docx文件),我会改变什么?
public static void main(String[] args){
try{
FileInputStream fis = new FileInputStream("/Users/Jasper/Desktop/document.doc");
POIFSFileSystem fs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
range.replaceText("%name", "Jasper");
range.replaceText("%age", "17");
FileOutputStream fos = new FileOutputStream("/Users/Jasper/Desktop/document2.doc");
doc.write(fos);
fis.close();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}