导出检验规格

This commit is contained in:
liuchengqian 2022-03-21 18:02:52 +08:00
parent 5f2e367acc
commit b284577437
11 changed files with 147 additions and 38 deletions

View File

@ -1,6 +1,6 @@
package com.xkrs.controller; package com.xkrs.controller;
import com.xkrs.model.qo.ExportExcelQo; import com.xkrs.model.qo.ExportSourceExcelQo;
import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoDelete;
import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.model.qo.QcSourceQoInsert;
import com.xkrs.service.QcSourceService; import com.xkrs.service.QcSourceService;
@ -51,8 +51,8 @@ public class QcSourceController {
* 导出Excel表格 * 导出Excel表格
*/ */
@PostMapping("/exportExcel") @PostMapping("/exportExcel")
public String exportExcel(@RequestBody ExportExcelQo exportExcelQo) throws Exception { public String exportExcel(@RequestBody ExportSourceExcelQo exportSourceExcelQo) throws Exception {
return qcSourceService.exportExcel(exportExcelQo); return qcSourceService.exportExcel(exportSourceExcelQo);
} }
} }

View File

@ -1,5 +1,6 @@
package com.xkrs.controller; package com.xkrs.controller;
import com.xkrs.model.qo.ExportSpecExcelQo;
import com.xkrs.model.qo.QcSpecQoDelete; import com.xkrs.model.qo.QcSpecQoDelete;
import com.xkrs.model.qo.QcSpecQoInsert; import com.xkrs.model.qo.QcSpecQoInsert;
import com.xkrs.model.qo.QcSpecQoUpdate; import com.xkrs.model.qo.QcSpecQoUpdate;
@ -58,9 +59,9 @@ public class QcSpecController {
/** /**
* 导出检验规格 * 导出检验规格
*/ */
@GetMapping("/exportSpecExcel") @PostMapping("/exportSpecExcel")
public String exportSpecExcel() { public String exportSpecExcel(@RequestBody ExportSpecExcelQo exportSpecExcelQo) throws Exception {
return qcSpecService.exportSpecExcel(); return qcSpecService.exportSpecExcel(exportSpecExcelQo);
} }
} }

View File

