导出Excel表格
This commit is contained in:
		| @@ -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); | ||||
| //    } | ||||
| } | ||||
|   | ||||
| @@ -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); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -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<CraftItemEntity> craftItemList = craftItemDao.findAll(); | ||||
|         if (craftItemList.isEmpty()) { | ||||
|             return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); | ||||
| @@ -260,18 +260,19 @@ public class QcSourceServiceImpl implements QcSourceService { | ||||
|         } | ||||
|         List<Map<String, Object>> sourceList = qcSourceDao.queryQcSource(LocalStringUtils.formatEmptyValue(batchNo), LocalStringUtils.formatEmptyValue(machineNo), LocalStringUtils.formatEmptyValue(materialNo), LocalStringUtils.formatEmptyValue(mouldNo), LocalStringUtils.formatEmptyValue(varietyNo), LocalStringUtils.formatEmptyValue(craftItemNo)); | ||||
|         List<Map<String, Object>> resultList = generateResultList(sourceList); | ||||
|         List<List<String>> formattedResultList = formatResultList(resultList, qcItemList); | ||||
|         String finalHost = host.endsWith("/") ? host : host + "/"; | ||||
|         List<List<String>> 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<List<String>> formatResultList(List<Map<String, Object>> resultList, List<QcItemEntity> qcItemList) { | ||||
|     private List<List<String>> formatResultList(String host, List<Map<String, Object>> resultList, List<QcItemEntity> qcItemList) { | ||||
|         List<List<String>> 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<List<String>> dataListList) throws Exception { | ||||
|     private String doExportExcel(List<List<String>> 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; | ||||
|     } | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user