web项目导出word表(思路+合并单元格+if判断+list循环)
2018-01-29 15:41
302 查看
一、导出word表第一步先用word表画出要导出的word表的样式如上图
然后另存为xml格式、控制层代码如下
/**
* 导出单个word
*/
@RequestMapping(value = "exportDoc")
public void exportDoc(String ids, HttpServletRequest request, HttpServletResponse response,
RedirectAttributes redirectAttributes) {
String docFileName = IdGen.uuid();
String directory =FileUtils.path(Global.getUserfilesBaseDir() + "\\resources\\download\\" + docFileName);
String zipDirectory =FileUtils.path(Global.getUserfilesBaseDir() + "\\resources\\download\\" + DateUtils.getDate("yyyyMMddHHmmss")+"docs");
try {
List<Ydkh> ydkhs = ydkhService.findListByIds(ids);
Map<String,Object> map = new HashMap<String, Object>();
for (Ydkh ydkh : ydkhs) {
String date = DateFormatUtils.format(ydkh.getKhendtime(), "yyyy年MM月");
String fileName = "月度考核"+ydkh.getUser().getName()+date+".doc";
map.put("ydkh", ydkh);
String title = date + DictUtils.getDictLabel(ydkh.getUser().getZw(), "zw_type", "") + "考核表";
map.put("title", title);
Lhzbkh lhzbkh = new Lhzbkh();
lhzbkh.setKhqj(ydkh.getKhqj());
lhzbkh.setUser(ydkh.getUser());
List<Lhzbkh> LhzbkhList = lhzbkhService.findList(lhzbkh);
if (LhzbkhList != null && !LhzbkhList.isEmpty()) {
lhzbkh = LhzbkhList.get(0);
}
map.put("lhzbkh", lhzbkh);
Gzxs gzxs = new Gzxs();
gzxs.setLhzbkh(lhzbkh );
gzxs.setTjStatus("1");
List<Gzxs> gzxsList = gzxsService.findList(gzxs);
for (Gzxs gzxs1:gzxsList) {
gzxs1.setGznr(Encodes.unescapeHtml(gzxs1.getGznr()));
}
map.put("gzxsList", gzxsList);
FileUtils.createDirectory(directory);
new DocUtil().newCreateRdoc(map, "ydkh2",fileName,directory);
}
ZipUtil zip = new ZipUtil();
zipDirectory = zipDirectory.substring(0,zipDirectory.length()-1)+".zip";
zip.compress(directory, zipDirectory, "GBK", "");
try {
// path是指欲下载的文件的路径。
File file = new File(zipDirectory);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(zipDirectory));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GBK")));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
addMessage(redirectAttributes, "导出失败!失败信息:"+e.getMessage());
// return "redirect:"+Global.getAdminPath()+"/gblzgl/ydkh/ydkh/ypHistoryList?status=4";
}
// return null;
}
/**
* 通过查询条件导出
* @param yd
* @param request
* @param response
* @param redirectAttributes
*/
@RequestMapping(value = "exportDocByCondition")
public void exportDoc(Ydkh yd, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
String docFileName = IdGen.uuid();
String directory =FileUtils.path(Global.getUserfilesBaseDir() + "\\resources\\download\\" + docFileName);
String zipDirectory =FileUtils.path(Global.getUserfilesBaseDir() + "\\resources\\download\\" + DateUtils.getDate("yyyyMMddHHmmss")+"docs");
try {
yd.setRskkhrid(UserUtils.getUser().getId());
List<Ydkh> ydkhs = ydkhService.findList(yd);
ydkhs = ydkhService.formartYdkh(ydkhs);
Map<String,Object> map = new HashMap<String, Object>();
for (Ydkh ydkh : ydkhs) {
String date = DateFormatUtils.format(ydkh.getKhendtime(), "yyyy年MM月");
String fileName = "月度考核"+ydkh.getUser().getName()+date+".doc";
map.put("ydkh", ydkh);
String title = date + DictUtils.getDictLabel(ydkh.getUser().getZw(), "zw_type", "") + "考核表";
map.put("titl
4000
e", title);
Lhzbkh lhzbkh = new Lhzbkh();
lhzbkh.setKhqj(ydkh.getKhqj());
lhzbkh.setUser(ydkh.getUser());
List<Lhzbkh> LhzbkhList = lhzbkhService.findList(lhzbkh);
if (LhzbkhList != null && !LhzbkhList.isEmpty()) {
lhzbkh = LhzbkhList.get(0);
}
map.put("lhzbkh", lhzbkh);
Gzxs gzxs = new Gzxs();
gzxs.setLhzbkh(lhzbkh );
gzxs.setTjStatus("1");
List<Gzxs> gzxsList = gzxsService.findList(gzxs);
for (Gzxs gzxs1:gzxsList) {
gzxs1.setGznr(Encodes.unescapeHtml(gzxs1.getGznr()));
}
map.put("gzxsList", gzxsList);
FileUtils.createDirectory(directory);
new DocUtil().newCreateRdoc(map, "ydkh2",fileName,directory);
}
ZipUtil zip = new ZipUtil();
zipDirectory = zipDirectory.substring(0,zipDirectory.length()-1)+".zip";
zip.compress(directory,zipDirectory , "GBK", "");
try {
// path是指欲下载的文件的路径。
File file = new File(FileUtils.path(zipDirectory));
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(zipDirectory));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition","attachment;filename="+new String(filename.getBytes("GBK")));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
addMessage(redirectAttributes, "导出失败!失败信息:"+e.getMessage());
}
}
导出的word样式
二、合并单元格
百度一大堆讲的
<w:vmerge w:val="restart"/>不合并单元格和
<w:vmerge/>合并单元格
思路就是<#if row_index == 0>第一条循环数据不合并<w:vMerge w:val="restart"
/></#if>
<#if row_index != 0>不是第一条循环数据合并<w:vMerge /></#if> 要放到<w:tc><w:tcPr><w:vMerge
/></w:tc></w:tcPr>里面
循环<#list
ydkh.gdzzList as row></#list>
具体代码如下:
<#list
ydkh.gdzzList as row>
<w:tr w:rsidR="005556A4" w:rsidRPr="009C2F85">
<w:trPr>
<w:trHeight w:val="810" />
<w:jc w:val="center" />
</w:trPr>
<#if row_index == 0>
<w:tc>
<w:tcPr>
<w:tcW w:w="724" w:type="dxa" />
<w:vMerge w:val="restart" />
<w:tcBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto" />
<w:left w:val="single" w:sz="4" w:space="0" w:color="auto" />
<w:bottom w:val="single" w:sz="4" w:space="0"
w:color="auto" />
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto" />
</w:tcBorders>
<w:shd w:val="clear" w:color="auto" w:fill="auto" />
<w:vAlign w:val="center" />
</w:tcPr>
<w:p w:rsidR="005556A4" w:rsidRPr="00FC2AB0" w:rsidRDefault="005556A4"
w:rsidP="00452B2A">
<w:pPr>
<w:widowControl />
<w:jc w:val="left" />
<w:rPr>
<w:rFonts w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体" />
<w:kern w:val="0" />
<w:sz w:val="22" />
</w:rPr>
</w:pPr>
<w:r w:rsidRPr="00FC2AB0">
<w:rPr>
<w:rFonts w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体"
w:hint="eastAsia" />
<w:kern w:val="0" />
<w:sz w:val="22" />
</w:rPr>
<w:t>履行职责(固定)</w:t>
</w:r>
</w:p>
</w:tc>
</#if>
<#if row_index != 0>
<w:tc>
<w:tcPr>
<w:tcW w:w="724" w:type="dxa" />
<w:vMerge />
<w:tcBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto" />
<w:left w:val="single" w:sz="4" w:space="0" w:color="auto" />
<w:bottom w:val="single" w:sz="4" w:space="0"
w:color="auto" />
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto" />
</w:tcBorders>
<w:shd w:val="clear" w:color="auto" w:fill="auto" />
<w:vAlign w:val="center" />
</w:tcPr>
<w:p w:rsidR="005556A4" w:rsidRPr="00FC2AB0" w:rsidRDefault="005556A4"
w:rsidP="00452B2A">
<w:pPr>
<w:widowControl />
<w:jc w:val="left" />
<w:rPr>
<w:rFonts w:ascii="宋体" w:hAnsi="宋体" w:cs="宋体" />
<w:kern w:val="0" />
<w:sz w:val="22" />
</w:rPr>
</w:pPr>
这个地方没有
<w:r
></w:r>
</w:p>
</w:tc>
</#if>
相关文章推荐
- 利用Freemarker生成doc文件(包含list循环,ifelse判断,合并单元格,嵌入表格单元格字数过多报错等)
- Python3学习笔记【基础3--list,tuple,if条件判断,for/while循环,dict字典,set集合】
- web项目导出word之用blob保存的图片
- web项目java后台将html表格导出为word表格文件
- freemaker导出word,带图片,list标签循环
- <c:forEach>循环list,<c:if>判断奇偶
- dedecms用channelartlist标签循环输出导航时,判断一级栏目下是否有子栏目的方法?
- if判断条件之List 如何表示不为null
- javaweb 项目设置welcome-file-list不生效的解决方法
- Eclipse将web项目以war包形式导出
- maven项目使用spring报java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListe
- ThinkPHP 3.2.2 在 模板中 多重循环嵌套中使用 if 判断标签
- foreach控制循环if判断
- eclipse svn导出 web 项目 显示为java 项目的解决办法
- while循环与if条件判断
- Excel文件的导入导出实战(1)--基于Struts2的web项目结构搭建
- JavaScript将Web页面内容导出到Word及Excel的方法
- JavaScript将Web页面内容导出到Word及Excel的方法
- weblogic服务上传word等文件直接打开问题解决 博客分类: web应用服务器 在weblogic上发布的web项目,测试中发现出现当上传word、excel、pdf等文件在下载的时候出现
- linked-list-cycle——链表、判断是否循环链表、快慢指针