要求导出签批单: 格式如下,
要获取“主办处室负责人”和“相关处室会签”环节的处理意见、处理人员和处理日期进行替换,导出word文档。
主要是如何拼接内容?
方法一:
导出word,直接拼内容,然后导出。 这样比较麻烦,而且格式控制是一个大问题(主要也不是很熟悉,处理起来就比较麻烦)
方法二:
把内容写到纯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]+"日";
}
中间的内容缩起来了。 试了下中间的文档加最小高度:右下角的添加空白:
签批事由:
用
印章
结果并不起作用。应该是java到导出的时候,空白的地方会缩进,这时候写css样式是不起作用的。
中间要加空行(因为中间不用填写内容),直接加多行:
空格使用占位符:
用
印章
public class SignApprove {
public static String getSignApproveHtml() {
return "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 签批单\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 经办人\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" 日期\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 年 \n" +
" \n" +
" 月 \n" +
" \n" +
" 日\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 签批事由:\n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" 用 \n" +
" 印章\n" +
" \n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 主办处室负责人\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" 处理意见 \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" 处理人员 处理日期\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 相关处室会签\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" 处理意见 \n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" 处理人员 处理日期\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 批准人\n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"
\n" +
" \n" +
" \n" +
"\n";
}
}
再进行导出,就符合要求了。
导出word文档,可以考虑使用导出html的方式,尤其是复杂的,没多少规律的。如果是有多行空白的,就使用多行
的方式;对于多个空格的使用空格站位符。