@ -1,15 +1,15 @@
package com.xkrs.model.qo; package com.xkrs.model.qo;
import com.xkrs.model.va.ExportExcelQoVa; import com.xkrs.model.va.ExportSourceExcelQoVa;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
public class ExportExcelQo { public class ExportSourceExcelQo {
/** /**
* IP端口 * IP端口
*/ */
@NotBlank(message = "{QcSourceEntity.host.blank}", groups = {ExportExcelQoVa.class}) @NotBlank(message = "{QcSourceEntity.host.blank}", groups = {ExportSourceExcelQoVa.class})
private String host; private String host;
/** /**
@ -42,7 +42,7 @@ public class ExportExcelQo {
*/ */
private String craftItemNo; private String craftItemNo;
public ExportExcelQo() { public ExportSourceExcelQo() {
} }
public String getHost() { public String getHost() {

View File

@ -0,0 +1,30 @@
package com.xkrs.model.qo;
import com.xkrs.model.va.ExportSpecExcelQoVa;
import javax.validation.constraints.NotBlank;
public class ExportSpecExcelQo {
/**
* IP端口
*/
@NotBlank(message = "{QcSpecEntity.host.blank}", groups = {ExportSpecExcelQoVa.class})
private String host;
public ExportSpecExcelQo() {
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
@Override
public String toString() {
return "ExportSpecExcelQo{" + "host='" + host + '\'' + '}';
}
}

View File

@ -1,4 +0,0 @@
package com.xkrs.model.va;
public interface ExportExcelQoVa {
}

View File

@ -0,0 +1,4 @@
package com.xkrs.model.va;
public interface ExportSourceExcelQoVa {
}

View File

@ -0,0 +1,4 @@
package com.xkrs.model.va;
public interface ExportSpecExcelQoVa {
}

View File

@ -1,6 +1,6 @@
package com.xkrs.service; package com.xkrs.service;
import com.xkrs.model.qo.ExportExcelQo; import com.xkrs.model.qo.ExportSourceExcelQo;
import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoDelete;
import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.model.qo.QcSourceQoInsert;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -25,6 +25,6 @@ public interface QcSourceService {
/** /**
* 导出Excel表格 * 导出Excel表格
*/ */
String exportExcel(ExportExcelQo exportExcelQo) throws Exception; String exportExcel(ExportSourceExcelQo exportSourceExcelQo) throws Exception;
} }

View File

@ -1,5 +1,6 @@
package com.xkrs.service; package com.xkrs.service;
import com.xkrs.model.qo.ExportSpecExcelQo;
import com.xkrs.model.qo.QcSpecQoDelete; import com.xkrs.model.qo.QcSpecQoDelete;
import com.xkrs.model.qo.QcSpecQoInsert; import com.xkrs.model.qo.QcSpecQoInsert;
import com.xkrs.model.qo.QcSpecQoUpdate; import com.xkrs.model.qo.QcSpecQoUpdate;
@ -35,6 +36,6 @@ public interface QcSpecService {
/** /**
* 导出检验规格 * 导出检验规格
*/ */
String exportSpecExcel(); String exportSpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception;
} }

View File

@ -9,7 +9,7 @@ import com.xkrs.model.entity.CraftItemEntity;
import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcItemEntity;
import com.xkrs.model.entity.QcSourceEntity; import com.xkrs.model.entity.QcSourceEntity;
import com.xkrs.model.entity.QcSpecEntity; import com.xkrs.model.entity.QcSpecEntity;
import com.xkrs.model.qo.ExportExcelQo; import com.xkrs.model.qo.ExportSourceExcelQo;
import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoDelete;
import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.model.qo.QcSourceQoInsert;
import com.xkrs.service.QcSourceService; import com.xkrs.service.QcSourceService;
@ -260,14 +260,14 @@ public class QcSourceServiceImpl implements QcSourceService {
* 导出Excel表格 * 导出Excel表格
*/ */
@Override @Override
public String exportExcel(ExportExcelQo exportExcelQo) throws Exception { public String exportExcel(ExportSourceExcelQo exportSourceExcelQo) throws Exception {
String host = exportExcelQo.getHost(); String host = exportSourceExcelQo.getHost();
String finalBatchNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getBatchNo()).trim(); String finalBatchNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getBatchNo()).trim();
String finalMachineNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getMachineNo()).trim(); String finalMachineNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMachineNo()).trim();
String finalMaterialNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getMaterialNo()).trim(); String finalMaterialNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMaterialNo()).trim();
String finalMouldNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getMouldNo()).trim(); String finalMouldNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getMouldNo()).trim();
String finalVarietyNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getVarietyNo()).trim(); String finalVarietyNo = LocalStringUtils.formatEmptyValue(exportSourceExcelQo.getVarietyNo()).trim();
String craftItemNo = exportExcelQo.getCraftItemNo(); String craftItemNo = exportSourceExcelQo.getCraftItemNo();
List<CraftItemEntity> craftItemList = craftItemDao.findAll(); List<CraftItemEntity> craftItemList = craftItemDao.findAll();
if (craftItemList.isEmpty()) { if (craftItemList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale);
@ -339,7 +339,7 @@ public class QcSourceServiceImpl implements QcSourceService {
//创建XSSFWorkbook对象(excel的文档对象) //创建XSSFWorkbook对象(excel的文档对象)
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单 //建立新的sheet对象excel的表单
XSSFSheet sheet = workbook.createSheet("测量数据表"); XSSFSheet sheet = workbook.createSheet("检验数据表");
XSSFCellStyle highCellStyle = workbook.createCellStyle(); XSSFCellStyle highCellStyle = workbook.createCellStyle();
highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
@ -367,7 +367,7 @@ public class QcSourceServiceImpl implements QcSourceService {
//创建单元格excel的单元格参数为列索引可以是0255之间的任何一个 //创建单元格excel的单元格参数为列索引可以是0255之间的任何一个
XSSFCell titleCell = titleRow.createCell(0); XSSFCell titleCell = titleRow.createCell(0);
//设置单元格内容 //设置单元格内容
titleCell.setCellValue("测量数据导出表"); titleCell.setCellValue("检验数据导出表");
titleCell.setCellStyle(normalCellStyle); titleCell.setCellStyle(normalCellStyle);
//合并单元格CellRangeAddress构造参数依次表示起始行截至行起始列 截至列 //合并单元格CellRangeAddress构造参数依次表示起始行截至行起始列 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, dataListList.get(0).size() - 1)); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, dataListList.get(0).size() - 1));
@ -394,6 +394,7 @@ public class QcSourceServiceImpl implements QcSourceService {
} }
//输出Excel文件 //输出Excel文件
File dirFile = new File("/home/sxy/server/industrial_measurement/excel/"); File dirFile = new File("/home/sxy/server/industrial_measurement/excel/");
// File dirFile = new File("/Users/liuchengqian/Desktop/DaJiang/");
if (!dirFile.exists()) { if (!dirFile.exists()) {
dirFile.mkdirs(); dirFile.mkdirs();
} }

View File

@ -8,6 +8,7 @@ import com.xkrs.model.bean.ReadSpecHeadBean;
import com.xkrs.model.entity.CraftItemEntity; import com.xkrs.model.entity.CraftItemEntity;
import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcItemEntity;
import com.xkrs.model.entity.QcSpecEntity; import com.xkrs.model.entity.QcSpecEntity;
import com.xkrs.model.qo.ExportSpecExcelQo;
import com.xkrs.model.qo.QcSpecQoDelete; import com.xkrs.model.qo.QcSpecQoDelete;
import com.xkrs.model.qo.QcSpecQoInsert; import com.xkrs.model.qo.QcSpecQoInsert;
import com.xkrs.model.qo.QcSpecQoUpdate; import com.xkrs.model.qo.QcSpecQoUpdate;
@ -15,17 +16,21 @@ import com.xkrs.service.QcSpecService;
import com.xkrs.util.LocalDateUtils; import com.xkrs.util.LocalDateUtils;
import com.xkrs.util.LocalStringUtils; import com.xkrs.util.LocalStringUtils;
import org.apache.http.util.TextUtils; import org.apache.http.util.TextUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*; import java.util.*;
import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject;
@ -156,7 +161,7 @@ public class QcSpecServiceImpl implements QcSpecService {
@Override @Override
public String importSpecExcel(MultipartFile specExcel) { public String importSpecExcel(MultipartFile specExcel) {
try { try {
//1读取测量规格数据表文件 //1读取检验规格数据表文件
XSSFWorkbook workbook = new XSSFWorkbook(specExcel.getInputStream()); XSSFWorkbook workbook = new XSSFWorkbook(specExcel.getInputStream());
//2获取工作表对象 //2获取工作表对象
XSSFSheet sheet = workbook.getSheetAt(0); XSSFSheet sheet = workbook.getSheetAt(0);
@ -213,7 +218,7 @@ public class QcSpecServiceImpl implements QcSpecService {
} }
if (appearIndexList.size() > 1) { if (appearIndexList.size() > 1) {
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
response.append("测量规格表中:机种号、工艺项目、检验项目三项存在全部相同项,行数:"); response.append("检验规格表中:机种号、工艺项目、检验项目三项存在全部相同项,行数:");
for (Integer appearIndex : appearIndexList) { for (Integer appearIndex : appearIndexList) {
response.append("").append(appearIndex + 2).append("行、"); response.append("").append(appearIndex + 2).append("行、");
} }
@ -255,17 +260,17 @@ public class QcSpecServiceImpl implements QcSpecService {
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
response.append("导入成功"); response.append("导入成功");
if (insertCount > 0) { if (insertCount > 0) {
response.append(",新增").append(insertCount).append("测量规格"); response.append(",新增").append(insertCount).append("检验规格");
} }
if (updateCount > 0) { if (updateCount > 0) {
response.append(",更新").append(updateCount).append("测量规格"); response.append(",更新").append(updateCount).append("检验规格");
} }
response.append(""); response.append("");
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, response.toString(), locale); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, response.toString(), locale);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "测量规格数据导入失败!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "检验规格数据导入失败!", locale);
} }
private List<Integer> checkAppearIndex(List<List<String>> sheetDataList, String varietyNo, String craftItemNo, String qcItemNo) { private List<Integer> checkAppearIndex(List<List<String>> sheetDataList, String varietyNo, String craftItemNo, String qcItemNo) {
@ -326,9 +331,76 @@ public class QcSpecServiceImpl implements QcSpecService {
} }
/**
* 导出检验规格
*/
@Override @Override
public String exportSpecExcel() { public String exportSpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception {
return null;
List<QcSpecEntity> qcSpecList = qcSpecDao.findAll(Sort.by(Sort.Direction.ASC, "createTime"));
if (qcSpecList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "无检验规格数据可导出,请先添加检验规格!", locale);
}
List<List<String>> dataListList = new ArrayList<>();
dataListList.add(QC_SPEC_HEAD);
for (QcSpecEntity qcSpecEntity : qcSpecList) {
List<String> dataList = new ArrayList<>();
dataList.add(qcSpecEntity.getVarietyNo());
Optional<CraftItemEntity> craftItemOptional = craftItemDao.findByCraftItemNo(qcSpecEntity.getCraftItemNo());
if (craftItemOptional.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "检验规格数据导出失败code=01", locale);
}
dataList.add(craftItemOptional.get().getCraftItemName());
Optional<QcItemEntity> qcItemOptional = qcItemDao.findByQcItemNo(qcSpecEntity.getQcItemNo());
if (qcItemOptional.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "检验规格数据导出失败code=02", locale);
}
dataList.add(qcItemOptional.get().getQcItemName());
dataList.add(qcSpecEntity.getMax());
dataList.add(qcSpecEntity.getMin());
dataList.add(qcSpecEntity.getMethod());
dataListList.add(dataList);
}
//创建XSSFWorkbook对象(excel的文档对象)
XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单
XSSFSheet sheet = workbook.createSheet("检验规格表");
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);
int startRowNum = 0;
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);
String data = dataList.get(j);
cell.setCellValue(data);
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();
String host = exportSpecExcelQo.getHost();
String finalHost = host.endsWith("/") ? host : host + "/";
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + "excel/" + excelFileName, locale);
} }
} }