控制层
public void query() {
String str=request.getParameter("dId");
String docPath = request.getServletContext().getRealPath("/") + "template";
Recode recode=recodeService.get(Long.parseLong(str));
String oFile=recodeService.createWord(recode,docPath);
//String str1=recodeService.word2Pdf(docPath+"/test-record.doc");
String str1=recodeService.word2Pdf(oFile);
try {
fileOnlinePreview(response, new File(str1), "application/pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
private void fileOnlinePreview(HttpServletResponse response, File file, String contentType) throws Exception {
if (file == null) {
return;
}
response.reset();
// 设置响应内容类型为PDF类型
response.setContentType(contentType);
response.setContentLength((int) file.length());
OutputStream os = response.getOutputStream();
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[1024 * 1024];
int readBytes = -1;
while ((readBytes = fis.read(buffer, 0, 1024 * 1024)) != -1) {
os.write(buffer, 0, readBytes);
}
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
fis.close();
fis = null;
}
if (os != null) {
os.close();
os = null;
response.flushBuffer();
}
}
}
public String createWord(Recode recode ,String docPath){
Configuration configuration = null;
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
Map<String,Object> dataMap=new HashMap<String,Object>();
/*getData(dataMap); */
//SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String[] ss=recode.getJcstartdate().split("-");
// for(int i=0;i<ss.length;i++){
dataMap.put("year", ss[0]);
dataMap.put("month", ss[1]);
dataMap.put("day1", ss[2]);
dataMap.put("hour1", ss[3]);
dataMap.put("min1", ss[4]);
//}
String[] ss1=recode.getJcenddate().split("-");
//for(int n=0;n<ss1.length;n++){
dataMap.put("day2", ss1[2]);
dataMap.put("hour2", ss1[3]);
dataMap.put("min2", ss1[4]);
// }
dataMap.put("spF250", recode.getSpF250());
if(StringUtil.isNotEmpty(recode.getName())){
dataMap.put("name", recode.getName());
}else{
dataMap.put("name", "");
}
if(StringUtil.isNotEmpty(recode.getZhiwu())){
dataMap.put("zhiwu", recode.getZhiwu());
}else{
dataMap.put("zhiwu", "");
}
if(StringUtil.isNotEmpty(recode.getComaddress())){
dataMap.put("comaddress", recode.getComaddress());
}else{
dataMap.put("comaddress", "");
}
if(StringUtil.isNotEmpty(recode.getZhifarenname1())){
dataMap.put("zhifarenname1", recode.getZhifarenname1());
}else{
dataMap.put("zhifarenname1", "");
}
if(StringUtil.isNotEmpty(recode.getZhifarenname2())){
dataMap.put("zhifarenname2", recode.getZhifarenname2());
}else{
dataMap.put("zhifarenname2", "");
}
if(StringUtil.isNotEmpty(recode.getZhengjianhaoma1())){
dataMap.put("zhengjianhaoma1", recode.getZhengjianhaoma1());
}else{
dataMap.put("zhengjianhaoma1", "");
}
if(StringUtil.isNotEmpty(recode.getZhengjianhaoma2())){
dataMap.put("zhengjianhaoma2", recode.getZhengjianhaoma2());
}else{
dataMap.put("zhengjianhaoma2", "");
}
if(StringUtil.isNotEmpty(recode.getJcneirong())){
dataMap.put("jcneirong", recode.getJcneirong());
}else{
dataMap.put("jcneirong", "");
}
if(StringUtil.isNotEmpty(recode.getPhone())){
dataMap.put("phone", recode.getPhone());
}else{
dataMap.put("phone", "");
}
if(StringUtil.isNotEmpty(recode.getChangsuo())){
dataMap.put("changsuo", recode.getChangsuo());
}else{
dataMap.put("changsuo", "");
}
if(StringUtil.isNotEmpty(recode.getZfrydw())){
dataMap.put("zfrydw", recode.getZfrydw());
}else{
dataMap.put("zfrydw", "");
}
try {
configuration.setDirectoryForTemplateLoading(new File(docPath));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 输出文档路径及名称
String str1=docPath+"\\"+Math.random()*1000+"recode.doc";
// String str1="D:\\test"+"\\"+Math.random()*1000+".doc";
File outFile = new File(str1);
//以utf-8的编码读取ftl文件
Template t = null;
try {
t = configuration.getTemplate("recode.xml","utf-8");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str1;
}
public String word2Pdf(String sourceFile){
final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
final int wdFormatPDF = 17;// PDF 格式
String toFileName = sourceFile.substring(0, sourceFile.length()-4) + ".pdf";
System.out.println("启动Word...");
long start = System.currentTimeMillis();
ActiveXComponent app = null;
try {
app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", false);
Dispatch docs = app.getProperty("Documents").toDispatch();
System.out.println("打开文档..." + sourceFile);
Dispatch doc = Dispatch.invoke(
docs,
"Open",
Dispatch.Method,
new Object[] { sourceFile, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
System.out.println("转换文档到PDF..." + toFileName);
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.invoke(doc,
"SaveAs",
Dispatch.Method,
new Object[] {toFileName,new Variant(17)},
new int[1]);
Dispatch.call(doc, "Close", false);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" );
} finally {
if (app != null)
app.invoke("Quit", wdDoNotSaveChanges);
}
ComThread.Release();
return toFileName;
}
word模板转为.xml结尾,变量用${zhengjianhaoma1}