1.使用easyWord
2.模版 .doc文件
3.maven依赖
com.sushengren
easyword
1.1.3
4.
————————————————
package com.test.domain.word;
import com.sushengren.easyword.annotation.WordProperty;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/*
导出word
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Wxdj {@WordProperty("wxdjcode")
private String wxdjcode;@WordProperty("deptName")
private String deptName;@WordProperty("policeName")
private String policeName;@WordProperty("ctAjmc")
private String ctAjmc;@WordProperty("policePhone")
private String policePhone;@WordProperty("createdate")
private String createdate;
}
@ExcelIgnore 隐藏不展示实体类的字段
5.给模版赋值
/*8
* 下载通知书 模版信息
* @param ctId
*/
@Override
public void downloadNotice(String ctId, HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = this.getClass().getResource("/").getPath();
String rootPath = path.substring(1, path.length());
//TODO 后续将模版文件写在 sys_config 表中
String filePath = rootPath.replace("ruoyi-admin/target/classes/", "business/src/main/resources/stencil/wxdj.doc");
File file = new File(filePath);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TbNbgzWxdj nbgzWxdj = tbNbgzWxdjMapper.selectTbNbgzWxdjByCtId(ctId);
//外协单位-组织信息
TbNbgzWxdjdata dept = new TbNbgzWxdjdata();
dept.setCtIdP(ctId);
dept.setCtFunNo(FunNoConstants.NBGZ_WXDJWXDW_DEPT);
dept.setCtFunNop(FunNoConstants.NBGZ_WXDJ);
List<TbNbgzWxdjdata> deptList = tbNbgzWxdjwdataMapper.selectTbNbgzWxdjwdataList(dept);
List<String> deptNames = deptList.stream().map(TbNbgzWxdjdata::getCtDeptName).collect(Collectors.toList());
String deptName = String.join(",", deptNames);
//外协单位人员名称信息
List<TbNbgzWxdjdata> newPoliceList = new ArrayList<>();
for (int i = 0; i < deptList.size(); i++) {
TbNbgzWxdjdata tbNbgzWxdjdata = deptList.get(i);
//外协单位-人员信息
TbNbgzWxdjdata police = new TbNbgzWxdjdata();
police.setCtFunNop(FunNoConstants.NBGZ_WXDJ);
police.setCtFunNo(FunNoConstants.NBGZ_WXDJWXDW_POLICE);
police.setCtIdP(deptList.get(i).getCtId());
List<TbNbgzWxdjdata> policeList = tbNbgzWxdjwdataMapper.selectTbNbgzWxdjwdataList(police);
if (policeList.size() > 0) {
tbNbgzWxdjdata.setWxdwPoliceList(policeList);
}
newPoliceList.add(tbNbgzWxdjdata);
}
nbgzWxdj.setWxdwList(newPoliceList);
List<TbNbgzWxdjdata> wxdwPoliceList = newPoliceList.get(0).getWxdwPoliceList();
List<String> policeLists = wxdwPoliceList.stream().map(TbNbgzWxdjdata::getCtQqmjName).collect(Collectors.toList());
String policeName = String.join(",", policeLists);
//外协单位人员电话信息
List<String> policePhoneLists = newPoliceList.stream().map(TbNbgzWxdjdata::getCtQqmjPhone).collect(Collectors.toList());
String policePhone = String.join(",", policePhoneLists);
for (int i = 0; i < deptList.size(); i++) {
TbNbgzWxdjdata tbNbgzWxdjdata = deptList.get(i);
//外协单位-人员信息
TbNbgzWxdjdata police = new TbNbgzWxdjdata();
police.setCtFunNop(FunNoConstants.NBGZ_WXDJ);
police.setCtFunNo(FunNoConstants.NBGZ_WXDJWXDW_POLICE);
police.setCtIdP(deptList.get(i).getCtId());
List<TbNbgzWxdjdata> policeList = tbNbgzWxdjwdataMapper.selectTbNbgzWxdjwdataList(police);
if (policeList.size() > 0) {
tbNbgzWxdjdata.setWxdwPoliceList(policeList);
newPoliceList.addAll(policeList);
}
}
Wxdj wxdj = Wxdj.builder()
.wxdjcode(ctId)
.deptName(deptName)
.policeName(policeName)
.ctAjmc(nbgzWxdj.getCtAjmc())
.policePhone(policePhone)
.createdate(df.format(nbgzWxdj.getDtCreateDate()))
.build();
EasyWord.of(file).doWrite(wxdj).toOutputStream(response.getOutputStream());
}