导出Excel表格

This commit is contained in:
liuchengqian 2022-03-17 11:42:19 +08:00
parent c452fbc650
commit 6e62f0de5a
4 changed files with 70 additions and 82 deletions

View File

@ -1,24 +1,6 @@
package com.xkrs.controller; 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.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 * @Author: XinYi Song
@ -27,56 +9,56 @@ import static com.xkrs.encapsulation.OutputEncapsulation.outputEncapsulationObje
@RestController @RestController
public class FileController { public class FileController {
@Resource // @Resource
private FileService fileService; // private FileService fileService;
//
@Resource // @Resource
private FileDao fileDao; // private FileDao fileDao;
//
/** // /**
* 上传模板信息 // * 上传模板信息
* // *
* @param fileQo // * @param fileQo
* @param fileExcel // * @param fileExcel
* @return // * @return
* @throws IOException // * @throws IOException
*/ // */
@PostMapping("/insertFileExcel") // @PostMapping("/insertFileExcel")
public String insertFileExcel(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { // public String insertFileExcel(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException {
return fileService.insertFileExcel(fileQo, fileExcel); // return fileService.insertFileExcel(fileQo, fileExcel);
} // }
//
/** // /**
* 修改上传的模板信息 // * 修改上传的模板信息
* // *
* @param fileQo // * @param fileQo
* @param fileExcel // * @param fileExcel
* @return // * @return
* @throws IOException // * @throws IOException
*/ // */
@PostMapping("/updateFileUploadPath") // @PostMapping("/updateFileUploadPath")
@Transactional(rollbackOn = Exception.class) // @Transactional(rollbackOn = Exception.class)
public String updateFileUploadPath(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException { // public String updateFileUploadPath(FileQo fileQo, @RequestParam("fileExcel") MultipartFile fileExcel) throws IOException {
String lotNo = fileQo.getLotNo(); // String lotNo = fileQo.getLotNo();
String machineNo = fileQo.getMachineNo(); // String machineNo = fileQo.getMachineNo();
String materialNo = fileQo.getMaterialNo(); // String materialNo = fileQo.getMaterialNo();
String modelNo = fileQo.getModelNo(); // String modelNo = fileQo.getModelNo();
String machineTypeNo = fileQo.getMachineTypeNo(); // String machineTypeNo = fileQo.getMachineTypeNo();
String craftItemName = fileQo.getCraftItemName(); // String craftItemName = fileQo.getCraftItemName();
Locale locale = LocaleContextHolder.getLocale(); // Locale locale = LocaleContextHolder.getLocale();
String file = ExcelUploadUtil.memoryFile(fileExcel, 1); // String file = ExcelUploadUtil.memoryFile(fileExcel, 1);
fileDao.updateFileUploadPath(lotNo, machineNo, materialNo, modelNo, machineTypeNo, craftItemName, file); // fileDao.updateFileUploadPath(lotNo, machineNo, materialNo, modelNo, machineTypeNo, craftItemName, file);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功!", locale); // return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功!", locale);
} // }
//
/** // /**
* 导出excel // * 导出excel
* // *
* @param fileQo // * @param fileQo
* @return // * @return
*/ // */
@PostMapping("/exportExcel") // @PostMapping("/exportExcel")
public String exportExcel(@RequestBody FileQo fileQo) throws IOException, InvalidFormatException { // public String exportExcel(@RequestBody FileQo fileQo) throws IOException, InvalidFormatException {
return fileService.exportExcel(fileQo); // return fileService.exportExcel(fileQo);
} // }
} }

View File

@ -42,8 +42,8 @@ public class QcSourceController {
* 导出Excel表格 * 导出Excel表格
*/ */
@GetMapping("/exportExcel") @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 { 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(batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo); return qcSourceService.exportExcel(host, batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo);
} }
} }

View File

@ -24,6 +24,6 @@ public interface QcSourceService {
/** /**
* 导出Excel表格 * 导出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;
} }

View File

@ -27,6 +27,7 @@ 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.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
@ -244,12 +245,11 @@ public class QcSourceServiceImpl implements QcSourceService {
return null; return null;
} }
/** /**
* 导出Excel表格 * 导出Excel表格
*/ */
@Override @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(); List<CraftItemEntity> craftItemList = craftItemDao.findAll();
if (craftItemList.isEmpty()) { if (craftItemList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); 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>> 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<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()) { if (formattedResultList.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale);
} }
doExportExcel(formattedResultList); String excelPath = doExportExcel(formattedResultList);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "导出成功!", locale); 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<>(); List<List<String>> formatResultList = new ArrayList<>();
if (resultList == null || resultList.isEmpty()) { if (resultList == null || resultList.isEmpty()) {
return formatResultList; return formatResultList;
@ -295,7 +296,7 @@ public class QcSourceServiceImpl implements QcSourceService {
dataList.add(value); dataList.add(value);
} else if (Constant.QC_ITEM_TYPE_PICTURE.equals(entity.getQcItemType())) { } else if (Constant.QC_ITEM_TYPE_PICTURE.equals(entity.getQcItemType())) {
if (value.startsWith("picture")) { if (value.startsWith("picture")) {
dataList.add("http://118.24.27.47:2088/" + value); dataList.add(host + value);
} else { } else {
dataList.add(value); dataList.add(value);
} }
@ -311,7 +312,7 @@ public class QcSourceServiceImpl implements QcSourceService {
/** /**
* 将格式化后的结果集数据导出为Excel文件 * 将格式化后的结果集数据导出为Excel文件
*/ */
private void doExportExcel(List<List<String>> dataListList) throws Exception { private String doExportExcel(List<List<String>> dataListList) throws Exception {
//创建XSSFWorkbook对象(excel的文档对象) //创建XSSFWorkbook对象(excel的文档对象)
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
//建立新的sheet对象excel的表单 //建立新的sheet对象excel的表单
@ -369,10 +370,15 @@ public class QcSourceServiceImpl implements QcSourceService {
} }
} }
//输出Excel文件 //输出Excel文件
File dirFile = new File("/home/sxy/server/industrial_measurement/excel/");
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String excelFileName = System.currentTimeMillis() + ".xlsx"; 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); workbook.write(output);
output.flush(); output.flush();
return "excel/" + excelFileName;
} }