导出Excel表格
This commit is contained in:
		| @@ -38,4 +38,12 @@ public class QcSourceController { | ||||
|         return qcSourceService.queryQcSource(batchNo, machineNo, materialNo, mouldNo, varietyNo, craftItemNo); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 导出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); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -21,4 +21,9 @@ public interface QcSourceService { | ||||
|      */ | ||||
|     String queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo); | ||||
|  | ||||
|     /** | ||||
|      * 导出Excel表格 | ||||
|      */ | ||||
|     String exportExcel(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) throws Exception; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -17,12 +17,17 @@ import com.xkrs.util.ExcelUploadUtil; | ||||
| 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.util.HSSFColor; | ||||
| import org.apache.poi.ss.util.CellRangeAddress; | ||||
| import org.apache.poi.xssf.usermodel.*; | ||||
| import org.springframework.context.i18n.LocaleContextHolder; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.web.multipart.MultipartFile; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
| import javax.transaction.Transactional; | ||||
| import java.io.FileOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.util.*; | ||||
|  | ||||
| @@ -45,6 +50,13 @@ public class QcSourceServiceImpl implements QcSourceService { | ||||
|     @Resource | ||||
|     private QcSpecDao qcSpecDao; | ||||
|  | ||||
|     /** | ||||
|      * 导出excel样式一(表头顺序) | ||||
|      * 批次号 机器号 物料号 模具号 机种号 | ||||
|      */ | ||||
|     List<String> EXCEL_HEAD_STYLE_ONE_NO = Arrays.asList("batchNo", "machineNo", "materialNo", "mouldNo", "varietyNo", "craftItemNo"); | ||||
|     List<String> EXCEL_HEAD_STYLE_ONE_NAME = Arrays.asList("批次", "机器", "物料", "模具", "机种", "工艺项目"); | ||||
|  | ||||
|     /** | ||||
|      * 添加检验数据 | ||||
|      */ | ||||
| @@ -127,6 +139,10 @@ public class QcSourceServiceImpl implements QcSourceService { | ||||
|      */ | ||||
|     @Override | ||||
|     public String queryQcSource(String batchNo, String machineNo, String materialNo, String mouldNo, String varietyNo, String craftItemNo) { | ||||
|         List<CraftItemEntity> craftItemList = craftItemDao.findAll(); | ||||
|         if (craftItemList.isEmpty()) { | ||||
|             return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); | ||||
|         } | ||||
|         List<QcItemEntity> qcItemList = qcItemDao.findAll(); | ||||
|         if (qcItemList.isEmpty()) { | ||||
|             return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale); | ||||
| @@ -163,12 +179,12 @@ public class QcSourceServiceImpl implements QcSourceService { | ||||
|             Map<String, Object> emptyRoom = findEmptyRoom(resultList, batchNo, qcItemNo); | ||||
|             if (emptyRoom == null) { | ||||
|                 Map<String, Object> newRoom = new HashMap<>(); | ||||
|                 newRoom.put("batchNo", batchNo); | ||||
|                 newRoom.put("machineNo", machineNo); | ||||
|                 newRoom.put("materialNo", materialNo); | ||||
|                 newRoom.put("mouldNo", mouldNo); | ||||
|                 newRoom.put("varietyNo", varietyNo); | ||||
|                 newRoom.put("craftItemNo", craftItemNo); | ||||
|                 newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(0), batchNo); | ||||
|                 newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(1), machineNo); | ||||
|                 newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(2), materialNo); | ||||
|                 newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(3), mouldNo); | ||||
|                 newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(4), varietyNo); | ||||
|                 newRoom.put(EXCEL_HEAD_STYLE_ONE_NO.get(5), craftItemNo); | ||||
|                 newRoom.put(qcItemNo, formatValue); | ||||
|                 resultList.add(newRoom); | ||||
|             } else { | ||||
| @@ -228,4 +244,136 @@ 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 { | ||||
|         List<CraftItemEntity> craftItemList = craftItemDao.findAll(); | ||||
|         if (craftItemList.isEmpty()) { | ||||
|             return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加工艺项目,请先添加工艺项目!", locale); | ||||
|         } | ||||
|         List<QcItemEntity> qcItemList = qcItemDao.findAll(); | ||||
|         if (qcItemList.isEmpty()) { | ||||
|             return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "未添加检验项目,请先添加检验项目!", locale); | ||||
|         } | ||||
|         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); | ||||
|         if (formattedResultList.isEmpty()) { | ||||
|             return outputEncapsulationObject(PromptMessageEnum.DATA_NONE, "暂时没有该检验数据的信息!", locale); | ||||
|         } | ||||
|         doExportExcel(formattedResultList); | ||||
|         return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "导出成功!", locale); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 格式化结果集数据 | ||||
|      */ | ||||
|     private List<List<String>> formatResultList(List<Map<String, Object>> resultList, List<QcItemEntity> qcItemList) { | ||||
|         List<List<String>> formatResultList = new ArrayList<>(); | ||||
|         if (resultList == null || resultList.isEmpty()) { | ||||
|             return formatResultList; | ||||
|         } | ||||
|         List<String> headList = new ArrayList<>(EXCEL_HEAD_STYLE_ONE_NAME); | ||||
|         for (QcItemEntity qcItemEntity : qcItemList) { | ||||
|             headList.add(qcItemEntity.getQcItemName()); | ||||
|         } | ||||
|         formatResultList.add(headList); | ||||
|         for (Map<String, Object> dataMap : resultList) { | ||||
|             List<String> dataList = new ArrayList<>(); | ||||
|             dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(0), "")); | ||||
|             dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(1), "")); | ||||
|             dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(2), "")); | ||||
|             dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(3), "")); | ||||
|             dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(4), "")); | ||||
|             dataList.add((String) dataMap.getOrDefault(EXCEL_HEAD_STYLE_ONE_NO.get(5), "")); | ||||
|             for (QcItemEntity entity : qcItemList) { | ||||
|                 String value = (String) dataMap.getOrDefault(entity.getQcItemNo(), ""); | ||||
|                 if (Constant.QC_ITEM_TYPE_STRING.equals(entity.getQcItemType())) { | ||||
|                     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); | ||||
|                     } else { | ||||
|                         dataList.add(value); | ||||
|                     } | ||||
|                 } else { | ||||
|                     dataList.add(value); | ||||
|                 } | ||||
|             } | ||||
|             formatResultList.add(dataList); | ||||
|         } | ||||
|         return formatResultList; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 将格式化后的结果集数据导出为Excel文件 | ||||
|      */ | ||||
|     private void doExportExcel(List<List<String>> dataListList) throws Exception { | ||||
|         //创建XSSFWorkbook对象(excel的文档对象) | ||||
|         XSSFWorkbook workbook = new XSSFWorkbook(); | ||||
|         //建立新的sheet对象(excel的表单) | ||||
|         XSSFSheet sheet = workbook.createSheet("测量数据表"); | ||||
|  | ||||
|         XSSFCellStyle highCellStyle = workbook.createCellStyle(); | ||||
|         highCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); | ||||
|         highCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | ||||
|         XSSFFont highFont = workbook.createFont(); | ||||
|         highFont.setColor(HSSFColor.RED.index); | ||||
|         highCellStyle.setFont(highFont); | ||||
|  | ||||
|         XSSFCellStyle lowCellStyle = workbook.createCellStyle(); | ||||
|         lowCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); | ||||
|         lowCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); | ||||
|         XSSFFont lowFont = workbook.createFont(); | ||||
|         lowFont.setColor(HSSFColor.BLUE.index); | ||||
|         lowCellStyle.setFont(lowFont); | ||||
|  | ||||
|         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); | ||||
|  | ||||
|         //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 | ||||
|         XSSFRow titleRow = sheet.createRow(0); | ||||
|         //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 | ||||
|         XSSFCell titleCell = titleRow.createCell(0); | ||||
|         //设置单元格内容 | ||||
|         titleCell.setCellValue("测量数据导出表"); | ||||
|         titleCell.setCellStyle(normalCellStyle); | ||||
|         //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 | ||||
|         sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, dataListList.get(0).size() - 1)); | ||||
|         int startRowNum = 1; | ||||
|         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); | ||||
|                     if (data.contains("##H")) { | ||||
|                         cell.setCellValue(data.split("##")[0]); | ||||
|                         cell.setCellStyle(highCellStyle); | ||||
|                     } else if (data.contains("##L")) { | ||||
|                         cell.setCellValue(data.split("##")[0]); | ||||
|                         cell.setCellStyle(lowCellStyle); | ||||
|                     } else { | ||||
|                         cell.setCellValue(data); | ||||
|                         cell.setCellStyle(normalCellStyle); | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         //输出Excel文件 | ||||
|         String excelFileName = System.currentTimeMillis() + ".xlsx"; | ||||
|         FileOutputStream output = new FileOutputStream("/Users/liuchengqian/Desktop/DaJiang/excel/" + excelFileName); | ||||
|         workbook.write(output); | ||||
|         output.flush(); | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -11,4 +11,5 @@ public interface Constant { | ||||
|      * 检验项目类型:图片 | ||||
|      */ | ||||
|     String QC_ITEM_TYPE_PICTURE = "1"; | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -12,39 +12,10 @@ import java.util.Map; | ||||
|  */ | ||||
| public class ExportExcel { | ||||
|  | ||||
|     /** | ||||
|      * 最简单的填充 | ||||
|      * | ||||
|      * @since 2.1.1 | ||||
|      */ | ||||
|     /*public static String simpleFill(Map<String, String> map,String templateFileName) { | ||||
|  | ||||
|  | ||||
|         String s = System.currentTimeMillis() + ".xlsx"; | ||||
|         // 方案2 根据Map填充 | ||||
|         String fileName = "/usr/local/excel/" + s; | ||||
|         //String fileName = "E:\\shoptest\\" + s; | ||||
|         // 这里 会填充到第一个sheet, 然后文件流会自动关闭 | ||||
|  | ||||
|         ExcelWriterBuilder write = EasyExcel.write(fileName); | ||||
|  | ||||
|         ExcelWriterBuilder excelWriterBuilder = write.withTemplate(templateFileName); | ||||
|  | ||||
|         ExcelWriter build = excelWriterBuilder.build(); | ||||
|         WriteSheet writeSheet = EasyExcel.writerSheet().build(); | ||||
|  | ||||
|         build.fill(map,writeSheet); | ||||
|         build.finish(); | ||||
|  | ||||
|         //EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(map); | ||||
|         return "http://118.24.27.47:2088/excel/"+s; | ||||
|         //return "http://192.168.2.139/"+s; | ||||
|     }*/ | ||||
|     public static String exportToProveExcel(Map<String, String> dataMap, String srcFilePath) throws IOException, InvalidFormatException { | ||||
|         String s = System.currentTimeMillis() + ".xlsx"; | ||||
|         //String path = "E:/shop/"+s; | ||||
|         String path = "/home/sxy/server/industrial_measurement/excel/" + s; | ||||
|  | ||||
|         //  开始转换。利用  transformer 转到Excel | ||||
|         XLSTransformer transformer = new XLSTransformer(); | ||||
|         // 参数:srcFilePath:模板源文件    cMap:需要导出的数据   destFile.getAbsolutePath():下载的目标文件 | ||||
| @@ -53,15 +24,4 @@ public class ExportExcel { | ||||
|         return "http://118.24.27.47:2088/excel/" + s; | ||||
|     } | ||||
|  | ||||
|     /*public static void main(String[] args) throws IOException, InvalidFormatException { | ||||
|         // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 | ||||
|         String templateFileName = TestFileUtil.getPath() + "templates" + File.separator + "test.xlsx"; | ||||
|         Map<String, String> map = new HashMap<String, String>(); | ||||
|         map.put("name", "张三"); | ||||
|         map.put("sex", "男"); | ||||
|         //simpleFill(map,templateFileName); | ||||
|         HttpServletResponse response = null; | ||||
|         String s = exportToProveExcel(map); | ||||
|         System.out.println(s); | ||||
|     }*/ | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user