industrial_measurement/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java

437 lines
22 KiB
Java
Raw Normal View History

2022-03-11 17:33:03 +08:00
package com.xkrs.service.impl;
2022-03-10 19:24:42 +08:00
2022-03-16 12:53:49 +08:00
import com.xkrs.dao.CraftItemDao;
2022-03-11 17:30:56 +08:00
import com.xkrs.dao.QcItemDao;
import com.xkrs.dao.QcSourceDao;
import com.xkrs.dao.QcSpecDao;
2022-03-12 13:52:15 +08:00
import com.xkrs.encapsulation.PromptMessageEnum;
2022-03-28 17:59:39 +08:00
import com.xkrs.model.bean.XSSFCellDataBean;
2022-03-16 12:53:49 +08:00
import com.xkrs.model.entity.CraftItemEntity;
2022-03-11 17:33:03 +08:00
import com.xkrs.model.entity.QcItemEntity;
import com.xkrs.model.entity.QcSourceEntity;
import com.xkrs.model.entity.QcSpecEntity;
2022-03-21 18:02:52 +08:00
import com.xkrs.model.qo.ExportSourceExcelQo;
2022-03-11 17:33:03 +08:00
import com.xkrs.model.qo.QcSourceQoDelete;
import com.xkrs.model.qo.QcSourceQoInsert;
import com.xkrs.service.QcSourceService;
2022-03-28 17:59:39 +08:00
import com.xkrs.util.*;
2022-03-12 13:52:15 +08:00
import org.apache.http.util.TextUtils;
2022-03-17 11:02:42 +08:00
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
2022-03-28 17:59:39 +08:00
import org.apache.poi.ss.usermodel.CellStyle;
2022-03-17 11:02:42 +08:00
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
2022-03-10 20:09:26 +08:00
import org.springframework.context.i18n.LocaleContextHolder;
2022-03-11 16:07:31 +08:00
import org.springframework.stereotype.Service;
2022-03-11 16:30:05 +08:00
import org.springframework.web.multipart.MultipartFile;
2022-03-10 20:09:26 +08:00
import javax.annotation.Resource;
2022-03-12 13:52:15 +08:00
import javax.transaction.Transactional;
2022-03-28 17:59:39 +08:00
import java.awt.*;
2022-03-17 11:42:19 +08:00
import java.io.File;
2022-03-17 11:02:42 +08:00
import java.io.FileOutputStream;
2022-03-11 17:03:37 +08:00
import java.io.IOException;
2022-03-28 17:59:39 +08:00
import java.util.List;
2022-03-12 13:52:15 +08:00
import java.util.*;
2022-03-10 19:24:42 +08:00
2022-03-10 20:50:21 +08:00
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
2022-03-11 16:07:31 +08:00
@Service
2022-03-10 19:24:42 +08:00
public class QcSourceServiceImpl implements QcSourceService {
2022-03-10 20:09:26 +08:00
private Locale locale = LocaleContextHolder.getLocale();
2022-03-16 12:53:49 +08:00
@Resource
private CraftItemDao craftItemDao;
2022-03-11 17:03:37 +08:00
@Resource
private QcItemDao qcItemDao;
2022-03-10 20:09:26 +08:00
@Resource
private QcSourceDao qcSourceDao;
@Resource
private QcSpecDao qcSpecDao;
2022-03-17 11:02:42 +08:00
/**
* 导出excel样式一表头顺序
* 批次号 机器号 物料号 模具号 机种号
*/
List<String> EXCEL_HEAD_STYLE_ONE_NO = Arrays.asList("batchNo", "machineNo", "materialNo", "mouldNo", "varietyNo", "craftItemNo");
List<String> EXCEL_HEAD_STYLE_ONE_NAME = Arrays.asList("批次", "机器", "物料", "模具", "机种", "工艺项目");
2022-03-10 20:50:21 +08:00
/**
2022-03-11 15:37:06 +08:00
* 添加检验数据
2022-03-10 20:50:21 +08:00
*/
2022-03-10 19:33:25 +08:00
@Override
2022-03-11 16:30:05 +08:00
public String insertQcSource(QcSourceQoInsert insertQo, MultipartFile picture) {
2022-03-10 20:50:21 +08:00
String batchNo = insertQo.getBatchNo();
String machineNo = insertQo.getMachineNo();
String materialNo = insertQo.getMaterialNo();
String mouldNo = insertQo.getMouldNo();
String varietyNo = insertQo.getVarietyNo();
2022-03-16 12:53:49 +08:00
String craftItemNo = insertQo.getCraftItemNo();
2022-03-10 20:50:21 +08:00
String qcItemNo = insertQo.getQcItemNo();
String qcValue = insertQo.getQcValue();
2022-03-16 12:53:49 +08:00
Optional<CraftItemEntity> craftItemByNo = craftItemDao.findByCraftItemNo(craftItemNo);
if (craftItemByNo.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该工艺项目,请先添加工艺项目!", locale);
}
Optional<QcItemEntity> qcItemByNo = qcItemDao.findByQcItemNo(qcItemNo);
if (qcItemByNo.isEmpty()) {
2022-03-11 17:03:37 +08:00
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该检验项目,请先添加检验项目!", locale);
}
2022-03-16 12:53:49 +08:00
if (Constant.QC_ITEM_TYPE_STRING.equals(qcItemByNo.get().getQcItemType())) {
2022-03-11 17:03:37 +08:00
QcSourceEntity entity = new QcSourceEntity();
entity.setCreateTime(LocalDateUtils.getCurrentSecond());
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));
2022-03-16 12:53:49 +08:00
entity.setCraftItemNo(LocalStringUtils.formatEmptyValue(craftItemNo));
2022-03-11 17:03:37 +08:00
entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo));
entity.setQcValue(LocalStringUtils.formatEmptyValue(qcValue));
qcSourceDao.save(entity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
}
2022-03-16 12:53:49 +08:00
if (Constant.QC_ITEM_TYPE_PICTURE.equals(qcItemByNo.get().getQcItemType())) {
2022-03-11 17:03:37 +08:00
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();
entity.setCreateTime(LocalDateUtils.getCurrentSecond());
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));
2022-03-16 12:53:49 +08:00
entity.setCraftItemNo(LocalStringUtils.formatEmptyValue(craftItemNo));
2022-03-11 17:03:37 +08:00
entity.setQcItemNo(LocalStringUtils.formatEmptyValue(qcItemNo));
entity.setQcValue(LocalStringUtils.formatEmptyValue(picturePath));
qcSourceDao.save(entity);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "添加成功!", locale);
}
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "添加失败!", locale);
2022-03-10 19:33:25 +08:00
}
2022-03-11 10:28:36 +08:00
/**
2022-03-11 15:37:06 +08:00
* 删除检验数据
*/
2022-03-12 13:52:15 +08:00
@Transactional(rollbackOn = Exception.class)
2022-03-11 15:37:06 +08:00
@Override
public String deleteQcSource(QcSourceQoDelete deleteQo) {
Integer id = deleteQo.getId();
Optional<QcSourceEntity> entityById = qcSourceDao.findById(id);
if (entityById.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "不存在该检验数据,请先添加检验数据!", locale);
}
qcSourceDao.deleteById(id);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功!", locale);
}
/**
* 查询检验数据
2022-03-11 10:28:36 +08:00
*/
@Override
2022-03-16 12:53:49 +08:00
public String queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) {
2022-03-17 11:02:42 +08:00
List<CraftItemEntity> craftItemList = craftItemDao.findAll();
if (craftItemList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale);
}
2022-03-12 13:52:15 +08:00
List<QcItemEntity> qcItemList = qcItemDao.findAll();
if (qcItemList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale);
2022-03-11 14:09:37 +08:00
}
2022-03-17 14:07:35 +08:00
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();
List<Map<String, Object>> sourceList;
if (TextUtils.isEmpty(craftItemNo)) {
sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo);
} else {
sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, craftItemNo.trim());
}
List<Map<String, Object>> resultList = generateResultList(sourceList);
2022-03-12 13:52:15 +08:00
if (resultList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale);
2022-03-11 17:23:34 +08:00
}
2022-03-12 13:52:15 +08:00
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, resultList, locale);
}
2022-03-11 17:23:34 +08:00
/**
* 生成返回结果
*/
private List<Map<String, Object>> generateResultList(List<Map<String, Object>> sourceList) {
2022-03-12 13:52:15 +08:00
List<Map<String, Object>> resultList = new ArrayList<>();
if (sourceList == null || sourceList.isEmpty()) {
return resultList;
}
for (Map<String, Object> source : sourceList) {
String batchNo = (String) source.getOrDefault("batchNo", "");
String machineNo = (String) source.getOrDefault("machineNo", "");
String materialNo = (String) source.getOrDefault("materialNo", "");
String mouldNo = (String) source.getOrDefault("mouldNo", "");
String varietyNo = (String) source.getOrDefault("varietyNo", "");
2022-03-16 12:53:49 +08:00
String craftItemNo = (String) source.getOrDefault("craftItemNo", "");
2022-03-12 13:52:15 +08:00
String qcItemNo = (String) source.getOrDefault("qcItemNo", "");
String qcValue = (String) source.getOrDefault("qcValue", "");
if (TextUtils.isEmpty(qcItemNo) || TextUtils.isEmpty(qcValue)) {
continue;
}
2022-03-16 12:53:49 +08:00
String formatValue = getFormatValue(varietyNo, craftItemNo, qcItemNo, qcValue);
Map<String, Object> emptyRoom = findEmptyRoom(resultList, qcItemNo, batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo);
2022-03-12 13:52:15 +08:00
if (emptyRoom == null) {
Map<String, Object> newRoom = new HashMap<>();
2022-03-17 11:02:42 +08:00
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(qcItemNo, formatValue);
2022-03-12 13:52:15 +08:00
resultList.add(newRoom);
} else {
emptyRoom.put(qcItemNo, formatValue);
2022-03-12 13:52:15 +08:00
}
}
return resultList;
}
2022-03-11 17:23:34 +08:00
/**
* 获取添加了 ##H ##L 后缀的测量值如果可能的话
*/
2022-03-16 12:53:49 +08:00
private String getFormatValue(String varietyNo, String craftItemNo, String qcItemNo, String qcValue) {
Optional<QcSpecEntity> existsQcSpecOptional = qcSpecDao.findExistsQcSpec(varietyNo, craftItemNo, qcItemNo);
if (existsQcSpecOptional.isEmpty()) {
return qcValue;
}
2022-03-16 12:53:49 +08:00
QcSpecEntity existsQcSpec = existsQcSpecOptional.get();
double valueMax = stringToDouble(existsQcSpec.getMax());
double valueMin = stringToDouble(existsQcSpec.getMin());
double value = stringToDouble(qcValue);
2022-03-28 17:59:39 +08:00
if (valueMax > -99_000D && value > -99_000D && value >= valueMax) {
return qcValue + "##H";
}
2022-03-28 17:59:39 +08:00
if (valueMin > -99_000D && value > -99_000D && value <= valueMin) {
return qcValue + "##L";
}
return qcValue;
}
/**
* 格式化字符串为Double
*/
private Double stringToDouble(String stringValue) {
try {
return Double.parseDouble(stringValue);
} catch (Exception e) {
e.printStackTrace();
2022-03-28 17:59:39 +08:00
return -100_000D;
}
}
/**
* 尝试找到一个qcItemNo有空位置的Map
*/
private Map<String, Object> findEmptyRoom(List<Map<String, Object>> mapList, String qcItemNo, String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) {
if (mapList == null || mapList.isEmpty() || TextUtils.isEmpty(qcItemNo)) {
2022-03-12 13:52:15 +08:00
return null;
}
for (Map<String, Object> room : mapList) {
if (room.getOrDefault("batchNo", "").equals(batchNo) && room.getOrDefault("machineNo", "").equals(machineNo) && room.getOrDefault("materialNo", "").equals(materialNo) && room.getOrDefault("mouldNo", "").equals(mouldNo) && room.getOrDefault("varietyNo", "").equals(varietyNo) && room.getOrDefault("craftItemNo", "").equals(craftItemNo)) {
2022-03-12 13:52:15 +08:00
if (!room.containsKey(qcItemNo)) {
return room;
}
}
}
return null;
2022-03-11 10:28:36 +08:00
}
2022-03-12 13:52:15 +08:00
2022-03-17 11:02:42 +08:00
/**
* 导出Excel表格
*/
@Override
2022-03-21 18:02:52 +08:00
public String exportExcel(ExportSourceExcelQo exportSourceExcelQo) throws Exception {
String host = exportSourceExcelQo.getHost();
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 craftItemNo = exportSourceExcelQo.getCraftItemNo();
2022-03-17 11:02:42 +08:00
List<CraftItemEntity> craftItemList = craftItemDao.findAll();
if (craftItemList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale);
}
List<QcItemEntity> qcItemList = qcItemDao.findAll();
if (qcItemList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale);
}
2022-03-17 14:07:35 +08:00
List<Map<String, Object>> sourceList;
if (TextUtils.isEmpty(craftItemNo)) {
sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo);
} else {
sourceList = qcSourceDao.queryQcSource(finalBatchNo, finalMachineNo, finalMaterialNo, finalMouldNo, finalVarietyNo, craftItemNo.trim());
}
2022-03-17 11:02:42 +08:00
List<Map<String, Object>> resultList = generateResultList(sourceList);
2022-03-17 11:42:19 +08:00
String finalHost = host.endsWith("/") ? host : host + "/";
List<List<String>> formattedResultList = formatResultList(finalHost, resultList, qcItemList);
2022-03-17 11:02:42 +08:00
if (formattedResultList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale);
}
2022-03-17 11:42:19 +08:00
String excelPath = doExportExcel(formattedResultList);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + excelPath, locale);
2022-03-17 11:02:42 +08:00
}
/**
* 格式化结果集数据
*/
2022-03-17 11:42:19 +08:00
private List<List<String>> formatResultList(String host, List<Map<String, Object>> resultList, List<QcItemEntity> qcItemList) {
2022-03-17 11:02:42 +08:00
List<List<String>> formatResultList = new ArrayList<>();
if (resultList == null || resultList.isEmpty()) {
return formatResultList;
}
List<String> headList = new ArrayList<>(EXCEL_HEAD_STYLE_ONE_NAME);
for (QcItemEntity qcItemEntity : qcItemList) {
headList.add(qcItemEntity.getQcItemName());
}
formatResultList.add(headList);
for (Map<String, Object> dataMap : resultList) {
List<String> dataList = new ArrayList<>();
dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(0), ""));
dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(1), ""));
dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(2), ""));
dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(3), ""));
dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(4), ""));
dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(5), ""));
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")) {
2022-03-17 11:42:19 +08:00
dataList.add(host + value);
2022-03-17 11:02:42 +08:00
} else {
dataList.add(value);
}
} else {
dataList.add(value);
}
}
formatResultList.add(dataList);
}
return formatResultList;
}
/**
* 将格式化后的结果集数据导出为Excel文件
*/
2022-03-17 11:42:19 +08:00
private String doExportExcel(List<List<String>> dataListList) throws Exception {
2022-03-17 11:02:42 +08:00
//创建XSSFWorkbook对象(excel的文档对象)
XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单
2022-03-21 18:02:52 +08:00
XSSFSheet sheet = workbook.createSheet("检验数据表");
2022-03-28 17:59:39 +08:00
for (int i = 0; i < dataListList.get(0).size(); i++) {
if (i <= 5) {
sheet.setColumnWidth(i, 20 * 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);//填充模式
2022-03-17 11:02:42 +08:00
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的行)可以是065535之间的任何一个
XSSFRow titleRow = sheet.createRow(0);
//创建单元格excel的单元格参数为列索引可以是0255之间的任何一个
XSSFCell titleCell = titleRow.createCell(0);
//设置单元格内容
2022-03-21 18:02:52 +08:00
titleCell.setCellValue("检验数据导出表");
2022-03-17 11:02:42 +08:00
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<String> 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);
2022-03-28 17:59:39 +08:00
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]));
2022-03-17 11:02:42 +08:00
cell.setCellStyle(highCellStyle);
2022-03-28 17:59:39 +08:00
} else if (sourceValue.contains("##L")) {
cell.setCellValue(LocalDoubleUtils.keepThreeDecimal(sourceValue.split("##")[0]));
2022-03-17 11:02:42 +08:00
cell.setCellStyle(lowCellStyle);
} else {
2022-03-28 17:59:39 +08:00
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()));
}
2022-03-17 11:02:42 +08:00
cell.setCellStyle(normalCellStyle);
}
}
}
}
//输出Excel文件
2022-03-17 11:42:19 +08:00
File dirFile = new File("/home/sxy/server/industrial_measurement/excel/");
2022-03-21 18:02:52 +08:00
// File dirFile = new File("/Users/liuchengqian/Desktop/DaJiang/");
2022-03-17 11:42:19 +08:00
if (!dirFile.exists()) {
dirFile.mkdirs();
}
2022-03-17 11:02:42 +08:00
String excelFileName = System.currentTimeMillis() + ".xlsx";
2022-03-17 11:42:19 +08:00
FileOutputStream output = new FileOutputStream(new File(dirFile, excelFileName));
2022-03-17 11:02:42 +08:00
workbook.write(output);
output.flush();
2022-03-17 11:42:19 +08:00
return "excel/" + excelFileName;
2022-03-17 11:02:42 +08:00
}
2022-03-10 19:24:42 +08:00
}