From 6e62f0de5a81bb448ec1c7793ec808a03dbcaf2e Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Thu, 17 Mar 2022 11:42:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BAExcel=E8=A1=A8=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/controller/FileController.java | 122 ++++++++---------- .../xkrs/controller/QcSourceController.java | 4 +- .../com/xkrs/service/QcSourceService.java | 2 +- .../service/impl/QcSourceServiceImpl.java | 24 ++-- 4 files changed, 70 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/xkrs/controller/FileController.java b/src/main/java/com/xkrs/controller/FileController.java index cd82dc6..0f85ac3 100644 --- a/src/main/java/com/xkrs/controller/FileController.java +++ b/src/main/java/com/xkrs/controller/FileController.java @@ -1,24 +1,6 @@ package com.xkrs.controller; -import com.xkrs.dao.FileDao; -import com.xkrs.encapsulation.PromptMessageEnum; -import com.xkrs.model.qo.FileQo; -import com.xkrs.service.FileService; -import com.xkrs.util.ExcelUploadUtil; -import org.apache.poi.openxml4j.exceptions.InvalidFormatException; -import org.springframework.context.i18n.LocaleContextHolder; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import javax.transaction.Transactional; -import java.io.IOException; -import java.util.Locale; - -import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObject; /** * @Author: XinYi Song @@ -27,56 +9,56 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje @RestController public class FileController { - @Resource - private FileService fileService; - - @Resource - private FileDao fileDao; - - /** - * 上传模板信息 - * - * @param fileQo - * @param fileExcel - * @return - * @throws IOException - */ - @PostMapping("/insertFileExcel") - public String insertFileExcel(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { - return fileService.insertFileExcel(fileQo, fileExcel); - } - - /** - * 修改上传的模板信息 - * - * @param fileQo - * @param fileExcel - * @return - * @throws IOException - */ - @PostMapping("/updateFileUploadPath") - @Transactional(rollbackOn = Exception.class) - public String updateFileUploadPath(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { - String lotNo = fileQo.getLotNo(); - String machineNo = fileQo.getMachineNo(); - String materialNo = fileQo.getMaterialNo(); - String modelNo = fileQo.getModelNo(); - String machineTypeNo = fileQo.getMachineTypeNo(); - String craftItemName = fileQo.getCraftItemName(); - Locale locale = LocaleContextHolder.getLocale(); - String file = ExcelUploadUtil.memoryFile(fileExcel, 1); - fileDao.updateFileUploadPath(lotNo, machineNo, materialNo, modelNo, machineTypeNo, craftItemName, file); - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功!", locale); - } - - /** - * 导出excel - * - * @param fileQo - * @return - */ - @PostMapping("/exportExcel") - public String exportExcel(@RequestBody FileQo fileQo) throws IOException, InvalidFormatException { - return fileService.exportExcel(fileQo); - } +// @Resource +// private FileService fileService; +// +// @Resource +// private FileDao fileDao; +// +// /** +// * 上传模板信息 +// * +// * @param fileQo +// * @param fileExcel +// * @return +// * @throws IOException +// */ +// @PostMapping("/insertFileExcel") +// public String insertFileExcel(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { +// return fileService.insertFileExcel(fileQo, fileExcel); +// } +// +// /** +// * 修改上传的模板信息 +// * +// * @param fileQo +// * @param fileExcel +// * @return +// * @throws IOException +// */ +// @PostMapping("/updateFileUploadPath") +// @Transactional(rollbackOn = Exception.class) +// public String updateFileUploadPath(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { +// String lotNo = fileQo.getLotNo(); +// String machineNo = fileQo.getMachineNo(); +// String materialNo = fileQo.getMaterialNo(); +// String modelNo = fileQo.getModelNo(); +// String machineTypeNo = fileQo.getMachineTypeNo(); +// String craftItemName = fileQo.getCraftItemName(); +// Locale locale = LocaleContextHolder.getLocale(); +// String file = ExcelUploadUtil.memoryFile(fileExcel, 1); +// fileDao.updateFileUploadPath(lotNo, machineNo, materialNo, modelNo, machineTypeNo, craftItemName, file); +// return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功!", locale); +// } +// +// /** +// * 导出excel +// * +// * @param fileQo +// * @return +// */ +// @PostMapping("/exportExcel") +// public String exportExcel(@RequestBody FileQo fileQo) throws IOException, InvalidFormatException { +// return fileService.exportExcel(fileQo); +// } } diff --git a/src/main/java/com/xkrs/controller/QcSourceController.java b/src/main/java/com/xkrs/controller/QcSourceController.java index 524ee2e..e0e4231 100644 --- a/src/main/java/com/xkrs/controller/QcSourceController.java +++ b/src/main/java/com/xkrs/controller/QcSourceController.java @@ -42,8 +42,8 @@ public class QcSourceController { * 导出Excel表格 */ @GetMapping("/exportExcel") - public String exportExcel(@RequestParam(required = false, value = "batchNo") String batchNo, @RequestParam(required = false, value = "machineNo") String machineNo, @RequestParam(required = false, value = "materialNo") String materialNo, @RequestParam(required = false, value = "mouldNo") String mouldNo, @RequestParam(required = false, value = "varietyNo") String varietyNo, @RequestParam(required = false, value = "craftItemNo") String craftItemNo) throws Exception { - return qcSourceService.exportExcel(batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo); + public String exportExcel(@RequestParam(value = "host") String host, @RequestParam(required = false, value = "batchNo") String batchNo, @RequestParam(required = false, value = "machineNo") String machineNo, @RequestParam(required = false, value = "materialNo") String materialNo, @RequestParam(required = false, value = "mouldNo") String mouldNo, @RequestParam(required = false, value = "varietyNo") String varietyNo, @RequestParam(required = false, value = "craftItemNo") String craftItemNo) throws Exception { + return qcSourceService.exportExcel(host, batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo); } } diff --git a/src/main/java/com/xkrs/service/QcSourceService.java b/src/main/java/com/xkrs/service/QcSourceService.java index 17d56e7..c386f32 100644 --- a/src/main/java/com/xkrs/service/QcSourceService.java +++ b/src/main/java/com/xkrs/service/QcSourceService.java @@ -24,6 +24,6 @@ public interface QcSourceService { /** * 导出Excel表格 */ - String exportExcel(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception; + String exportExcel(String host, String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) 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 b580711..cc9fcab 100644 --- a/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/QcSourceServiceImpl.java @@ -27,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.transaction.Transactional; +import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; @@ -244,12 +245,11 @@ public class QcSourceServiceImpl implements QcSourceService { return null; } - /** * 导出Excel表格 */ @Override - public String exportExcel(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception { + public String exportExcel(String host, String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception { List craftItemList = craftItemDao.findAll(); if (craftItemList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); @@ -260,18 +260,19 @@ public class QcSourceServiceImpl implements QcSourceService { } List> sourceList = qcSourceDao.queryQcSource(LocalStringUtils.formatEmptyValue(batchNo), LocalStringUtils.formatEmptyValue(machineNo), LocalStringUtils.formatEmptyValue(materialNo), LocalStringUtils.formatEmptyValue(mouldNo), LocalStringUtils.formatEmptyValue(varietyNo), LocalStringUtils.formatEmptyValue(craftItemNo)); List> resultList = generateResultList(sourceList); - List> formattedResultList = formatResultList(resultList, qcItemList); + String finalHost = host.endsWith("/") ? host : host + "/"; + List> formattedResultList = formatResultList(finalHost, resultList, qcItemList); if (formattedResultList.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); } - doExportExcel(formattedResultList); - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "导出成功!", locale); + String excelPath = doExportExcel(formattedResultList); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, finalHost + excelPath, locale); } /** * 格式化结果集数据 */ - private List> formatResultList(List> resultList, List qcItemList) { + private List> formatResultList(String host, List> resultList, List qcItemList) { List> formatResultList = new ArrayList<>(); if (resultList == null || resultList.isEmpty()) { return formatResultList; @@ -295,7 +296,7 @@ public class QcSourceServiceImpl implements QcSourceService { dataList.add(value); } else if (Constant.QC_ITEM_TYPE_PICTURE.equals(entity.getQcItemType())) { if (value.startsWith("picture")) { - dataList.add("http://118.24.27.47:2088/" + value); + dataList.add(host + value); } else { dataList.add(value); } @@ -311,7 +312,7 @@ public class QcSourceServiceImpl implements QcSourceService { /** * 将格式化后的结果集数据导出为Excel文件 */ - private void doExportExcel(List> dataListList) throws Exception { + private String doExportExcel(List> dataListList) throws Exception { //创建XSSFWorkbook对象(excel的文档对象) XSSFWorkbook workbook = new XSSFWorkbook(); //建立新的sheet对象(excel的表单) @@ -369,10 +370,15 @@ public class QcSourceServiceImpl implements QcSourceService { } } //输出Excel文件 + File dirFile = new File("/home/sxy/server/industrial_measurement/excel/"); + if (!dirFile.exists()) { + dirFile.mkdirs(); + } String excelFileName = System.currentTimeMillis() + ".xlsx"; - FileOutputStream output = new FileOutputStream("/Users/liuchengqian/Desktop/DaJiang/excel/" + excelFileName); + FileOutputStream output = new FileOutputStream(new File(dirFile, excelFileName)); workbook.write(output); output.flush(); + return "excel/" + excelFileName; }