情景:
要求导出签批单: 格式如下,
要获取“主办处室负责人”和“相关处室会签”环节的处理意见、处理人员和处理日期进行替换,导出word文档。
处理:
主要是如何拼接内容?
方法一:
导出word,直接拼内容,然后导出。 这样比较麻烦,而且格式控制是一个大问题(主要也不是很熟悉,处理起来就比较麻烦)
方法二:
把内容写到纯html里面,然后直接导出html。这样要修改样式也比较方便。
写html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<title></title>
</head>
<body>
<div style="text-align:center;">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:22pt">签批单</span>
</p>
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse; margin-left:0pt;margin:0 auto;">
<tr style="height:31.85pt">
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt">经办人</span>
</p>
</td>
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:80.2pt">
<p style="margin:0pt; orphans:0; text-align:justify; widows:0">
<span style="font-family:宋体; font-size:14pt"> </span>
</p>
</td>
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:46.75pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt">日期</span>
</p>
</td>
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:139.05pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt"></span>
<span style="font-family:宋体; font-size:14pt">年 </span>
<span style="font-family:宋体; font-size:14pt"></span>
<span style="font-family:宋体; font-size:14pt">月 </span>
<span style="font-family:宋体; font-size:14pt"></span>
<span style="font-family:宋体; font-size:14pt">日</span>
</p>
</td>
</tr>
<tr style="height:228.6pt">
<td colspan="4" style="position:relative; border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:404pt">
<p style="margin:0pt; orphans:0; text-align:justify; widows:0;position: absolute;top:40px;">
<span style="font-family:宋体; font-size:14pt">签批事由:</span>
</p>
<p style="margin:0pt; orphans:0; text-align:right; widows:0;">
</p>
<p style="margin:0pt; orphans:0; text-align:right; widows:0;position: absolute;bottom:40px;right: 10px;">
<span style="font-family:宋体; font-size:14pt;padding-right: 50px;">用</span>
<span style="font-family:宋体; font-size:14pt;padding-right: 30px;">印章</span>
</p>
</td>
</tr>
<tr style="height:102.85pt">
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt">主办处室负责人</span>
</p>
</td>
<td colspan="3" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:287.6pt">
<p style="margin:0pt; orphans:0; text-align:justify; widows:0">
<span style="font-family:宋体; font-size:14pt">处理意见</span>
</p>
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt"> </span>
</p>
<p style="margin:0pt; orphans:0; text-align:right; widows:0;padding-right: 10px;">
<span style="font-family:宋体; font-size:14pt">处理人员 处理日期</span>
</p>
</td>
</tr>
<tr style="height:96.25pt">
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0;">
<span style="font-family:宋体; font-size:14pt">相关处室会签</span>
</p>
</td>
<td colspan="3" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:287.6pt">
<p style="margin:0pt; orphans:0; text-align:justify; widows:0">
<span style="font-family:宋体; font-size:14pt">处理意见</span>
</p>
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt"> </span>
</p>
<p style="margin:0pt; orphans:0; text-align:right; widows:0;padding-right: 10px;">
<span style="font-family:宋体; font-size:14pt">处理人员 处理日期</span>
</p>
</td>
</tr>
<tr style="height:45.65pt">
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt">批准人</span>
</p>
</td>
<td colspan="3" style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:287.6pt">
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt"> </span>
</p>
</td>
</tr>
<tr style="height:0pt">
<td style="width:116.4pt; border:none"></td>
<td style="width:91pt; border:none"></td>
<td style="width:57.55pt; border:none"></td>
<td style="width:149.85pt; border:none"></td>
</tr>
</table>
</div>
</body>
</html>
打开html的效果是想要的;
导出html
《导出html到word》,这边做一个优化,添加response的处理
public static void exportDoc(String html, String fileName, HttpServletResponse response) throws IOException {
setFileName(fileName, response);
exportDoc(html, response.getOutputStream());
}
public static void setFileName(String fileName,HttpServletResponse response) {
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain");
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=" + fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
调用:
// controller
@ResponseBody
@RequestMapping(value = "/exportSignSheet", method = RequestMethod.GET)
@ApiOperation("导出签批单")
public void exportSignSheet(@RequestParam("condition") String condition,HttpServletResponse response) throws IOException {
signService.exportSignSheet(condition, response);
}
// service
public void exportSignSheet(String condition, HttpServletResponse response) throws IOException {
String html = SignApprove.getSignApproveHtml(); // 把内容放到实体里面
// html = html.replace 内容替换
String fileName = "签批单-"+changeDateToStr(new Date())+".doc";
HtmlToWordUtils.exportDoc(html, fileName, response);
}
private String changeDateToStr(Date date){
String dateStr = DateUtils.formatDate(date);
String[] split = dateStr.split("-");
return split[0]+"年"+split[1]+"月"+split[2]+"日";
}
结果:
中间的内容缩起来了。 试了下中间的文档加最小高度:右下角的添加空白:
<p style="margin:0pt; orphans:0; text-align:justify; widows:0;position: absolute;top:40px;">
<span style="font-family:宋体; font-size:14pt">签批事由:</span>
</p>
<p style="margin:0pt; orphans:0; text-align:right; widows:0; min-height:150px;">
</p>
<p style="margin:0pt; orphans:0; text-align:right; widows:0;position: absolute;bottom:40px;right: 10px;">
<span style="font-family:宋体; font-size:14pt;padding-right: 50px;">用</span>
<span style="font-family:宋体; font-size:14pt;padding-right: 50px;">印章</span>
</p>
结果并不起作用。应该是java到导出的时候,空白的地方会缩进,这时候写css样式是不起作用的。
添加空行和占位符:
中间要加空行(因为中间不用填写内容),直接加多行:
<p style="margin:0pt; orphans:0; text-align:center; widows:0">
<span style="font-family:宋体; font-size:14pt"> </span>
</p>
空格使用占位符:  
<p style="margin:0pt; orphans:0; text-align:right; widows:0;position: absolute;bottom:40px;right: 10px;">
<span style="font-family:宋体; font-size:14pt;padding-right: 50px;">用  </span>
<span style="font-family:宋体; font-size:14pt;">印章 </span>
</p>
最后使用的html:
public class SignApprove {
public static String getSignApproveHtml() {
return "<html>\n" +
" <head>\n" +
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n" +
" <meta http-equiv=\"Content-Style-Type\" content=\"text/css\"/> \n" +
" <title></title>\n" +
" </head>\n" +
" <body>\n" +
" <div style=\"text-align:center;\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:22pt\">签批单</span>\n" +
" </p>\n" +
" <table cellspacing=\"0\" cellpadding=\"0\" style=\"border-collapse:collapse; margin-left:0pt;margin:0 auto;\">\n" +
" <tr style=\"height:31.85pt\">\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">经办人</span>\n" +
" </p>\n" +
" </td>\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:80.2pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:justify; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" </td>\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:46.75pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">日期</span>\n" +
" </p>\n" +
" </td>\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:139.05pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"></span>\n" +
" <span style=\"font-family:宋体; font-size:14pt\">年 </span>\n" +
" <span style=\"font-family:宋体; font-size:14pt\"></span>\n" +
" <span style=\"font-family:宋体; font-size:14pt\">月 </span>\n" +
" <span style=\"font-family:宋体; font-size:14pt\"></span>\n" +
" <span style=\"font-family:宋体; font-size:14pt\">日</span>\n" +
" </p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr style=\"height:228.6pt\">\n" +
" <td colspan=\"4\" style=\"position:relative; border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:404pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:justify; widows:0;position: absolute;top:40px;\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">签批事由:</span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0;position: absolute;bottom:40px;right: 10px;\">\n" +
" <span style=\"font-family:宋体; font-size:14pt;padding-right: 80px;\">用  </span>\n" +
" <span style=\"font-family:宋体; font-size:14pt;\">印章</span>\n" +
" <span style=\"font-family:宋体; font-size:14pt;\"> </span>\n" +
" </p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr style=\"height:102.85pt\">\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">主办处室负责人</span>\n" +
" </p>\n" +
" </td>\n" +
" <td colspan=\"3\" style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:287.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:justify; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">处理意见 </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0;padding-right: 10px;\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">处理人员 处理日期</span>\n" +
" </p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr style=\"height:96.25pt\">\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0;\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">相关处室会签</span>\n" +
" </p>\n" +
" </td>\n" +
" <td colspan=\"3\" style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:287.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:justify; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">处理意见 </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" <p style=\"margin:0pt; orphans:0; text-align:right; widows:0;padding-right: 10px;\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">处理人员 处理日期</span>\n" +
" </p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr style=\"height:45.65pt\">\n" +
" <td style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:105.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\">批准人</span>\n" +
" </p>\n" +
" </td>\n" +
" <td colspan=\"3\" style=\"border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-left-color:#000000; border-left-style:solid; border-left-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; padding-left:5.03pt; padding-right:5.03pt; vertical-align:middle; width:287.6pt\">\n" +
" <p style=\"margin:0pt; orphans:0; text-align:center; widows:0\">\n" +
" <span style=\"font-family:宋体; font-size:14pt\"> </span>\n" +
" </p>\n" +
" </td>\n" +
" </tr>\n" +
" <tr style=\"height:0pt\">\n" +
" <td style=\"width:116.4pt; border:none\"></td>\n" +
" <td style=\"width:91pt; border:none\"></td>\n" +
" <td style=\"width:57.55pt; border:none\"></td>\n" +
" <td style=\"width:149.85pt; border:none\"></td>\n" +
" </tr>\n" +
" </table>\n" +
" </div> \n" +
" </body>\n" +
"</html>\n";
}
}
再进行导出,就符合要求了。
总结:
导出word文档,可以考虑使用导出html的方式,尤其是复杂的,没多少规律的。如果是有多行空白的,就使用多行<p></p>的方式;对于多个空格的使用空格站位符。