diff --git a/src/main/java/com/xkrs/controller/QcSourceController.java b/src/main/java/com/xkrs/controller/QcSourceController.java index cc8bbd2..d8b2373 100644 --- a/src/main/java/com/xkrs/controller/QcSourceController.java +++ b/src/main/java/com/xkrs/controller/QcSourceController.java @@ -1,6 +1,6 @@ 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.QcSourceQoInsert; import com.xkrs.service.QcSourceService; @@ -51,8 +51,8 @@ public class QcSourceController { * 导出Excel表格 */ @PostMapping("/exportExcel") - public String exportExcel(@RequestBody ExportExcelQo exportExcelQo) throws Exception { - return qcSourceService.exportExcel(exportExcelQo); + public String exportExcel(@RequestBody ExportSourceExcelQo exportSourceExcelQo) throws Exception { + return qcSourceService.exportExcel(exportSourceExcelQo); } } diff --git a/src/main/java/com/xkrs/controller/QcSpecController.java b/src/main/java/com/xkrs/controller/QcSpecController.java index 9578b2b..20a5952 100644 --- a/src/main/java/com/xkrs/controller/QcSpecController.java +++ b/src/main/java/com/xkrs/controller/QcSpecController.java @@ -1,5 +1,6 @@ package com.xkrs.controller; +import com.xkrs.model.qo.ExportSpecExcelQo; import com.xkrs.model.qo.QcSpecQoDelete; import com.xkrs.model.qo.QcSpecQoInsert; import com.xkrs.model.qo.QcSpecQoUpdate; @@ -58,9 +59,9 @@ public class QcSpecController { /** * 导出检验规格 */ - @GetMapping("/exportSpecExcel") - public String exportSpecExcel() { - return qcSpecService.exportSpecExcel(); + @PostMapping("/exportSpecExcel") + public String exportSpecExcel(@RequestBody ExportSpecExcelQo exportSpecExcelQo) throws Exception { + return qcSpecService.exportSpecExcel(exportSpecExcelQo); } } diff --git a/src/main/java/com/xkrs/model/qo/ExportExcelQo.java b/src/main/java/com/xkrs/model/qo/ExportSourceExcelQo.java similarity index 93% rename from src/main/java/com/xkrs/model/qo/ExportExcelQo.java rename to src/main/java/com/xkrs/model/qo/ExportSourceExcelQo.java index c5b97c7..5cfb71d 100644 --- a/src/main/java/com/xkrs/model/qo/ExportExcelQo.java +++ b/src/main/java/com/xkrs/model/qo/ExportSourceExcelQo.java @@ -1,15 +1,15 @@ package com.xkrs.model.qo; -import com.xkrs.model.va.ExportExcelQoVa; +import com.xkrs.model.va.ExportSourceExcelQoVa; import javax.validation.constraints.NotBlank; -public class ExportExcelQo { +public class ExportSourceExcelQo { /** * IP端口 */ - @NotBlank(message = "{QcSourceEntity.host.blank}", groups = {ExportExcelQoVa.class}) + @NotBlank(message = "{QcSourceEntity.host.blank}", groups = {ExportSourceExcelQoVa.class}) private String host; /** @@ -42,7 +42,7 @@ public class ExportExcelQo { */ private String craftItemNo; - public ExportExcelQo() { + public ExportSourceExcelQo() { } public String getHost() { diff --git a/src/main/java/com/xkrs/model/qo/ExportSpecExcelQo.java b/src/main/java/com/xkrs/model/qo/ExportSpecExcelQo.java new file mode 100644 index 0000000..0c94d2b --- /dev/null +++ b/src/main/java/com/xkrs/model/qo/ExportSpecExcelQo.java @@ -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 + '\'' + '}'; + } +} diff --git a/src/main/java/com/xkrs/model/va/ExportExcelQoVa.java b/src/main/java/com/xkrs/model/va/ExportExcelQoVa.java deleted file mode 100644 index 90fb9db..0000000 --- a/src/main/java/com/xkrs/model/va/ExportExcelQoVa.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.xkrs.model.va; - -public interface ExportExcelQoVa { -} diff --git a/src/main/java/com/xkrs/model/va/ExportSourceExcelQoVa.java b/src/main/java/com/xkrs/model/va/ExportSourceExcelQoVa.java new file mode 100644 index 0000000..1c619b2 --- /dev/null +++ b/src/main/java/com/xkrs/model/va/ExportSourceExcelQoVa.java @@ -0,0 +1,4 @@ +package com.xkrs.model.va; + +public interface ExportSourceExcelQoVa { +} diff --git a/src/main/java/com/xkrs/model/va/ExportSpecExcelQoVa.java b/src/main/java/com/xkrs/model/va/ExportSpecExcelQoVa.java new file mode 100644 index 0000000..e331835 --- /dev/null +++ b/src/main/java/com/xkrs/model/va/ExportSpecExcelQoVa.java @@ -0,0 +1,4 @@ +package com.xkrs.model.va; + +public interface ExportSpecExcelQoVa { +} diff --git a/src/main/java/com/xkrs/service/QcSourceService.java b/src/main/java/com/xkrs/service/QcSourceService.java index bf92211..5728f20 100644 --- a/src/main/java/com/xkrs/service/QcSourceService.java +++ b/src/main/java/com/xkrs/service/QcSourceService.java @@ -1,6 +1,6 @@ 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.QcSourceQoInsert; import org.springframework.web.multipart.MultipartFile; @@ -25,6 +25,6 @@ public interface QcSourceService { /** * 导出Excel表格 */ - String exportExcel(ExportExcelQo exportExcelQo) throws Exception; + String exportExcel(ExportSourceExcelQo exportSourceExcelQo) throws Exception; } diff --git a/src/main/java/com/xkrs/service/QcSpecService.java b/src/main/java/com/xkrs/service/QcSpecService.java index 727c974..315e39b 100644 --- a/src/main/java/com/xkrs/service/QcSpecService.java +++ b/src/main/java/com/xkrs/service/QcSpecService.java @@ -1,5 +1,6 @@ package com.xkrs.service; +import com.xkrs.model.qo.ExportSpecExcelQo; import com.xkrs.model.qo.QcSpecQoDelete; import com.xkrs.model.qo.QcSpecQoInsert; import com.xkrs.model.qo.QcSpecQoUpdate; @@ -35,6 +36,6 @@ public interface QcSpecService { /** * 导出检验规格 */ - String exportSpecExcel(); + String exportSpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception; } diff --git a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java index e06710d..2868739 100644 --- a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java @@ -9,7 +9,7 @@ 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.ExportExcelQo; +import com.xkrs.model.qo.ExportSourceExcelQo; import com.xkrs.model.qo.QcSourceQoDelete; import com.xkrs.model.qo.QcSourceQoInsert; import com.xkrs.service.QcSourceService; @@ -260,14 +260,14 @@ public class QcSourceServiceImpl implements QcSourceService { * 导出Excel表格 */ @Override - public String exportExcel(ExportExcelQo exportExcelQo) throws Exception { - String host = exportExcelQo.getHost(); - String finalBatchNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getBatchNo()).trim(); - String finalMachineNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getMachineNo()).trim(); - String finalMaterialNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getMaterialNo()).trim(); - String finalMouldNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getMouldNo()).trim(); - String finalVarietyNo = LocalStringUtils.formatEmptyValue(exportExcelQo.getVarietyNo()).trim(); - String craftItemNo = exportExcelQo.getCraftItemNo(); + 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(); List craftItemList = craftItemDao.findAll(); if (craftItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); @@ -339,7 +339,7 @@ public class QcSourceServiceImpl implements QcSourceService { //创建XSSFWorkbook对象(excel的文档对象) XSSFWorkbook workbook = new XSSFWorkbook(); //建立新的sheet对象(excel的表单) - XSSFSheet sheet = workbook.createSheet("测量数据表"); + XSSFSheet sheet = workbook.createSheet("检验数据表"); XSSFCellStyle highCellStyle = workbook.createCellStyle(); highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); @@ -367,7 +367,7 @@ public class QcSourceServiceImpl implements QcSourceService { //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 XSSFCell titleCell = titleRow.createCell(0); //设置单元格内容 - titleCell.setCellValue("测量数据导出表"); + titleCell.setCellValue("检验数据导出表"); titleCell.setCellStyle(normalCellStyle); //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, dataListList.get(0).size() - 1)); @@ -394,6 +394,7 @@ public class QcSourceServiceImpl implements QcSourceService { } //输出Excel文件 File dirFile = new File("/home/sxy/server/industrial_measurement/excel/"); +// File dirFile = new File("/Users/liuchengqian/Desktop/DaJiang/"); if (!dirFile.exists()) { dirFile.mkdirs(); } diff --git a/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java b/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java index aa85c63..8657972 100644 --- a/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSpecServiceImpl.java @@ -8,6 +8,7 @@ import com.xkrs.model.bean.ReadSpecHeadBean; import com.xkrs.model.entity.CraftItemEntity; import com.xkrs.model.entity.QcItemEntity; import com.xkrs.model.entity.QcSpecEntity; +import com.xkrs.model.qo.ExportSpecExcelQo; import com.xkrs.model.qo.QcSpecQoDelete; import com.xkrs.model.qo.QcSpecQoInsert; import com.xkrs.model.qo.QcSpecQoUpdate; @@ -15,17 +16,21 @@ import com.xkrs.service.QcSpecService; import com.xkrs.util.LocalDateUtils; import com.xkrs.util.LocalStringUtils; 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.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xssf.usermodel.*; import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.transaction.Transactional; +import java.io.File; +import java.io.FileOutputStream; import java.util.*; import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; @@ -156,7 +161,7 @@ public class QcSpecServiceImpl implements QcSpecService { @Override public String importSpecExcel(MultipartFile specExcel) { try { - //1、读取测量规格数据表文件 + //1、读取检验规格数据表文件 XSSFWorkbook workbook = new XSSFWorkbook(specExcel.getInputStream()); //2、获取工作表对象 XSSFSheet sheet = workbook.getSheetAt(0); @@ -213,7 +218,7 @@ public class QcSpecServiceImpl implements QcSpecService { } if (appearIndexList.size() > 1) { StringBuilder response = new StringBuilder(); - response.append("测量规格表中:机种号、工艺项目、检验项目三项存在全部相同项,行数:"); + response.append("检验规格表中:机种号、工艺项目、检验项目三项存在全部相同项,行数:"); for (Integer appearIndex : appearIndexList) { response.append("第").append(appearIndex + 2).append("行、"); } @@ -255,17 +260,17 @@ public class QcSpecServiceImpl implements QcSpecService { StringBuilder response = new StringBuilder(); response.append("导入成功"); if (insertCount > 0) { - response.append(",新增").append(insertCount).append("条测量规格"); + response.append(",新增").append(insertCount).append("条检验规格"); } if (updateCount > 0) { - response.append(",更新").append(updateCount).append("条测量规格"); + response.append(",更新").append(updateCount).append("条检验规格"); } response.append("。"); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, response.toString(), locale); } catch (Exception e) { e.printStackTrace(); } - return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "测量规格数据导入失败!", locale); + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "检验规格数据导入失败!", locale); } private List checkAppearIndex(List> sheetDataList, String varietyNo, String craftItemNo, String qcItemNo) { @@ -326,9 +331,76 @@ public class QcSpecServiceImpl implements QcSpecService { } - + /** + * 导出检验规格 + */ @Override - public String exportSpecExcel() { - return null; + public String exportSpecExcel(ExportSpecExcelQo exportSpecExcelQo) throws Exception { + + List qcSpecList = qcSpecDao.findAll(Sort.by(Sort.Direction.ASC, "createTime")); + if (qcSpecList.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "无检验规格数据可导出,请先添加检验规格!", locale); + } + + List> dataListList = new ArrayList<>(); + dataListList.add(QC_SPEC_HEAD); + for (QcSpecEntity qcSpecEntity : qcSpecList) { + List dataList = new ArrayList<>(); + dataList.add(qcSpecEntity.getVarietyNo()); + Optional craftItemOptional = craftItemDao.findByCraftItemNo(qcSpecEntity.getCraftItemNo()); + if (craftItemOptional.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "检验规格数据导出失败(code=01)!", locale); + } + dataList.add(craftItemOptional.get().getCraftItemName()); + Optional 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 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); } }