///////////////////测试导出word
public void testWrite() throws Exception {
try {
String templatePath = "D:\innerPlatform\addition\format\temp.doc";
InputStream is = new FileInputStream(templatePath);
HWPFDocument doc = new HWPFDocument(is);
Range range = doc.getRange();
//把range范围内的${reportDate}替换为当前的日期
range.replaceText("${reportDate}", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
range.replaceText("${appleAmt}", "100.00");
range.replaceText("${bananaAmt}", "200.00");
range.replaceText("${totalAmt}", "300.00");
OutputStream os = new FileOutputStream("D:\word\writexx.doc");
//把doc输出到输出流中
doc.write(os);
this.closeStream(os);
this.closeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭输入流
* @param is
*/
private void closeStream(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
* @param os
*/
private void closeStream(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}