您的位置:首页 > 其它

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: