将数据库中的数据导入到word文档中。
在word中提前制作数据模板,规定每个数据写入到哪个位置。
<repositories>
<repository>
<id>com.e-iceblueid>
<url>https://repo.e-iceblue.cn/repository/maven-public/url>
repository>
repositories>
<dependency>
<groupId>e-icebluegroupId>
<artifactId>spire.doc.freeartifactId>
<version>3.9.0version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.17version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>3.17version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-excelantartifactId>
<version>3.14version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-examplesartifactId>
<version>3.14version>
dependency>
<dependency>
<groupId>org.apache.xmlbeansgroupId>
<artifactId>xmlbeansartifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>ooxml-schemasartifactId>
<version>1.3version>
dependency>
/**
* 下载安全教育word 登记确认表
*
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/safe/word/1", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public EiInfo downloadSafeEduWord1(@Param("projectId") Integer projectId, final HttpServletResponse response) throws Exception {
// 获取文件
ResultMessage projectSafeEduWord = externalAssistProjectService.createProjectSafeEduWord(projectId);
if (projectSafeEduWord.getState() != 1) {
return ReturnOutInfo.outInfoFailure(new EiInfo(), "下载失败");
}
Map resMap = EiInfoUtils.objectToMap(projectSafeEduWord.getResult());
// 安全环保教育名单模板
String wordReport1 = resMap.get("wordReport1").toString();
File file1 = new File(wordReport1);
boolean a = downloadFile(file1, file1.getName(), response);
if (a) {
return ReturnOutInfo.outInfoSuccess(new EiInfo(), "下载成功");
} else {
return ReturnOutInfo.outInfoFailure(new EiInfo(), "下载失败");
}
}
/**
* 下载安全教育word 安全教育名单
*
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/safe/word/2", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
public EiInfo downloadSafeEduWord2(@Param("projectId") Integer projectId, final HttpServletResponse response) throws Exception {
// 获取文件
ResultMessage projectSafeEduWord = externalAssistProjectService.createProjectSafeEduWord(projectId);
if (projectSafeEduWord.getState() != 1) {
return ReturnOutInfo.outInfoFailure(new EiInfo(), "下载失败");
}
Map resMap = EiInfoUtils.objectToMap(projectSafeEduWord.getResult());
// 登记确认表模板
String wordReport2 = resMap.get("wordReport2").toString();
File file2 = new File(wordReport2);
boolean b = downloadFile(file2, file2.getName(), response);
if (b) {
return ReturnOutInfo.outInfoSuccess(new EiInfo(), "下载成功");
} else {
return ReturnOutInfo.outInfoFailure(new EiInfo(), "下载失败");
}
}
/**
* 给项目的安全教育生成word文档
*
* @param projectId 项目编号
*/
@Override
public ResultMessage createProjectSafeEduWord(Integer projectId) {
try {
// 查询项目信息
Map<String, Integer> queryMap = new HashMap<>();
queryMap.put("id", projectId);
queryMap.put("allData", 1);
ExternalAssistProject project = externalAssistProjectDao.queryExternalAssistProject(queryMap).get(0);
// 查询该项目项目下的人员信息
queryMap.put("projectId", projectId);
List<AssistProjectUserLicenseDTO> userList = externalAssistProjectDao.queryProjectUserLicense(queryMap);
if (EiInfoUtils.listIsEmpty(userList)) {
return new ResultMessage(-1, "该项目没有用户参与");
}
// 编辑安全环保教育名单
// 表格外内容
Map<String, String> textMap = new HashMap<>();
textMap.put("${projectName}", project.getProjectName());
textMap.put("${startYear}", project.getWorkCycleStart().substring(0, 4));
textMap.put("${startMonth}", project.getWorkCycleStart().substring(5, 7));
textMap.put("${startDate}", project.getWorkCycleStart().substring(8));
textMap.put("${endYear}", project.getWorkCycleEnd().substring(0, 4));
textMap.put("${endMonth}", project.getWorkCycleEnd().substring(5, 7));
textMap.put("${endDate}", project.getWorkCycleEnd().substring(8));
// 表格内内容
List<Map<String, String>> list = new ArrayList<>();
for (AssistProjectUserLicenseDTO user : userList) {
Map<String, String> map = new HashMap<>();
map.put("assistCompany", user.getAssistCompany());
map.put("username", user.getUsername());
map.put("sex", user.getSex());
map.put("age", String.valueOf(user.getAge()));
map.put("icCard", user.getIcCardWorkNumber());
list.add(map);
}
// 编辑审批登记表
// 查询项目一级安全教育信息
Map<String, Integer> querySafe = new HashMap<>();
querySafe.put("projectId", projectId);
querySafe.put("safeLevel", 1);
List<AssistSafetyEduDTO> projectSafeList = safetyEducationDao.queryAssistSafetyEdu(querySafe);
if (EiInfoUtils.listIsEmpty(projectSafeList)) {
return new ResultMessage(-1, "该项目未申请安全教育");
}
AssistSafetyEduDTO projectSafe = projectSafeList.get(0);
Map<String, String> map = new HashMap<>();
map.put("${assistCompany}", project.getAssistCompany());
map.put("${safetyCharge}", project.getSafetyEducationCharge());
map.put("${projectName}", project.getProjectName());
map.put("${contactPerson}", project.getContactPerson());
map.put("${manageA}", areaDictionaryDao.getManageAreaName(projectSafe.getManageArea()));
map.put("${work}", project.getWorkCycleStart() + "/" + project.getWorkCycleEnd());
map.put("${userNum}", userList.size() + "");
map.put("${checkR}", DictionaryUtil.getSafeStatusName(projectSafe.getCheckStatus()));
map.put("${sign}", "");
map.put("${checkD}", projectSafe.getCheckTime());
map.put("${classP}", projectSafe.getEduPerson());
map.put("${claD}", projectSafe.getEduDate());
map.put("${chargeP}", "");
// 写入登记确认表模板
String filePath1 = wordPath + registrationConfirmation;
String outPath1 = wordPath + "外协安全环保审批登记确认表-" + project.getProjectName() + ".docx";
WordReport wordReport1 = new WordReport();
wordReport1.setTempLocalPath(filePath1);
wordReport1.init2();
wordReport1.replaceSpecialWord(map);
wordReport1.generate(outPath1);
// 写入安全环保教育名单模板
// 文件地址
String filePath2 = wordPath + safeEducationUser;
String outPath2 = wordPath + "安全环保教育名单-" + project.getProjectName() + ".docx";
WordReport wordReport2 = new WordReport();
wordReport2.setTempLocalPath(filePath2);
wordReport2.init1();
wordReport2.export(list, 0, textMap);
wordReport2.generate(outPath2);
// 将生成的word路径发送到controller层
Map<String, String> resWord = new HashMap<>();
resWord.put("wordReport1", outPath1);
resWord.put("wordReport2", outPath2);
return new ResultMessage(1, "生成word成功", resWord);
} catch (IOException e) {
e.printStackTrace();
return new ResultMessage(-1, e.getMessage());
}
}
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
/**
* @author huangzixiao
* @Description
* @date 2022/9/19
*/
public class WordReport {
private String tempLocalPath;
private XWPFDocument xwpfDocument = null;
private FileInputStream inputStream = null;
private OutputStream outputStream = null;
private Document doc;
public WordReport() {
}
public WordReport(String tempLocalPath) {
this.tempLocalPath = tempLocalPath;
}
/**
* 设置模板路径
*
* @param tempLocalPath
*/
public void setTempLocalPath(String tempLocalPath) {
this.tempLocalPath = tempLocalPath;
}
/**
* 初始化
*
* @throws IOException
*/
public void init1() throws IOException {
inputStream = new FileInputStream(new File(this.tempLocalPath));
xwpfDocument = new XWPFDocument(inputStream);
}
/**
* 初始化
*
* @throws FileNotFoundException
*/
public void init2() throws FileNotFoundException {
inputStream = new FileInputStream(new File(this.tempLocalPath));
doc = new Document(inputStream);
}
/**
* 导出方法
*
* @param params 表格里的数据列表
* @param tableIndex 需要替换的几个表格的下标
* @param textMap 需要替换的文本的数据入参
* @return
*/
public boolean export(List<Map<String, String>> params, int tableIndex, Map<String, String> textMap) {
replaceText(xwpfDocument, textMap);
this.insertValueToTable(xwpfDocument, params, tableIndex);
return true;
}
/**
* 替换非表格埋点值
*
* @param xwpfDocument
* @param textMap
*/
public void replaceText(XWPFDocument xwpfDocument, Map<String, String> textMap) {
List<XWPFParagraph> paras = xwpfDocument.getParagraphs();
Set<String> keySet = textMap.keySet();
for (XWPFParagraph para : paras) {
// 当前段落属性
//System.out.println("--------------打印获取到的段落的每一行数据---------------" + para.getText());
String str = para.getText();
//System.out.println("-------------------------->>>>>>>>>" + para.getParagraphText());
List<XWPFRun> list = para.getRuns();
for (XWPFRun run : list) {
for (String key : keySet) {
if (key.equals(run.text())) {
run.setText(textMap.get(key), 0);
}
}
}
}
}
/**
* 循环填充表格内容
*
* @param xwpfDocument
* @param params
* @param tableIndex
*/
public void insertValueToTable(XWPFDocument xwpfDocument, List<Map<String, String>> params, int tableIndex) {
try {
List<XWPFTable> tableList = xwpfDocument.getTables();
if (tableList.size() <= tableIndex) {
throw new Exception("tableIndex对应的表格不存在");
}
XWPFTable table = tableList.get(tableIndex);
List<XWPFTableRow> rows = table.getRows();
if (rows.size() < 2) {
throw new Exception("tableIndex对应表格应该为2行");
}
// 模板的那一行
XWPFTableRow tmpRow = rows.get(1);
List<XWPFTableCell> tmpCells = null;
List<XWPFTableCell> cells = null;
XWPFTableCell tmpCell = null;
tmpCells = tmpRow.getTableCells();
String cellText = null;
String cellTextKey = null;
Map<String, Object> totalMap = null;
for (int i = 0, len = params.size(); i < len; i++) {
Map<String, String> map = params.get(i);
// 创建新的一行
XWPFTableRow row = table.createRow();
// 获取模板的行高,设置为新一行的行高
row.setHeight(tmpRow.getHeight());
cells = row.getTableCells();
for (int k = 0; k < cells.size(); k++) {
tmpCell = tmpCells.get(k);
XWPFTableCell cell = cells.get(k);
cellText = tmpCell.getText();
if (cellText != null && !cellText.equals("")) {
// 转换为mapKey对应的字段
cellTextKey = cellText.replace("$", "").replace("{", "").replace("}", "");
if (map.containsKey(cellTextKey)) {
// 填充内容
setCellText(tmpCell, cell, map.get(cellTextKey));
}
}
}
}
// 删除模板行
table.removeRow(1);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 复制模板行属性
*
* @param tmpCell
* @param cell
* @param text
*/
private void setCellText(XWPFTableCell tmpCell, XWPFTableCell cell, String text) {
CTTc cttc2 = tmpCell.getCTTc();
CTTcPr ctPr2 = cttc2.getTcPr();
CTTc cttc = cell.getCTTc();
CTTcPr ctPr = cttc.addNewTcPr();
if (ctPr2.getTcW() != null) {
ctPr.addNewTcW().setW(ctPr2.getTcW().getW());
}
if (ctPr2.getVAlign() != null) {
ctPr.addNewVAlign().setVal(ctPr2.getVAlign().getVal());
}
if (cttc2.getPList().size() > 0) {
CTP ctp = cttc2.getPList().get(0);
if (ctp.getPPr() != null) {
if (ctp.getPPr().getJc() != null) {
cttc.getPList().get(0).addNewPPr().addNewJc().setVal(ctp.getPPr().getJc().getVal());
}
}
}
if (ctPr2.getTcBorders() != null) {
ctPr.setTcBorders(ctPr2.getTcBorders());
}
XWPFParagraph tmpP = tmpCell.getParagraphs().get(0);
XWPFParagraph cellP = cell.getParagraphs().get(0);
XWPFRun tmpR = null;
if (tmpP.getRuns() != null && tmpP.getRuns().size() > 0) {
tmpR = tmpP.getRuns().get(0);
}
XWPFRun cellR = cellP.createRun();
cellR.setText(text);
// 复制段落信息
cellP.setAlignment(tmpP.getAlignment());
cellP.setVerticalAlignment(tmpP.getVerticalAlignment());
cellP.setBorderBetween(tmpP.getBorderBetween());
cellP.setBorderBottom(tmpP.getBorderBottom());
cellP.setBorderLeft(tmpP.getBorderLeft());
cellP.setBorderRight(tmpP.getBorderRight());
cellP.setBorderTop(tmpP.getBorderTop());
cellP.setPageBreak(tmpP.isPageBreak());
if (tmpP.getCTP() != null) {
if (tmpP.getCTP().getPPr() != null) {
CTPPr tmpPPr = tmpP.getCTP().getPPr();
CTPPr cellPPr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
// 复制段落间距信息
CTSpacing tmpSpacing = tmpPPr.getSpacing();
if (tmpSpacing != null) {
CTSpacing cellSpacing = cellPPr.getSpacing() != null ? cellPPr.getSpacing() : cellPPr.addNewSpacing();
if (tmpSpacing.getAfter() != null) {
cellSpacing.setAfter(tmpSpacing.getAfter());
}
if (tmpSpacing.getAfterAutospacing() != null) {
cellSpacing.setAfterAutospacing(tmpSpacing.getAfterAutospacing());
}
if (tmpSpacing.xgetAfterLines() != null) {
cellSpacing.setAfterLines(tmpSpacing.getAfterLines());
}
if (tmpSpacing.getBefore() != null) {
cellSpacing.setBefore(tmpSpacing.getBefore());
}
if (tmpSpacing.getBeforeAutospacing() != null) {
cellSpacing.setBeforeAutospacing(tmpSpacing.getBeforeAutospacing());
}
if (tmpSpacing.getBeforeLines() != null) {
cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines());
}
if (tmpSpacing.getLine() != null) {
cellSpacing.setLine(tmpSpacing.getLine());
}
if (tmpSpacing.getLineRule() != null) {
cellSpacing.setLineRule(tmpSpacing.getLineRule());
}
}
// 复制段落缩进信息
CTInd tmpInd = tmpPPr.getInd();
if (tmpInd != null) {
CTInd cellInd = cellPPr.getInd() != null ? cellPPr.getInd() : cellPPr.addNewInd();
if (tmpInd.getFirstLine() != null) {
cellInd.setFirstLine(tmpInd.getFirstLine());
}
if (tmpInd.getFirstLineChars() != null) {
cellInd.setFirstLineChars(tmpInd.getFirstLineChars());
}
if (tmpInd.getHanging() != null) {
cellInd.setHanging(tmpInd.getHanging());
}
if (tmpInd.getHangingChars() != null) {
cellInd.setHangingChars(tmpInd.getHangingChars());
}
if (tmpInd.getLeft() != null) {
cellInd.setLeft(tmpInd.getLeft());
}
if (tmpInd.getLeftChars() != null) {
cellInd.setLeftChars(tmpInd.getLeftChars());
}
if (tmpInd.getRight() != null) {
cellInd.setRight(tmpInd.getRight());
}
if (tmpInd.getRightChars() != null) {
cellInd.setRightChars(tmpInd.getRightChars());
}
}
}
}
}
/**
* 收尾方法
*
* @param outDocPath
* @return
* @throws IOException
*/
public boolean generate(String outDocPath) throws IOException {
outputStream = new FileOutputStream(outDocPath);
if (xwpfDocument != null) {
xwpfDocument.write(outputStream);
}
if (doc != null) {
doc.saveToFile(outputStream, FileFormat.Docx);
}
this.close(outputStream);
this.close(inputStream);
return true;
}
/**
* 关闭输入流
*
* @param is
*/
private void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭输出流
*
* @param os
*/
private void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 变换模板中的内容
*
* @param map
*/
public void replaceSpecialWord(Map<String, String> map) {
// 正则表达式
Pattern pattern = Pattern.compile("\\$\\{.*?}");
// 根据正则表达式获取所有文本
TextSelection[] allPattern = doc.findAllPattern(pattern);
for (TextSelection textSelection : allPattern) {
String tmp = map.get(textSelection.getSelectedText());
//System.out.print("------>"+textSelection.getSelectedText());
int res = doc.replace(textSelection.getSelectedText(), tmp, true, true);
//System.out.println(":"+res);
}
}
}
需要在word中实现一下效果
word文档中每一页都有一个相同格式的表格,但是内容不同
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>ooxml-schemasartifactId>
<version>1.1version>
dependency>
public class OfficeExportUtils {
/**
* 复制word中的表格
*
* @param templatePath 源文件
* @param targetPath 目标文件
* @param varPrefix 变量前缀
* @param copyNum 拷贝数量
* @param isNewPage 是否新增页
*/
public static void copyWordTable(String templatePath, String targetPath, String varPrefix, int copyNum, boolean isNewPage) {
File targetFile = new File(targetPath);
XWPFDocument document = null;
FileOutputStream out = null;
try {
document = new XWPFDocument(new FileInputStream(templatePath));
List<XWPFTable> tables = document.getTables();
if (EiInfoUtils.listIsEmpty(tables)) {
return;
}
List<XWPFTable> srcTables = new ArrayList<>(tables);
for (int i = 1; i <= copyNum; i++) {
XWPFParagraph paragraph = document.createParagraph();
if (isNewPage) {
// 新增空白页
paragraph.createRun().addBreak(BreakType.PAGE);
}
String newVarPreFix = "" + varPrefix + i;
for (int j = 0; j < srcTables.size(); j++) {
XWPFTable srcTable = srcTables.get(j);
XWPFTable newTable = document.createTable();
for (int n = 0; n < srcTable.getRows().size(); n++) {
XWPFTableRow srcRow = srcTable.getRows().get(n);
XWPFTableRow newRow = newTable.insertNewTableRow(n);
copyTableRow(srcRow, newRow, varPrefix, newVarPreFix);
}
newTable.removeRow(newTable.getRows().size() - 1);
}
}
out = new FileOutputStream(targetFile);
document.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (document != null) {
document.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 复制word中的表格行
*
* @param srcRow 源表格行
* @param newRow 目标表格行
* @param varPrefix 源变量前缀
* @param newVarPreFix 新变量前缀
*/
private static void copyTableRow(XWPFTableRow srcRow, XWPFTableRow newRow, String varPrefix, String newVarPreFix) {
int size = srcRow.getTableCells().size();
for (int i = 0; i < size; i++) {
newRow.addNewTableCell();
}
newRow.getCtRow().setTrPr(srcRow.getCtRow().getTrPr());
for (int i = 0; i < size; i++) {
copyTableCell(srcRow.getCell(i), newRow.getCell(i), varPrefix, newVarPreFix);
}
}
/**
* 复制word中的表格cell
*
* @param srcCell 源表格cell
* @param newCell 目标表格cell
* @param varPrefix 源变量前缀
* @param newVarPreFix 新变量前置
*/
private static void copyTableCell(XWPFTableCell srcCell, XWPFTableCell newCell, String varPrefix, String newVarPreFix) {
newCell.getCTTc().setTcPr(srcCell.getCTTc().getTcPr());
for (int i = 0; i < newCell.getParagraphs().size(); i++) {
newCell.removeParagraph(i);
}
for (XWPFParagraph srcParagraph : srcCell.getParagraphs()) {
XWPFParagraph newParagraph = newCell.addParagraph();
copyParagraph(srcParagraph, newParagraph, varPrefix, newVarPreFix);
}
}
/**
* 复制word中的表格Paragraph
*
* @param srcParagraph 源表格paragraph
* @param newParagraph 目标表格paragraph
* @param varPrefix 源变量前缀
* @param newVarPreFix 新变量前缀
*/
private static void copyParagraph(XWPFParagraph srcParagraph, XWPFParagraph newParagraph, String varPrefix, String newVarPreFix) {
newParagraph.getCTP().setPPr(srcParagraph.getCTP().getPPr());
for (int i = 0; i < newParagraph.getRuns().size(); i++) {
newParagraph.removeRun(i);
}
for (XWPFRun srcRun : srcParagraph.getRuns()) {
XWPFRun newRun = newParagraph.createRun();
copyRun(srcRun, newRun, varPrefix, newVarPreFix);
}
}
/**
* 复制word中的表格run
*
* @param srcRun 源表格Run
* @param newRun 目标表格Run
* @param varPrefix 源变量前缀
* @param newVarPreFix 新变量前缀
*/
private static void copyRun(XWPFRun srcRun, XWPFRun newRun, String varPrefix, String newVarPreFix) {
newRun.getCTR().setRPr(srcRun.getCTR().getRPr());
newRun.setText(EiInfoUtils.stringIsNotBlank(varPrefix) && EiInfoUtils.stringIsNotBlank(srcRun.text()) ? srcRun.text().replace(varPrefix, newVarPreFix) : srcRun.text());
}
}
/**
* 创建点检记录word 并转为pdf 由系统自动按时创建
*/
@Override
public void createCheckRecordDoc() {
File file = new File(fileLocalPath + "template/日常点检记录表模板.docx");
String temp = FileUtils.getFile(new File(fileLocalPath), sfFileDate.format(new Date()) + "点检记录文档" + ".docx").getAbsolutePath();
System.out.println(temp);
List<Integer> deviceIdList = deviceDao.queryAllDeviceId();
int copyNum = deviceIdList.size() - 1;
String varPrefix = "device";
//Map data = getUser(varPrefix, deviceIdList);
Map<String, Object> data = getCheckRecordList(varPrefix, deviceIdList);
// 复制模板,由于基础模板中已经有一个表格,因此只需要在复制3个表格即可
OfficeExportUtils.copyWordTable(file.getAbsolutePath(), temp, varPrefix, copyNum, true);
// 使用easyPoi导出
File outFile = FileUtils.getFile(new File(fileLocalPath), sfFileDate.format(new Date()) + "点检记录文档" + ".docx");
FileOutputStream out = null;
XWPFDocument xwpfDocument = null;
Workbook workbook = null;
// 导出pdf格式文件
try {
xwpfDocument = WordExportUtil.exportWord07(temp, data);
out = new FileOutputStream(outFile);
xwpfDocument.write(out);
// 生成的记录写入记录表
FileRecord insertFileRecord = new FileRecord();
insertFileRecord.setFileName(outFile.getName());
insertFileRecord.setFileUploader("系统自动创建");
insertFileRecord.setFileType(1);
insertFileRecord.setInsertTime(sfPicTime.format(new Date()));
insertFileRecord.setReleaseStatus(1);
fileRecordDao.insertFileRecord(insertFileRecord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (xwpfDocument != null) {
try {
xwpfDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private Map<String, Object> getCheckRecordList(String varPrefix, List<Integer> deviceIdList) {
Map<String, Object> checkRecordRes = new HashMap<>();
for (int i = 0; i < deviceIdList.size(); i++) {
Map<String, Object> device = new HashMap<>();
// 查询这个设备的信息
DeviceNumberNameVO deviceInfo = deviceDao.queryDeviceInfoByDeviceIdItemId(deviceIdList.get(i), 1);
device.put("productionLine", deviceInfo.getProductionLine());
device.put("name", deviceInfo.getDeviceName());
//device.put("baoRobotNumber", deviceInfo.getBaoRobotNumber());
device.put("checkDate", sfFileDate.format(new Date()));
// 统计该设备点检完成情况
Map<String, Integer> countFinishNum = new HashMap<>();
countFinishNum.put("deviceId", deviceInfo.getDeviceId());
// 统计全部数量
Integer sum = checkRecordDao.countCheckRecord(countFinishNum);
// 统计已完成的
countFinishNum.put("isFinish", 1);
Integer isFinish = checkRecordDao.countCheckRecord(countFinishNum);
// 统计未完成的
countFinishNum.put("isFinish", 0);
Integer notFinish = checkRecordDao.countCheckRecord(countFinishNum);
// 封装到word中
device.put("sum", sum);
device.put("finish", isFinish);
device.put("notFinish", notFinish);
// 根据设备编号查询点检记录
List<CheckRecordWordDataDTO> checkRecordList = checkRecordDao.queryCheckRecordByDeviceId(deviceIdList.get(i));
List<Map<String, Object>> checkRecords = new ArrayList<>();
for (int j = 0; j < checkRecordList.size(); j++) {
// 提取设备相关数据
if (EiInfoUtils.stringIsNotEmpty(checkRecordList.get(j).getCheckUser())) {
device.put("checkUser", checkRecordList.get(j).getCheckUser());
} else {
device.put("checkUser", "未点检");
}
// 提取点检记录
Map<String, Object> record = new HashMap<>();
record.put("no", j + 1);
record.put("checkItem", checkRecordList.get(j).getCheckItem());
record.put("checkDetails", checkRecordList.get(j).getCheckDetails());
record.put("checkResult", checkRecordList.get(j).getCheckResult());
record.put("resultDetails", checkRecordList.get(j).getResultDetails());
record.put("remark", checkRecordList.get(j).getRemark());
checkRecords.add(record);
}
device.put("checkRecords", checkRecords);
if (i == 0) {
checkRecordRes.put(varPrefix, device);
} else {
checkRecordRes.put(varPrefix + i, device);
}
}
return checkRecordRes;
}