package com.xkrs.service.impl; import com.xkrs.dao.CraftItemDao; import com.xkrs.dao.QcItemDao; import com.xkrs.dao.QcSourceDao; import com.xkrs.dao.QcSpecDao; import com.xkrs.encapsulation.PromptMessageEnum; import com.xkrs.model.bean.XSSFCellDataBean; import com.xkrs.model.entity.CraftItemEntity; import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcSourceEntity; import com.xkrs.model.entity.QcSpecEntity; import com.xkrs.model.qo.ExportSourceExcelQo; import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.service.QcSourceService; import com.xkrs.util.*; import org.apache.http.util.TextUtils; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.*; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.transaction.Transactional; import java.awt.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.*; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; @Service public class QcSourceServiceImpl implements QcSourceService { private Locale locale = LocaleContextHolder.getLocale(); @Resource private CraftItemDao craftItemDao; @Resource private QcItemDao qcItemDao; @Resource private QcSourceDao qcSourceDao; @Resource private QcSpecDao qcSpecDao; /** * 导出excel样式一(表头顺序) * 批次号 机器号 物料号 模具号 机种号 */ List EXCEL_HEAD_STYLE_ONE_NO = Arrays.asList("batchNo", "machineNo", "materialNo", "mouldNo", "varietyNo", "craftItemNo", "createTime", "index"); List EXCEL_HEAD_STYLE_ONE_NAME = Arrays.asList("批次", "机器", "物料", "模具", "机种", "工艺项目", "上传时间", "过程抽检批次"); /** * 添加检验数据 */ @Override public String insertQcSource(QcSourceQoInsert insertQo, MultipartFile picture) { String batchNo = insertQo.getBatchNo(); String machineNo = insertQo.getMachineNo(); String materialNo = insertQo.getMaterialNo(); String mouldNo = insertQo.getMouldNo(); String varietyNo = insertQo.getVarietyNo(); String craftItemNo = insertQo.getCraftItemNo(); String qcItemNo = insertQo.getQcItemNo(); String qcValue = insertQo.getQcValue(); Optional craftItemByNo = craftItemDao.findByCraftItemNo(craftItemNo); if (craftItemByNo.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该工艺项目,请先添加工艺项目!", locale); } Optional qcItemByNo = qcItemDao.findByQcItemNo(qcItemNo); if (qcItemByNo.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该检验项目,请先添加检验项目!", locale); } if (Constant.QC_ITEM_TYPE_STRING.equals(qcItemByNo.get().getQcItemType())) { System.out.println("添加检验数据(文本类型)" + insertQo.toString()); QcSourceEntity entity = new QcSourceEntity(); String currentSecond = LocalDateUtils.getCurrentSecond(); entity.setCreateTime(currentSecond); entity.setCreateTimeFormat(LocalDateUtils.formatCurrentSecond(currentSecond)); entity.setBatchNo(LocalStringUtils.formatEmptyValue(batchNo)); entity.setMachineNo(LocalStringUtils.formatEmptyValue(machineNo)); entity.setMaterialNo(LocalStringUtils.formatEmptyValue(materialNo)); entity.setMouldNo(LocalStringUtils.formatEmptyValue(mouldNo)); entity.setVarietyNo(LocalStringUtils.formatEmptyValue(varietyNo)); entity.setCraftItemNo(LocalStringUtils.formatEmptyValue(craftItemNo)); entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo)); if (TextUtils.isEmpty(qcValue)) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "添加失败,测量值(qcValue)字段不能为空!", locale); } entity.setQcValue(LocalStringUtils.formatEmptyValue(qcValue)); qcSourceDao.save(entity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); } if (Constant.QC_ITEM_TYPE_PICTURE.equals(qcItemByNo.get().getQcItemType())) { System.out.println("添加检验数据(图片类型)" + insertQo.toString()); if (picture == null) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "上传图片不能为空!", locale); } String picturePath; try { picturePath = ExcelUploadUtil.memoryFile(picture, 2); } catch (IOException e) { e.printStackTrace(); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "上传图片失败!", locale); } QcSourceEntity entity = new QcSourceEntity(); String currentSecond = LocalDateUtils.getCurrentSecond(); entity.setCreateTime(currentSecond); entity.setCreateTimeFormat(LocalDateUtils.formatCurrentSecond(currentSecond)); entity.setBatchNo(LocalStringUtils.formatEmptyValue(batchNo)); entity.setMachineNo(LocalStringUtils.formatEmptyValue(machineNo)); entity.setMaterialNo(LocalStringUtils.formatEmptyValue(materialNo)); entity.setMouldNo(LocalStringUtils.formatEmptyValue(mouldNo)); entity.setVarietyNo(LocalStringUtils.formatEmptyValue(varietyNo)); entity.setCraftItemNo(LocalStringUtils.formatEmptyValue(craftItemNo)); entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo)); entity.setQcValue(LocalStringUtils.formatEmptyValue(picturePath)); qcSourceDao.save(entity); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale); } System.out.println("添加检验数据(未知类型)" + insertQo.toString()); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "添加失败!", locale); } /** * 删除检验数据 */ @Transactional(rollbackOn = Exception.class) @Override public String deleteQcSource(QcSourceQoDelete deleteQo) { Integer id = deleteQo.getId(); Optional entityById = qcSourceDao.findById(id); if (entityById.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该检验数据,请先添加检验数据!", locale); } qcSourceDao.deleteById(id); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功!", locale); } /** * 查询检验数据 */ @Override public String queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) { long startTimeMillis = System.currentTimeMillis(); List craftItemList = craftItemDao.findAll(); if (craftItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); } List qcItemList = qcItemDao.findAll(); if (qcItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale); } String finalBatchNo = LocalStringUtils.formatEmptyValue(batchNo).trim(); String finalMachineNo = LocalStringUtils.formatEmptyValue(machineNo).trim(); String finalMaterialNo = LocalStringUtils.formatEmptyValue(materialNo).trim(); String finalMouldNo = LocalStringUtils.formatEmptyValue(mouldNo).trim(); String finalVarietyNo = LocalStringUtils.formatEmptyValue(varietyNo).trim(); String finalCraftItemNo = LocalStringUtils.formatEmptyValue(craftItemNo).trim(); //检验数据结果集 List sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, finalCraftItemNo); //检验规格 List qcSpecList = qcSpecDao.findAll(); List> resultList = generateResultList(sourceList, qcSpecList); System.out.println("查询检验数据耗时:" + formatDuring(System.currentTimeMillis() - startTimeMillis)); if (resultList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); } return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale); } /** * 生成返回结果 * * @param sourceList 检验数据结果集 * @param qcSpecList 检验规格结果集 * @return */ private List> generateResultList(List sourceList, List qcSpecList) { List> resultList = new ArrayList<>(); if (sourceList == null || sourceList.isEmpty()) { return resultList; } long startTimeMillis = System.currentTimeMillis(); //这个for循环完成后:一维结果集被格式化为二维结果集,每一行的上传时间是本行所有数据的最早时间 for (QcSourceEntity sourceEntity : sourceList) { String batchNo = LocalStringUtils.formatEmptyValue(sourceEntity.getBatchNo()); String machineNo = LocalStringUtils.formatEmptyValue(sourceEntity.getMachineNo()); String materialNo = LocalStringUtils.formatEmptyValue(sourceEntity.getMaterialNo()); String mouldNo = LocalStringUtils.formatEmptyValue(sourceEntity.getMouldNo()); String varietyNo = LocalStringUtils.formatEmptyValue(sourceEntity.getVarietyNo()); String craftItemNo = LocalStringUtils.formatEmptyValue(sourceEntity.getCraftItemNo()); String createTime = LocalStringUtils.formatEmptyValue(sourceEntity.getCreateTime()); String qcItemNo = LocalStringUtils.formatEmptyValue(sourceEntity.getQcItemNo()); String qcValue = LocalStringUtils.formatEmptyValue(sourceEntity.getQcValue()); String formatValue = getFormatValue(qcSpecList, varietyNo, craftItemNo, qcItemNo, qcValue); Map emptyRoom = findEmptyRoom(resultList, qcItemNo, batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo); if (emptyRoom == null) { Map newRoom = new HashMap<>(); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(0), batchNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(1), machineNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(2), materialNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(3), mouldNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(4), varietyNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(5), craftItemNo); newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(6), createTime);//新行的上传时间使用第一个数据的上传时间 newRoom.put(qcItemNo, formatValue); resultList.add(newRoom); } else { emptyRoom.put(qcItemNo, formatValue); long oldTime = Long.parseLong((String) emptyRoom.get(EXCEL_HEAD_STYLE_ONE_NO.get(6))); if (Long.parseLong(createTime) < oldTime) {//已存在行的上传时间用更早的时间替换更晚的时间 emptyRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(6), createTime); } } } //这个for循环完成后:二维结果集每一行的上传时间是本分组所有数据的最早时间 for (Map result : resultList) { String createTime = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(6)); List createTimeTagList = getCreateTimeTagList(resultList, result); int createTimeTagIndex = getCreateTimeTagIndex(createTimeTagList, Long.parseLong(createTime)); result.put(EXCEL_HEAD_STYLE_ONE_NO.get(6), String.valueOf(createTimeTagList.get(createTimeTagIndex))); result.put(EXCEL_HEAD_STYLE_ONE_NO.get(7), "第" + (createTimeTagIndex + 1) + "批"); } System.out.println("generateResultList耗时:" + formatDuring(System.currentTimeMillis() - startTimeMillis)); return resultList; } /** * 获取上传时间节点索引 * * @param createTimeTagList 上传时间节点集合 * @param createTimeLong 当前行上传时间(其实也是当前行所有数据的最早上传时间) * @return 上传时间节点索引 */ private int getCreateTimeTagIndex(List createTimeTagList, long createTimeLong) { for (int i = 0; i < createTimeTagList.size(); i++) { long dur = createTimeLong - createTimeTagList.get(i); if (dur >= 0 && dur <= Constant.QC_TIME_INTERVAL) { return i; } } throw new RuntimeException("获取上传时间节点索引时发生异常"); } /** * 获取上传时间节点集合 * * @param resultList 二维结果集 * @param currentResult 当前行 * @return 当前行所在分组的时间节点集合 */ private List getCreateTimeTagList(List> resultList, Map currentResult) { String currentBatchNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(0)); String currentMachineNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(1)); String currentMaterialNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(2)); String currentMouldNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(3)); String currentVarietyNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(4)); String currentCraftItemNo = (String) currentResult.get(EXCEL_HEAD_STYLE_ONE_NO.get(5)); List createTimeList = new ArrayList<>(); //这个for循环的作用是在二维结果集中过滤出和当前行数据属于同一分组的所有行上传时间集合 for (Map result : resultList) { String batchNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(0)); String machineNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(1)); String materialNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(2)); String mouldNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(3)); String varietyNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(4)); String craftItemNo = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(5)); String createTime = (String) result.get(EXCEL_HEAD_STYLE_ONE_NO.get(6)); if (batchNo.equals(currentBatchNo) && machineNo.equals(currentMachineNo) && materialNo.equals(currentMaterialNo) && mouldNo.equals(currentMouldNo) && varietyNo.equals(currentVarietyNo) && craftItemNo.equals(currentCraftItemNo)) { createTimeList.add(Long.parseLong(createTime)); } } //对上传时间集合进行排序 createTimeList.sort(Long::compareTo); //根据上传时间集合和时间间隔生成上传时间节点集合 List createTimeTagList = new ArrayList<>();//时间节点列表 for (int i = 0; i < createTimeList.size(); i++) { Long createTime = createTimeList.get(i); if (createTimeTagList.isEmpty()) { createTimeTagList.add(createTime); continue; } Long newCreateTimeTag = createTimeTagList.get(createTimeTagList.size() - 1);//最新的时间节点 if (createTime >= newCreateTimeTag + Constant.QC_TIME_INTERVAL) { createTimeTagList.add(createTime); } } return createTimeTagList; } /** * 获取添加了 ##H 或 ##L 后缀的测量值(如果可能的话) */ private String getFormatValue(List qcSpecList, String varietyNo, String craftItemNo, String qcItemNo, String qcValue) { if (qcSpecList == null || qcSpecList.isEmpty()) { return qcValue; } for (int i = 0; i < qcSpecList.size(); i++) { QcSpecEntity qcSpec = qcSpecList.get(i); if (varietyNo.equals(qcSpec.getVarietyNo()) && craftItemNo.equals(qcSpec.getCraftItemNo()) && qcItemNo.equals(qcSpec.getQcItemNo())) { double valueMax = stringToDouble(qcSpec.getMax()); double valueMin = stringToDouble(qcSpec.getMin()); double value = stringToDouble(qcValue); if (valueMax > -99_000D && value > -99_000D && value >= valueMax) { return qcValue + "##H"; } if (valueMin > -99_000D && value > -99_000D && value <= valueMin) { return qcValue + "##L"; } return qcValue; } } return qcValue; } /** * 格式化字符串为Double */ private Double stringToDouble(String stringValue) { try { return Double.parseDouble(stringValue); } catch (Exception e) { // e.printStackTrace(); // return -100_000D; } return -100_000D; } /** * 尝试找到一个qcItemNo有空位置的Map */ private Map findEmptyRoom(List> mapList, String qcItemNo, String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) { if (mapList == null || mapList.isEmpty() || TextUtils.isEmpty(qcItemNo)) { return null; } for (Map room : mapList) { if (room.get(EXCEL_HEAD_STYLE_ONE_NO.get(0)).equals(batchNo) && room.get(EXCEL_HEAD_STYLE_ONE_NO.get(1)).equals(machineNo) && room.get(EXCEL_HEAD_STYLE_ONE_NO.get(2)).equals(materialNo) && room.get(EXCEL_HEAD_STYLE_ONE_NO.get(3)).equals(mouldNo) && room.get(EXCEL_HEAD_STYLE_ONE_NO.get(4)).equals(varietyNo) && room.get(EXCEL_HEAD_STYLE_ONE_NO.get(5)).equals(craftItemNo)) { if (!room.containsKey(qcItemNo)) { return room; } } } return null; } /** * 导出Excel表格 */ @Override public String exportExcel(ExportSourceExcelQo exportSourceExcelQo) throws Exception { String host = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getHost()).trim(); String finalBatchNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getBatchNo()).trim(); String finalMachineNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMachineNo()).trim(); String finalMaterialNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMaterialNo()).trim(); String finalMouldNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMouldNo()).trim(); String finalVarietyNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getVarietyNo()).trim(); String finalCraftItemNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getCraftItemNo()).trim(); List craftItemList = craftItemDao.findAll(); if (craftItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); } List qcItemList = qcItemDao.findAll(); if (qcItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale); } List sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, finalCraftItemNo); List qcSpecList = qcSpecDao.findAll(); List> resultList = generateResultList(sourceList, qcSpecList); String finalHost = host.endsWith("/") ? host : host + "/"; List> formattedResultList = formatResultList(finalHost, resultList, qcItemList); if (formattedResultList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); } String excelPath = doExportExcel(formattedResultList); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + excelPath, locale); } /** * 格式化结果集数据 */ private List> formatResultList(String host, List> resultList, List qcItemList) { List> formatResultList = new ArrayList<>(); if (resultList == null || resultList.isEmpty()) { return formatResultList; } List headList = new ArrayList<>(EXCEL_HEAD_STYLE_ONE_NAME); for (QcItemEntity qcItemEntity : qcItemList) { headList.add(qcItemEntity.getQcItemName()); } formatResultList.add(headList); for (Map dataMap : resultList) { List dataList = new ArrayList<>(); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(0))); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(1))); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(2))); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(3))); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(4))); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(5))); dataList.add(LocalDateUtils.exportFormatCurrentSecond((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(6)))); dataList.add((String) dataMap.get(EXCEL_HEAD_STYLE_ONE_NO.get(7))); for (QcItemEntity entity : qcItemList) { String value = (String) dataMap.getOrDefault(entity.getQcItemNo(), ""); if (Constant.QC_ITEM_TYPE_STRING.equals(entity.getQcItemType())) { dataList.add(value); } else if (Constant.QC_ITEM_TYPE_PICTURE.equals(entity.getQcItemType())) { if (value.startsWith("picture")) { dataList.add(host + value); } else { dataList.add(value); } } else { dataList.add(value); } } formatResultList.add(dataList); } return formatResultList; } /** * 将格式化后的结果集数据导出为Excel文件 */ private String doExportExcel(List> dataListList) throws Exception { //创建XSSFWorkbook对象(excel的文档对象) XSSFWorkbook workbook = new XSSFWorkbook(); //建立新的sheet对象(excel的表单) XSSFSheet sheet = workbook.createSheet("检验数据表"); for (int i = 0; i < dataListList.get(0).size(); i++) { if (i <= 7) { sheet.setColumnWidth(i, 22 * 256); } else { sheet.setColumnWidth(i, 14 * 256); } } XSSFCellStyle headCellStyle = workbook.createCellStyle(); headCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); XSSFFont headFont = workbook.createFont(); headFont.setColor(HSSFColor.BLACK.index); headCellStyle.setFont(headFont); headCellStyle.setFillForegroundColor(new XSSFColor(new Color(156, 195, 230)));//设置背景色 headCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充模式 XSSFCellStyle highCellStyle = workbook.createCellStyle(); highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); highCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); XSSFFont highFont = workbook.createFont(); highFont.setColor(HSSFColor.RED.index); highCellStyle.setFont(highFont); XSSFCellStyle lowCellStyle = workbook.createCellStyle(); lowCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); lowCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); XSSFFont lowFont = workbook.createFont(); lowFont.setColor(HSSFColor.BLUE.index); lowCellStyle.setFont(lowFont); XSSFCellStyle normalCellStyle = workbook.createCellStyle(); normalCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); normalCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); XSSFFont normalFont = workbook.createFont(); normalFont.setColor(HSSFColor.BLACK.index); normalCellStyle.setFont(normalFont); //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 XSSFRow titleRow = sheet.createRow(0); //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 XSSFCell titleCell = titleRow.createCell(0); //设置单元格内容 titleCell.setCellValue("检验数据导出表"); titleCell.setCellStyle(normalCellStyle); //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, dataListList.get(0).size() - 1)); int startRowNum = 1; for (int i = 0; i < dataListList.size(); i++) { List dataList = dataListList.get(i); if (dataList != null && dataList.size() > 0) { XSSFRow row = sheet.createRow(i + startRowNum); for (int j = 0; j < dataList.size(); j++) { XSSFCell cell = row.createCell(j); String sourceValue = dataList.get(j); if (i == 0) { cell.setCellValue(sourceValue); cell.setCellStyle(headCellStyle); } else if (j <= 5) { cell.setCellValue(sourceValue); cell.setCellStyle(normalCellStyle); } else if (sourceValue.contains("##H")) { cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(sourceValue.split("##")[0])); cell.setCellStyle(highCellStyle); } else if (sourceValue.contains("##L")) { cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(sourceValue.split("##")[0])); cell.setCellStyle(lowCellStyle); } else { XSSFCellDataBean cellDataBean = new XSSFCellDataBean(sourceValue); if (XSSFCellDataBean.VALUE_TYPE_STRING == cellDataBean.getValueType()) { cell.setCellValue(cellDataBean.getStringValue()); } else if (XSSFCellDataBean.VALUE_TYPE_DOUBLE == cellDataBean.getValueType()) { cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(cellDataBean.getDoubleValue())); } cell.setCellStyle(normalCellStyle); } } } } //输出Excel文件 File dirFile = new File("/home/sxy/server/industrial_measurement/excel/"); // File dirFile = new File("/Users/liuchengqian/Desktop/DaJiang/"); if (!dirFile.exists()) { dirFile.mkdirs(); } String excelFileName = System.currentTimeMillis() + ".xlsx"; FileOutputStream output = new FileOutputStream(new File(dirFile, excelFileName)); workbook.write(output); output.flush(); return "excel/" + excelFileName; } /** * 格式化创建时间历史数据 */ @Override public String formatCreateTime() { System.out.println("开始格式化创建时间历史数据"); List sourceEntityList = qcSourceDao.findAll(); long startTimeMillis = System.currentTimeMillis(); int listSize = sourceEntityList.size(); for (int i = 0; i < listSize; i++) { QcSourceEntity sourceEntity = sourceEntityList.get(i); String createTime = sourceEntity.getCreateTime(); String formatCurrentSecond = LocalDateUtils.formatCurrentSecond(createTime); qcSourceDao.formatCreateTime(sourceEntity.getId(), formatCurrentSecond); if (i % 10 == 0) { long spendTimeMillis = System.currentTimeMillis() - startTimeMillis; double remainTimeMillis = (listSize - i) * spendTimeMillis * 1D / i; System.out.println("已完成(" + i + "/" + listSize + "),用时" + formatDuring(spendTimeMillis) + ",剩余" + formatDuring((long) remainTimeMillis)); } } return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "格式化创建时间历史数据成功", locale); } public static String formatDuring(long mss) { long days = mss / (1000 * 60 * 60 * 24); long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); long seconds = (mss % (1000 * 60)) / 1000; if (days <= 0 && hours <= 0 && minutes <= 0) { return seconds + " 秒 "; } if (days <= 0 && hours <= 0) { return minutes + " 分钟 " + seconds + " 秒 "; } if (days <= 0) { return hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; } return days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 "; } }