From 66d9f6c0df19b3da97bde0e0856db5ebd37cb245 Mon Sep 17 00:00:00 2001 From: Aug <17674666882@163.com> Date: Sat, 7 Dec 2024 01:31:35 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=B7=A5=E5=8D=95=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workorderTemplate/WorkorderTemplateList.vue | 224 ++++ .../modules/WorkorderTemplateForm.vue | 134 ++ .../WorkorderTemplateModal.Style#Drawer.vue | 84 ++ .../modules/WorkorderTemplateModal.vue | 60 + .../controller/WorkorderTemplateController.java | 37 + .../workorderTemplate/entity/ImportBean.java | 21 + .../service/WorkorderExcelService.java | 31 + .../service/impl/WorkorderExcelServiceImpl.java | 1278 ++++++++++++++++++++ .../workorderTemplate/util/ExcelClassField.java | 77 ++ .../workorderTemplate/util/ExcelExport.java | 28 + .../workorderTemplate/util/ExcelImport.java | 31 + .../modules/workorderTemplate/util/ExcelUtils.java | 1006 +++++++++++++++ 12 files changed, 3011 insertions(+) create mode 100644 admin-hanhai-vue/src/views/workorderTemplate/WorkorderTemplateList.vue create mode 100644 admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateForm.vue create mode 100644 admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.Style#Drawer.vue create mode 100644 admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.vue create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/entity/ImportBean.java create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/WorkorderExcelService.java create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/impl/WorkorderExcelServiceImpl.java create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelClassField.java create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelExport.java create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelImport.java create mode 100644 jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelUtils.java diff --git a/admin-hanhai-vue/src/views/workorderTemplate/WorkorderTemplateList.vue b/admin-hanhai-vue/src/views/workorderTemplate/WorkorderTemplateList.vue new file mode 100644 index 0000000..5fcf12e --- /dev/null +++ b/admin-hanhai-vue/src/views/workorderTemplate/WorkorderTemplateList.vue @@ -0,0 +1,224 @@ + + + + \ No newline at end of file diff --git a/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateForm.vue b/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateForm.vue new file mode 100644 index 0000000..b5230a8 --- /dev/null +++ b/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateForm.vue @@ -0,0 +1,134 @@ + + + \ No newline at end of file diff --git a/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.Style#Drawer.vue b/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.Style#Drawer.vue new file mode 100644 index 0000000..d6c4013 --- /dev/null +++ b/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.Style#Drawer.vue @@ -0,0 +1,84 @@ + + + + + \ No newline at end of file diff --git a/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.vue b/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.vue new file mode 100644 index 0000000..1782209 --- /dev/null +++ b/admin-hanhai-vue/src/views/workorderTemplate/modules/WorkorderTemplateModal.vue @@ -0,0 +1,60 @@ + + + \ No newline at end of file diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/controller/WorkorderTemplateController.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/controller/WorkorderTemplateController.java index 26176fc..1bde699 100644 --- a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/controller/WorkorderTemplateController.java +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/controller/WorkorderTemplateController.java @@ -1,5 +1,7 @@ package org.jeecg.modules.workorderTemplate.controller; +import java.io.File; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -7,11 +9,16 @@ import java.util.stream.Collectors; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import javax.annotation.Resource; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.Part; + import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.util.oConvertUtils; +import org.jeecg.modules.workorderTemplate.entity.ImportBean; import org.jeecg.modules.workorderTemplate.entity.WorkorderTemplate; import org.jeecg.modules.workorderTemplate.service.IWorkorderTemplateService; @@ -20,6 +27,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; +import org.jeecg.modules.workorderTemplate.service.WorkorderExcelService; +import org.jeecg.modules.workorderTemplate.util.ExcelUtils; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.def.NormalExcelConstants; import org.jeecgframework.poi.excel.entity.ExportParams; @@ -165,7 +174,35 @@ public class WorkorderTemplateController extends JeecgController importExcel(HttpServletRequest request, HttpServletResponse response) { + System.out.println("test"); return super.importExcel(request, response, WorkorderTemplate.class); } + @PostMapping("/import") + public Result importUser(@RequestPart("file")MultipartFile file) throws Exception { + List importBeanList = ExcelUtils.readMultipartFile(file, ImportBean.class); + return workorderExcelService.templateImport1(importBeanList); + } + + /******************************************************************************************************************/ + //excel信息 + @Resource + private WorkorderExcelService workorderExcelService; + /****************************************************************************************************/ + + @ApiOperation(value="导入导出-工单信息导入", notes="导入导出-工单信息导入") + @RequestMapping(value = "/templateImport") + public Result templateImport(HttpServletRequest request){ + String path = ""; + return workorderExcelService.templateImport(path); + } + + @ApiOperation(value="导入导出-工单信息导出", notes="导入导出-工单信息导出") + @RequestMapping(value = "/templateExport") + public Result templateExport(HttpServletRequest request, HttpServletResponse response){ + String path = request.getParameter("fPath").replace("\\", "\\\\") + "\\\\"; + String templateId = request.getParameter("templateId"); + return workorderExcelService.templateExport(path, templateId); + } + } diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/entity/ImportBean.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/entity/ImportBean.java new file mode 100644 index 0000000..cba59bd --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/entity/ImportBean.java @@ -0,0 +1,21 @@ +package org.jeecg.modules.workorderTemplate.entity; + +import lombok.Data; +import org.jeecg.modules.workorderTemplate.util.ExcelImport; + +@Data +public class ImportBean { + + @ExcelImport("任务号") + private String taskNo; + + @ExcelImport("担当信息") + private String responsibler; + + @ExcelImport("机型信息") + private String model; + + @ExcelImport("数量") + private int number; + +} diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/WorkorderExcelService.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/WorkorderExcelService.java new file mode 100644 index 0000000..0edd35e --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/WorkorderExcelService.java @@ -0,0 +1,31 @@ +package org.jeecg.modules.workorderTemplate.service; + +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.workorderTemplate.entity.ImportBean; + +import java.util.List; + +public interface WorkorderExcelService { + + /** + * 工单导入 + * @param importBeanList 导入数据 + * @return + */ + public Result templateImport1(List importBeanList); + + /** + * 工单导入 + * @param path 导入文件路径 + * @return + */ + public Result templateImport(String path); + + /** + * 工单导出 + * @param templateId 工单编号 + * @return + */ + public Result templateExport(String path, String templateId); + +} diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/impl/WorkorderExcelServiceImpl.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/impl/WorkorderExcelServiceImpl.java new file mode 100644 index 0000000..22441d9 --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/service/impl/WorkorderExcelServiceImpl.java @@ -0,0 +1,1278 @@ +package org.jeecg.modules.workorderTemplate.service.impl; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.jeecg.common.api.vo.Result; +import org.jeecg.modules.workorderGeneralStepfour.entity.WorkorderGeneralStepfour; +import org.jeecg.modules.workorderGeneralStepfour.service.IWorkorderGeneralStepfourService; +import org.jeecg.modules.workorderGeneralStepone.entity.WorkorderGeneralStepone; +import org.jeecg.modules.workorderGeneralStepone.service.IWorkorderGeneralSteponeService; +import org.jeecg.modules.workorderGeneralStepthree.entity.WorkorderGeneralStepthree; +import org.jeecg.modules.workorderGeneralStepthree.service.IWorkorderGeneralStepthreeService; +import org.jeecg.modules.workorderGeneralSteptwo.entity.WorkorderGeneralSteptwo; +import org.jeecg.modules.workorderGeneralSteptwo.service.IWorkorderGeneralSteptwoService; +import org.jeecg.modules.workorderItemCheck.entity.WorkorderItemCheck; +import org.jeecg.modules.workorderItemCheck.service.IWorkorderItemCheckService; +import org.jeecg.modules.workorderParamStepfour.entity.WorkorderParamStepfour; +import org.jeecg.modules.workorderParamStepfour.service.IWorkorderParamStepfourService; +import org.jeecg.modules.workorderParamStepone.entity.WorkorderParamStepone; +import org.jeecg.modules.workorderParamStepone.service.IWorkorderParamSteponeService; +import org.jeecg.modules.workorderParamStepthree.entity.WorkorderParamStepthree; +import org.jeecg.modules.workorderParamStepthree.service.IWorkorderParamStepthreeService; +import org.jeecg.modules.workorderParamSteptwo.entity.WorkorderParamSteptwo; +import org.jeecg.modules.workorderParamSteptwo.service.IWorkorderParamSteptwoService; +import org.jeecg.modules.workorderProcess.entity.WorkorderProcess; +import org.jeecg.modules.workorderProcess.service.IWorkorderProcessService; +import org.jeecg.modules.workorderStep.entity.WorkorderStep; +import org.jeecg.modules.workorderStep.service.IWorkorderStepService; +import org.jeecg.modules.workorderTemplate.entity.ImportBean; +import org.jeecg.modules.workorderTemplate.entity.WorkorderTemplate; +import org.jeecg.modules.workorderTemplate.service.IWorkorderTemplateService; +import org.jeecg.modules.workorderTemplate.service.WorkorderExcelService; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +@Service +public class WorkorderExcelServiceImpl implements WorkorderExcelService { + + + + //工单信息 + @Resource + private IWorkorderTemplateService workorderTemplateService; + + //工序卡信息 + @Resource + private IWorkorderStepService workorderStepService; + + //工序流程信息 + @Resource + private IWorkorderProcessService workorderProcessService; + + //检查项目信息 + @Resource + private IWorkorderItemCheckService workorderItemCheckService; + + //工序卡常规参数 + @Resource + private IWorkorderGeneralSteponeService workorderGeneralSteponeService; + @Resource + private IWorkorderGeneralSteptwoService workorderGeneralSteptwoService; + @Resource + private IWorkorderGeneralStepthreeService workorderGeneralStepthreeService; + @Resource + private IWorkorderGeneralStepfourService workorderGeneralStepfourService; + + //工序卡列表参数 + @Resource + private IWorkorderParamSteponeService workorderParamSteponeService; + @Resource + private IWorkorderParamSteptwoService workorderParamSteptwoService; + @Resource + private IWorkorderParamStepthreeService workorderParamStepthreeService; + @Resource + private IWorkorderParamStepfourService workorderParamStepfourService; + /******************************************************************************************************************/ + @Override + public Result templateImport1(List importBeanList) { + List templateList = new ArrayList<>(); + + try { + //1、获取excel数据 + for (ImportBean importBean : importBeanList) { + WorkorderTemplate workorderTemplate = new WorkorderTemplate(); + workorderTemplate.setTaskNo(importBean.getTaskNo()); + workorderTemplate.setResponsibler(importBean.getResponsibler()); + workorderTemplate.setModel(importBean.getModel()); + workorderTemplate.setNumber(importBean.getNumber()); + templateList.add(workorderTemplate); + } + + //2、将数据录入到数据库 + Boolean result = workorderTemplateService.saveBatch(templateList); + if(result){ + return Result.OK("数据导入成功!"); + }else { + return Result.error("数据导入失败!"); + } + } catch (Exception e) { + e.printStackTrace(); + return Result.error("数据导入失败!"); + } + } + + /**********************************************主体入口************************************************************/ + //工单导入 + @Override + public Result templateImport(String path) { + try { + //1、获取excel数据 + List templateList = getImportData(path); + + //2、将数据录入到数据库 + Boolean result = workorderTemplateService.saveBatch(templateList); + if(result){ + return Result.OK("数据导入成功!"); + }else { + return Result.error("数据导入失败!"); + } + } catch (Exception e) { + e.printStackTrace(); + return Result.error("数据导入失败!"); + } + } + + //工单导出 + @Override + public Result templateExport(String path, String templateId) { + try { + template(path, templateId); + System.out.println("test over!"); + return Result.OK(templateId+"导出成功!"); + } catch (Exception e) { + e.printStackTrace(); + return Result.OK("导出失败!"); + } + } + + /******************************************************************************************************************/ + /**********************************************工单导入************************************************************/ + //获取导入工单数据 + public List getImportData(String path) throws Exception{ + path = "F:\\temp\\工单导入测试数据.xlsx"; + //工单列表 + List templateList = new ArrayList<>(); + + //创建文件流 + InputStream fileInputStream = null; + try{ + //获取文件流 + fileInputStream = new FileInputStream(path); + + //1、创建一个工作簿 + Workbook workbook = new XSSFWorkbook(fileInputStream); + //2、得到一个工作表 + Sheet sheet = workbook.getSheetAt(0); + + //3、获取表中内容 + //3.1、获取表中有多少行数据 + int rowCount = sheet.getPhysicalNumberOfRows(); + for (int rowNum = 1; rowNum < rowCount; rowNum++) { + WorkorderTemplate template = new WorkorderTemplate(); + Row rowData = sheet.getRow(rowNum); + if(rowData != null){ + template.setTaskNo(rowData.getCell(0).getStringCellValue()); + template.setResponsibler(rowData.getCell(1).getStringCellValue()); + template.setModel(rowData.getCell(2).getStringCellValue()); + template.setNumber((int)rowData.getCell(3).getNumericCellValue()); + } + templateList.add(template); + } + + return templateList; + + }catch (Exception e){ + e.printStackTrace(); + return null; + }finally { + if(null != fileInputStream){ + //操作结束,关闭文件 + fileInputStream.close(); + } + } + } + + + + + + /******************************************************************************************************************/ + /**********************************************工单导出************************************************************/ + //创建工作表 + public void template(String path, String templateId) throws Exception{ + //创建一个工作表 + Workbook workbook = new SXSSFWorkbook(); + + //编辑工作表内容 + sheet(workbook, templateId); + + //导出工作表 + FileOutputStream out = new FileOutputStream(path+templateId+".xlsx"); + workbook.write(out); + + //操作结束,关闭文件流 + out.close(); + + //清除临时文件 + ((SXSSFWorkbook) workbook).dispose(); + + } + + //常规参数-工序卡1 + public int generalParamOne(Sheet sheet, WorkorderTemplate template, int stepNum, int generalNum, CellStyle cellStyle){ + //常规参数信息 + WorkorderGeneralStepone generalStepone = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralSteponeList().get(generalNum); + //设置跨行跨列 + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9)); + sheet.addMergedRegion(new CellRangeAddress(2, 3, 6, 6)); + sheet.addMergedRegion(new CellRangeAddress(2, 3, 7, 7)); + + //第一行 + Row row1 = sheet.createRow(0); + //第一行第一列 + Cell cell11 = row1.createCell(0); + cell11.setCellValue("中试压缩机试作工序卡1(选配)"); + cell11.setCellStyle(cellStyle); + + //第二行 + Row row2 = sheet.createRow(1); + //第二行第n列 + Cell cell21 = row2.createCell(0); + cell21.setCellValue("委托担当"); + Cell cell22 = row2.createCell(1); + cell22.setCellValue(template.getResponsibler()); + Cell cell23 = row2.createCell(2); + cell23.setCellValue("指示书/流程编号:"); + Cell cell24 = row2.createCell(3); + cell24.setCellValue(template.getTaskNo()); + Cell cell25 = row2.createCell(4); + cell25.setCellValue("机种名"); + Cell cell26 = row2.createCell(5); + cell26.setCellValue(template.getModel()); + Cell cell27 = row2.createCell(6); + cell27.setCellValue("数量"); + Cell cell28 = row2.createCell(7); + cell28.setCellValue(template.getNumber()); + Cell cell29 = row2.createCell(8); + cell29.setCellValue("页数"); + Cell cell210 = row2.createCell(9); + cell210.setCellValue("1/4"); + + //第三行 + Row row3 = sheet.createRow(2); + //第三行第n列 + Cell cell31 = row3.createCell(0); + cell31.setCellValue("气缸料号1"); + Cell cell32 = row3.createCell(1); + cell32.setCellValue(generalStepone.getCylinder1()); + Cell cell33 = row3.createCell(2); + cell33.setCellValue("活塞料号1"); + Cell cell34 = row3.createCell(3); + cell34.setCellValue(generalStepone.getPiston1()); + Cell cell35 = row3.createCell(4); + cell35.setCellValue("划片料号1"); + Cell cell36 = row3.createCell(5); + cell36.setCellValue(generalStepone.getScribing1()); + Cell cell37 = row3.createCell(6); + cell37.setCellValue("中试作业员"); + Cell cell38 = row3.createCell(7); + cell38.setCellValue(generalStepone.getOperator()); + + //第四行 + Row row4 = sheet.createRow(3); + //第四行第n列 + Cell cell41 = row4.createCell(0); + cell41.setCellValue("气缸料号2"); + Cell cell42 = row4.createCell(1); + cell42.setCellValue(generalStepone.getCylinder2()); + Cell cell43 = row4.createCell(2); + cell43.setCellValue("活塞料号2"); + Cell cell44 = row4.createCell(3); + cell44.setCellValue(generalStepone.getPiston2()); + Cell cell45 = row4.createCell(4); + cell45.setCellValue("划片料号2"); + Cell cell46 = row4.createCell(5); + cell46.setCellValue(generalStepone.getScribing2()); + Cell cell47 = row4.createCell(6); + cell47.setCellValue("中试作业员"); + Cell cell48 = row4.createCell(7); + cell48.setCellValue(generalStepone.getOperator()); + return sheet.getLastRowNum(); + } + + //常规参数-工序卡2 + public int generalParamTwo(Sheet sheet, WorkorderTemplate template, int stepNum, int generalNum, CellStyle cellStyle){ + //常规参数信息 + WorkorderGeneralSteptwo generalSteptwo = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralSteptwoList().get(generalNum); + //设置跨行跨列 + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9)); + + //第一行 + Row row1 = sheet.createRow(0); + //第一行第一列 + Cell cell11 = row1.createCell(0); + cell11.setCellValue("中试压缩机试作工序卡2"); + cell11.setCellStyle(cellStyle); + + //第二行 + Row row2 = sheet.createRow(1); + //第二行第n列 + Cell cell21 = row2.createCell(0); + cell21.setCellValue("委托担当"); + Cell cell22 = row2.createCell(1); + cell22.setCellValue(template.getResponsibler()); + Cell cell23 = row2.createCell(2); + cell23.setCellValue("指示书/流程编号:"); + Cell cell24 = row2.createCell(3); + cell24.setCellValue(template.getTaskNo()); + Cell cell25 = row2.createCell(4); + cell25.setCellValue("机种名"); + Cell cell26 = row2.createCell(5); + cell26.setCellValue(template.getModel()); + Cell cell27 = row2.createCell(6); + cell27.setCellValue("数量"); + Cell cell28 = row2.createCell(7); + cell28.setCellValue(template.getNumber()); + Cell cell29 = row2.createCell(8); + cell29.setCellValue("页数"); + Cell cell210 = row2.createCell(9); + cell210.setCellValue("2/4"); + + //第三行 + Row row3 = sheet.createRow(2); + //第三行第n列 + Cell cell31 = row3.createCell(0); + cell31.setCellValue("部件名"); + Cell cell32 = row3.createCell(1); + cell32.setCellValue(generalSteptwo.getComponent()); + Cell cell33 = row3.createCell(2); + cell33.setCellValue("日期"); + Cell cell34 = row3.createCell(3); + cell34.setCellValue(generalSteptwo.getCurrentdate()); + Cell cell35 = row3.createCell(4); + cell35.setCellValue("中试确认"); + Cell cell36 = row3.createCell(5); + cell36.setCellValue(generalSteptwo.getCreateBy()); + Cell cell37 = row3.createCell(6); + cell37.setCellValue("品质确认"); + Cell cell38 = row3.createCell(7); + cell38.setCellValue(generalSteptwo.getConfirmQuality()); + Cell cell39 = row3.createCell(8); + cell39.setCellValue("研发确认"); + Cell cell310 = row3.createCell(9); + cell310.setCellValue(generalSteptwo.getConfirmDevelop()); + + //第四行 + Row row4 = sheet.createRow(3); + //第四行第n列 + Cell cell41 = row4.createCell(0); + cell41.setCellValue("工序"); + Cell cell42 = row4.createCell(1); + cell42.setCellValue("检查项目"); + Cell cell43 = row4.createCell(2); + cell43.setCellValue("规格"); + Cell cell44 = row4.createCell(3); + cell44.setCellValue("1"); + Cell cell45 = row4.createCell(4); + cell45.setCellValue("2"); + Cell cell46 = row4.createCell(5); + cell46.setCellValue("3"); + Cell cell47 = row4.createCell(6); + cell47.setCellValue("4"); + Cell cell48 = row4.createCell(7); + cell48.setCellValue("5"); + Cell cell49 = row4.createCell(8); + cell49.setCellValue("6"); + Cell cell410 = row4.createCell(9); + cell410.setCellValue("7"); + Cell cell411 = row4.createCell(10); + cell411.setCellValue("8"); + Cell cell412 = row4.createCell(11); + cell412.setCellValue("9"); + Cell cell413 = row4.createCell(12); + cell413.setCellValue("10"); + Cell cell414 = row4.createCell(13); + cell414.setCellValue("作业员"); + + return sheet.getLastRowNum(); + } + + //常规参数-工序卡3 + public int generalParamThree(Sheet sheet, WorkorderTemplate template, int stepNum, int generalNum, CellStyle cellStyle){ + //常规参数信息 + WorkorderGeneralStepthree generalStepthree = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralStepthreeList().get(generalNum); + //设置跨行跨列 + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9)); + + //第一行 + Row row1 = sheet.createRow(0); + //第一行第一列 + Cell cell11 = row1.createCell(0); + cell11.setCellValue("中试压缩机试作工序卡3"); + cell11.setCellStyle(cellStyle); + + + //第二行 + Row row2 = sheet.createRow(1); + //第二行第n列 + Cell cell21 = row2.createCell(0); + cell21.setCellValue("委托担当"); + Cell cell22 = row2.createCell(1); + cell22.setCellValue(template.getResponsibler()); + Cell cell23 = row2.createCell(2); + cell23.setCellValue("指示书/流程编号:"); + Cell cell24 = row2.createCell(3); + cell24.setCellValue(template.getTaskNo()); + Cell cell25 = row2.createCell(4); + cell25.setCellValue("机种名"); + Cell cell26 = row2.createCell(5); + cell26.setCellValue(template.getModel()); + Cell cell27 = row2.createCell(6); + cell27.setCellValue("数量"); + Cell cell28 = row2.createCell(7); + cell28.setCellValue(template.getNumber()); + Cell cell29 = row2.createCell(8); + cell29.setCellValue("页数"); + Cell cell210 = row2.createCell(9); + cell210.setCellValue("3/4"); + + //第三行 + Row row3 = sheet.createRow(2); + //第三行第n列 + Cell cell31 = row3.createCell(0); + cell31.setCellValue("部件名"); + Cell cell32 = row3.createCell(1); + cell32.setCellValue(generalStepthree.getComponent()); + Cell cell33 = row3.createCell(2); + cell33.setCellValue("日期"); + Cell cell34 = row3.createCell(3); + cell34.setCellValue(generalStepthree.getCurrentdate()); + Cell cell35 = row3.createCell(4); + cell35.setCellValue("中试确认"); + Cell cell36 = row3.createCell(5); + cell36.setCellValue(generalStepthree.getCreateBy()); + Cell cell37 = row3.createCell(6); + cell37.setCellValue("品质确认"); + Cell cell38 = row3.createCell(7); + cell38.setCellValue(generalStepthree.getConfirmQuality()); + Cell cell39 = row3.createCell(8); + cell39.setCellValue("研发确认"); + Cell cell310 = row3.createCell(9); + cell310.setCellValue(generalStepthree.getComfirmDevelop()); + + //第四行 + Row row4 = sheet.createRow(3); + //第四行第n列 + Cell cell41 = row4.createCell(0); + cell41.setCellValue("工序"); + Cell cell42 = row4.createCell(1); + cell42.setCellValue("检查项目"); + Cell cell43 = row4.createCell(2); + cell43.setCellValue("规格"); + Cell cell44 = row4.createCell(3); + cell44.setCellValue("1"); + Cell cell45 = row4.createCell(4); + cell45.setCellValue("2"); + Cell cell46 = row4.createCell(5); + cell46.setCellValue("3"); + Cell cell47 = row4.createCell(6); + cell47.setCellValue("4"); + Cell cell48 = row4.createCell(7); + cell48.setCellValue("5"); + Cell cell49 = row4.createCell(8); + cell49.setCellValue("6"); + Cell cell410 = row4.createCell(9); + cell410.setCellValue("7"); + Cell cell411 = row4.createCell(10); + cell411.setCellValue("8"); + Cell cell412 = row4.createCell(11); + cell412.setCellValue("9"); + Cell cell413 = row4.createCell(12); + cell413.setCellValue("10"); + Cell cell414 = row4.createCell(13); + cell414.setCellValue("作业员"); + + return sheet.getLastRowNum(); + } + + //常规参数-工序卡4 + public int generalParamFour(Sheet sheet, WorkorderTemplate template, int stepNum, int generalNum, CellStyle cellStyle){ + //常规参数信息 + WorkorderGeneralStepfour generalStepfour = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralStepfourList().get(generalNum); + //设置跨行跨列 + sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9)); + + //第一行 + Row row1 = sheet.createRow(0); + //第一行第一列 + Cell cell11 = row1.createCell(0); + cell11.setCellValue("中试压缩机试作工序卡4(总成)"); + cell11.setCellStyle(cellStyle); + + //第二行 + Row row2 = sheet.createRow(1); + //第二行第n列 + Cell cell21 = row2.createCell(0); + cell21.setCellValue("委托担当"); + Cell cell22 = row2.createCell(1); + cell22.setCellValue(template.getResponsibler()); + Cell cell23 = row2.createCell(2); + cell23.setCellValue("指示书/流程编号:"); + Cell cell24 = row2.createCell(3); + cell24.setCellValue(template.getTaskNo()); + Cell cell25 = row2.createCell(4); + cell25.setCellValue("机种名"); + Cell cell26 = row2.createCell(5); + cell26.setCellValue(template.getModel()); + Cell cell27 = row2.createCell(6); + cell27.setCellValue("数量"); + Cell cell28 = row2.createCell(7); + cell28.setCellValue(template.getNumber()); + Cell cell29 = row2.createCell(8); + cell29.setCellValue("页数"); + Cell cell210 = row2.createCell(9); + cell210.setCellValue("4/4"); + + //第三行 + Row row3 = sheet.createRow(2); + //第三行第n列 + Cell cell31 = row3.createCell(0); + cell31.setCellValue("部件名"); + Cell cell32 = row3.createCell(1); + cell32.setCellValue(generalStepfour.getComponent()); + Cell cell33 = row3.createCell(2); + cell33.setCellValue("日期"); + Cell cell34 = row3.createCell(3); + cell34.setCellValue(generalStepfour.getCurrentdate()); + Cell cell35 = row3.createCell(4); + cell35.setCellValue("中试确认"); + Cell cell36 = row3.createCell(5); + cell36.setCellValue(generalStepfour.getCreateBy()); + Cell cell37 = row3.createCell(6); + cell37.setCellValue("品质确认"); + Cell cell38 = row3.createCell(7); + cell38.setCellValue(generalStepfour.getComfirmQuality()); + Cell cell39 = row3.createCell(8); + cell39.setCellValue("研发确认"); + Cell cell310 = row3.createCell(9); + cell310.setCellValue(generalStepfour.getComfirmDevelop()); + + //第四行 + Row row4 = sheet.createRow(3); + //第四行第n列 + Cell cell41 = row4.createCell(0); + cell41.setCellValue("工序"); + Cell cell42 = row4.createCell(1); + cell42.setCellValue("检查项目"); + Cell cell43 = row4.createCell(2); + cell43.setCellValue("规格"); + Cell cell44 = row4.createCell(3); + cell44.setCellValue("1"); + Cell cell45 = row4.createCell(4); + cell45.setCellValue("2"); + Cell cell46 = row4.createCell(5); + cell46.setCellValue("3"); + Cell cell47 = row4.createCell(6); + cell47.setCellValue("4"); + Cell cell48 = row4.createCell(7); + cell48.setCellValue("5"); + Cell cell49 = row4.createCell(8); + cell49.setCellValue("6"); + Cell cell410 = row4.createCell(9); + cell410.setCellValue("7"); + Cell cell411 = row4.createCell(10); + cell411.setCellValue("8"); + Cell cell412 = row4.createCell(11); + cell412.setCellValue("9"); + Cell cell413 = row4.createCell(12); + cell413.setCellValue("10"); + Cell cell414 = row4.createCell(13); + cell414.setCellValue("作业员"); + + return sheet.getLastRowNum(); + } + + //工作簿 + public void sheet(Workbook workbook, String templateId) { + //设置居中 + CellStyle cellStyle = workbook.createCellStyle(); + cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中 + cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中 + + WorkorderTemplate template = getExportData(templateId); + + int stepCount = template.getWorkorderStepList().size();//工序卡数量 + for (int stepNum = 0; stepNum < stepCount; stepNum++) { + + int lineNum = 0;//写入单元行 + int firstLine= 0;//合并单元起始行 + String stepName = template.getWorkorderStepList().get(stepNum).getName();//工序卡名称 + Sheet sheet = workbook.createSheet(stepName);//创建工序卡 + int processCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().size();//工序数量 + //工序卡1 + String stepId = template.getWorkorderStepList().get(stepNum).getId();//工序卡id + + //常规参数-工序卡1 + int generalOneCount = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralSteponeList().size();//常规参数数量 + for (int generalNum = 0; generalNum < generalOneCount; generalNum++) { + String g_stepId = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralSteponeList().get(generalNum).getStepId(); + if(g_stepId.equals(stepId)){ + lineNum = generalParamOne(sheet, template, stepNum, generalNum, cellStyle) + 1; + firstLine = lineNum; + } + } + + //常规参数-工序卡2 + int generalTwoCount = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralSteptwoList().size();//常规参数数量 + for (int generalNum = 0; generalNum < generalTwoCount; generalNum++) { + String g_stepId = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralSteptwoList().get(generalNum).getStepId(); + if(g_stepId.equals(stepId)){ + lineNum = generalParamTwo(sheet, template, stepNum, generalNum, cellStyle) + 1; + firstLine = lineNum; + } + } + + //常规参数-工序卡3 + int generalThreeCount = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralStepthreeList().size();//常规参数数量 + for (int generalNum = 0; generalNum < generalThreeCount; generalNum++) { + String g_stepId = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralStepthreeList().get(generalNum).getStepId(); + if(g_stepId.equals(stepId)){ + lineNum = generalParamThree(sheet, template, stepNum, generalNum, cellStyle) + 1; + firstLine = lineNum; + } + } + + //常规参数-工序卡3 + int generalFourCount = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralStepfourList().size();//常规参数数量 + for (int generalNum = 0; generalNum < generalFourCount; generalNum++) { + String g_stepId = template.getWorkorderStepList().get(stepNum).getWorkorderGeneralStepfourList().get(generalNum).getStepId(); + if(g_stepId.equals(stepId)){ + lineNum = generalParamFour(sheet, template, stepNum, generalNum, cellStyle) + 1; + firstLine = lineNum; + } + } + + //检查项目参数 + for (int processNum = 0; processNum < processCount; processNum++) { + //工序 + String p_stepId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getStepId();//关联工序卡id + if(p_stepId.equals(stepId)){ + String processId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getId();//工序id + String processName = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getName();//工序名称 + int itemCheckCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().size();//检查项目数量 + int lastLine = firstLine + itemCheckCount-1;//合并单元结束行 + if(firstLine != lastLine){ + sheet.addMergedRegion(new CellRangeAddress(firstLine,lastLine,0,0));//合并单元格 + } + for (int itemNum = 0; itemNum < itemCheckCount; itemNum++) { + //检查项目 + String i_processId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getProcessId();//关联工序id + if(i_processId.equals(processId)){ + String itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getId();//检查项目id + String itemCheckName = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getName();//检查项目名称 + + Row row = sheet.createRow(lineNum); + Cell cell1 = row.createCell(0); + cell1.setCellValue(processName); + Cell cell2 = row.createCell(1); + cell2.setCellValue(itemCheckName); + + int paramOneCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteponeList().size(); + for (int paramNum = 0; paramNum < paramOneCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteponeList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)) { + + //检查项目参数-工序卡1 + WorkorderParamStepone paramStepone = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteponeList().get(paramNum); + if (null != paramStepone) { + //第n行第n列 + Cell specs = row.createCell(2); + specs.setCellValue(paramStepone.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramStepone.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramStepone.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramStepone.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramStepone.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramStepone.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramStepone.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramStepone.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramStepone.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramStepone.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramStepone.getParam10()); + Cell param11 = row.createCell(13); + param11.setCellValue(paramStepone.getParam11()); + Cell param12 = row.createCell(14); + param12.setCellValue(paramStepone.getParam12()); + Cell param13 = row.createCell(15); + param13.setCellValue(paramStepone.getParam13()); + Cell param14 = row.createCell(16); + param14.setCellValue(paramStepone.getParam14()); + Cell param15 = row.createCell(17); + param15.setCellValue(paramStepone.getParam15()); + Cell param16 = row.createCell(18); + param16.setCellValue(paramStepone.getParam16()); + Cell param17 = row.createCell(19); + param17.setCellValue(paramStepone.getParam17()); + Cell param18 = row.createCell(20); + param18.setCellValue(paramStepone.getParam18()); + Cell param19 = row.createCell(21); + param19.setCellValue(paramStepone.getParam19()); + Cell param20 = row.createCell(22); + param20.setCellValue(paramStepone.getParam20()); + Cell remark = row.createCell(23); + remark.setCellValue(paramStepone.getRemark()); + } else { + break; + } + } + } + + //检查项目参数-工序卡2 + int paramTwoCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteptwoList().size(); + for (int paramNum = 0; paramNum < paramTwoCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteptwoList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //工序卡1 + WorkorderParamSteptwo paramSteptwo = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteptwoList().get(paramNum); + if (null != paramSteptwo) { + //第n行第n列 + Cell specs = row.createCell(2); + specs.setCellValue(paramSteptwo.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramSteptwo.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramSteptwo.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramSteptwo.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramSteptwo.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramSteptwo.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramSteptwo.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramSteptwo.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramSteptwo.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramSteptwo.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramSteptwo.getParam10()); + Cell operator = row.createCell(23); + operator.setCellValue(paramSteptwo.getOperator()); + } else { + break; + } + } + } + + //检查项目参数-工序卡3 + int paramThreeCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepthreeList().size(); + for (int paramNum = 0; paramNum < paramThreeCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepthreeList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //工序卡1 + WorkorderParamStepthree paramStepthree = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepthreeList().get(paramNum); + if (null != paramStepthree) { + //第n行第n列 + Cell specs = row.createCell(2); + specs.setCellValue(paramStepthree.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramStepthree.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramStepthree.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramStepthree.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramStepthree.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramStepthree.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramStepthree.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramStepthree.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramStepthree.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramStepthree.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramStepthree.getParam10()); + Cell operator = row.createCell(23); + operator.setCellValue(paramStepthree.getOperator()); + } else { + break; + } + } + } + + //检查项目参数-工序卡4 + int paramFourCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepfourList().size(); + for (int paramNum = 0; paramNum < paramFourCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepfourList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //工序卡1 + WorkorderParamStepfour paramStepfour = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepfourList().get(paramNum); + if (null != paramStepfour) { + //第n行第n列 + Cell specs = row.createCell(2); + specs.setCellValue(paramStepfour.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramStepfour.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramStepfour.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramStepfour.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramStepfour.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramStepfour.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramStepfour.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramStepfour.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramStepfour.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramStepfour.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramStepfour.getParam10()); + Cell operator = row.createCell(23); + operator.setCellValue(paramStepfour.getOperator()); + } else { + break; + } + } + } + + lineNum++;//行数增加 + + } + } + firstLine = lastLine+1;//改变下一个起始行位置 + } + } + } + } + + //工作簿 + public void sheetTest(Workbook workbook, String templateId) { + WorkorderTemplate template = getExportData(templateId); + + int stepCount = template.getWorkorderStepList().size();//工序卡数量 + for (int stepNum = 0; stepNum < stepCount; stepNum++) { + int lineNum = 0;//写入单元行 + int firstLine= 0;//合并单元起始行 + String stepName = template.getWorkorderStepList().get(stepNum).getName();//工序卡名称 + Sheet sheet = workbook.createSheet(stepName);//创建工序卡 + int processCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().size();//工序数量 + //工序卡1 + String stepId = template.getWorkorderStepList().get(stepNum).getId();//工序卡id + for (int processNum = 0; processNum < processCount; processNum++) { + int line = 0;//合并行数(检查项目) + //工序 + String p_stepId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getStepId();//关联工序卡id + if(p_stepId.equals(stepId)){ + String processId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getId();//工序id + String processName = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getName();//工序名称 + int itemCheckCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().size();//检查项目数量 + + for (int itemNum = 0; itemNum < itemCheckCount; itemNum++) { + //检查项目 + String i_processId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getProcessId();//关联工序id + if(i_processId.equals(processId)){ + line++; + String itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getId();//检查项目id + String itemCheckName = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getName();//检查项目名称 + int paramOneCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteponeList().size(); + for (int paramNum = 0; paramNum < paramOneCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteponeList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //检查项目参数-工序卡1 + WorkorderParamStepone paramStepone = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteponeList().get(paramNum); + if (null != paramStepone) { + //第n行 + //int lineNum = (stepNum * processCount * itemCheckCount) + (itemCheckCount * itemNum) + itemNum; + Row row = sheet.createRow((lineNum)); + //第n行第n列 + Cell process = row.createCell(0); + process.setCellValue(processName); + Cell itemCheck = row.createCell(1); + itemCheck.setCellValue(itemCheckName); + Cell specs = row.createCell(2); + specs.setCellValue(paramStepone.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramStepone.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramStepone.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramStepone.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramStepone.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramStepone.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramStepone.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramStepone.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramStepone.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramStepone.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramStepone.getParam10()); + Cell param11 = row.createCell(13); + param11.setCellValue(paramStepone.getParam11()); + Cell param12 = row.createCell(14); + param12.setCellValue(paramStepone.getParam12()); + Cell param13 = row.createCell(15); + param13.setCellValue(paramStepone.getParam13()); + Cell param14 = row.createCell(16); + param14.setCellValue(paramStepone.getParam14()); + Cell param15 = row.createCell(17); + param15.setCellValue(paramStepone.getParam15()); + Cell param16 = row.createCell(18); + param16.setCellValue(paramStepone.getParam16()); + Cell param17 = row.createCell(19); + param17.setCellValue(paramStepone.getParam17()); + Cell param18 = row.createCell(20); + param18.setCellValue(paramStepone.getParam18()); + Cell param19 = row.createCell(21); + param19.setCellValue(paramStepone.getParam19()); + Cell param20 = row.createCell(22); + param20.setCellValue(paramStepone.getParam20()); + Cell remark = row.createCell(23); + remark.setCellValue(paramStepone.getRemark()); + lineNum++; + } else { + break; + } + + } + } + + //检查项目参数-工序卡2 + int paramTwoCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteptwoList().size(); + for (int paramNum = 0; paramNum < paramTwoCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteptwoList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //工序卡1 + WorkorderParamSteptwo paramSteptwo = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamSteptwoList().get(paramNum); + if (null != paramSteptwo) { + //第n行 + //int lineNum = (stepNum * processCount * itemCheckCount) + (itemCheckCount * itemNum) + itemNum; + Row row = sheet.createRow((lineNum)); + //第n行第n列 + Cell process = row.createCell(0); + process.setCellValue(processName); + Cell itemCheck = row.createCell(1); + itemCheck.setCellValue(itemCheckName); + Cell specs = row.createCell(2); + specs.setCellValue(paramSteptwo.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramSteptwo.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramSteptwo.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramSteptwo.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramSteptwo.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramSteptwo.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramSteptwo.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramSteptwo.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramSteptwo.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramSteptwo.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramSteptwo.getParam10()); + Cell operator = row.createCell(23); + operator.setCellValue(paramSteptwo.getOperator()); + lineNum++; + } else { + break; + } + + } + } + + //检查项目参数-工序卡3 + int paramThreeCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepthreeList().size(); + for (int paramNum = 0; paramNum < paramThreeCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepthreeList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //工序卡1 + WorkorderParamStepthree paramStepthree = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepthreeList().get(paramNum); + if (null != paramStepthree) { + //第n行 + //int lineNum = (stepNum * processCount * itemCheckCount) + (itemCheckCount * itemNum) + itemNum; + Row row = sheet.createRow((lineNum)); + //第n行第n列 + Cell process = row.createCell(0); + process.setCellValue(processName); + Cell itemCheck = row.createCell(1); + itemCheck.setCellValue(itemCheckName); + Cell specs = row.createCell(2); + specs.setCellValue(paramStepthree.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramStepthree.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramStepthree.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramStepthree.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramStepthree.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramStepthree.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramStepthree.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramStepthree.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramStepthree.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramStepthree.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramStepthree.getParam10()); + Cell operator = row.createCell(23); + operator.setCellValue(paramStepthree.getOperator()); + lineNum++; + } else { + break; + } + + } + } + + //检查项目参数-工序卡4 + int paramFourCount = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepfourList().size(); + for (int paramNum = 0; paramNum < paramFourCount; paramNum++) { + String p_itemCheckId = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepfourList().get(paramNum).getProcessId();//关联检查项目id + if(p_itemCheckId.equals(itemCheckId)){ + //工序卡1 + WorkorderParamStepfour paramStepfour = template.getWorkorderStepList().get(stepNum).getWorkorderProcessList().get(processNum).getWorkorderItemCheckList().get(itemNum).getWorkorderParamStepfourList().get(paramNum); + if (null != paramStepfour) { + //第n行 + //int lineNum = (stepNum * processCount * itemCheckCount) + (itemCheckCount * itemNum) + itemNum; + Row row = sheet.createRow((lineNum)); + //第n行第n列 + Cell process = row.createCell(0); + process.setCellValue(processName); + Cell itemCheck = row.createCell(1); + itemCheck.setCellValue(itemCheckName); + Cell specs = row.createCell(2); + specs.setCellValue(paramStepfour.getSpecs()); + Cell param1 = row.createCell(3); + param1.setCellValue(paramStepfour.getParam1()); + Cell param2 = row.createCell(4); + param2.setCellValue(paramStepfour.getParam2()); + Cell param3 = row.createCell(5); + param3.setCellValue(paramStepfour.getParam3()); + Cell param4 = row.createCell(6); + param4.setCellValue(paramStepfour.getParam4()); + Cell param5 = row.createCell(7); + param5.setCellValue(paramStepfour.getParam5()); + Cell param6 = row.createCell(8); + param6.setCellValue(paramStepfour.getParam6()); + Cell param7 = row.createCell(9); + param7.setCellValue(paramStepfour.getParam7()); + Cell param8 = row.createCell(10); + param8.setCellValue(paramStepfour.getParam8()); + Cell param9 = row.createCell(11); + param9.setCellValue(paramStepfour.getParam9()); + Cell param10 = row.createCell(12); + param10.setCellValue(paramStepfour.getParam10()); + Cell operator = row.createCell(23); + operator.setCellValue(paramStepfour.getOperator()); + lineNum++; + } else { + break; + } + + } + } + + } + } + } + sheet.addMergedRegion(new CellRangeAddress(firstLine,firstLine+line,0,0));//合并单元格 + firstLine = firstLine+line+1;//改变下一个起始行位置 + } + } + } + + //获取数据 + public WorkorderTemplate getExportData(String templateId){ + //查询工单信息 + WorkorderTemplate workorderTemplate = workorderTemplateService + .lambdaQuery() + .eq(WorkorderTemplate::getId, templateId) + .one(); + + //查询工序卡信息 + List stepList = workorderStepService + .lambdaQuery() + .list(); + + //查询工序信息 + List processList = workorderProcessService + .lambdaQuery() + .list(); + + //查询检查项目信息 + List itemCheckList = workorderItemCheckService + .lambdaQuery() + .list(); + + //根据工单id查询常规参数 + List GeneralSteponeList = workorderGeneralSteponeService + .lambdaQuery() + .eq(WorkorderGeneralStepone::getTemplateId, templateId) + .list(); + List GeneralSteptwoList = workorderGeneralSteptwoService + .lambdaQuery() + .eq(WorkorderGeneralSteptwo::getTemplateId, templateId) + .list(); + List GeneralStepthreeList = workorderGeneralStepthreeService + .lambdaQuery() + .eq(WorkorderGeneralStepthree::getTemplateId, templateId) + .list(); + List GeneralStepfourList = workorderGeneralStepfourService + .lambdaQuery() + .eq(WorkorderGeneralStepfour::getTemplateId, templateId) + .list(); + + //根据工单id查询检查项目参数 + List paramSteponeList = workorderParamSteponeService + .lambdaQuery() + .eq(WorkorderParamStepone::getTemplateId, templateId) + .list(); + List paramSteptwoList = workorderParamSteptwoService + .lambdaQuery() + .eq(WorkorderParamSteptwo::getTemplateId, templateId) + .list(); + List paramStepthreeList = workorderParamStepthreeService + .lambdaQuery() + .eq(WorkorderParamStepthree::getTemplateId, templateId) + .list(); + List paramStepfourList = workorderParamStepfourService + .lambdaQuery() + .eq(WorkorderParamStepfour::getTemplateId, templateId) + .list(); + + //一、将检查项目参数对象放入对应检查项目对象下 + for (WorkorderItemCheck workorderItemCheck : itemCheckList) { + //工序卡1 + List workorderParamSteponeList = new ArrayList<>(); + for (WorkorderParamStepone workorderParamStepone : paramSteponeList) { + if(workorderParamStepone.getProcessId().equals(workorderItemCheck.getId()) ){ + workorderParamSteponeList.add(workorderParamStepone); + } + } + workorderItemCheck.setWorkorderParamSteponeList(workorderParamSteponeList); + + //工序卡2 + List workorderParamSteptwoList = new ArrayList<>(); + for (WorkorderParamSteptwo workorderParamSteptwo : paramSteptwoList) { + if(workorderParamSteptwo.getProcessId().equals(workorderItemCheck.getId())){ + workorderParamSteptwoList.add(workorderParamSteptwo); + } + } + workorderItemCheck.setWorkorderParamSteptwoList(workorderParamSteptwoList); + + //工序卡3 + List workorderParamStepthreeList = new ArrayList<>(); + for (WorkorderParamStepthree workorderParamStepthree : paramStepthreeList) { + if(workorderParamStepthree.getProcessId().equals(workorderItemCheck.getId())){ + workorderParamStepthreeList.add(workorderParamStepthree); + } + } + workorderItemCheck.setWorkorderParamStepthreeList(workorderParamStepthreeList); + + //工序卡4 + List workorderParamStepfourList = new ArrayList<>(); + for (WorkorderParamStepfour workorderParamStepfour : paramStepfourList) { + if(workorderParamStepfour.getProcessId().equals(workorderItemCheck.getId())){ + workorderParamStepfourList.add(workorderParamStepfour); + } + } + workorderItemCheck.setWorkorderParamStepfourList(workorderParamStepfourList); + + } + + //二、将检查项目对象放入对应工序流程对象下 + for (WorkorderProcess workorderProcess : processList) { + List workorderItemCheckList = new ArrayList<>(); + for (WorkorderItemCheck workorderItemCheck : itemCheckList) { + if(workorderItemCheck.getProcessId().equals(workorderProcess.getId())){ + workorderItemCheckList.add(workorderItemCheck); + } + } + workorderProcess.setWorkorderItemCheckList(workorderItemCheckList); + } + + //三、将工序流程对象放入对应工序卡对象下 + for (WorkorderStep workorderStep : stepList) { + //工序参数 + List workorderProcessList = new ArrayList<>(); + for (WorkorderProcess workorderProcess : processList) { + if (workorderProcess.getStepId().equals(workorderStep.getId())) { + workorderProcessList.add(workorderProcess); + } + } + workorderStep.setWorkorderProcessList(workorderProcessList); + + //常规参数(待优化--冗余list) + //工序卡1 + List workorderGeneralSteponeList = new ArrayList<>(); + for (WorkorderGeneralStepone generalStepone : GeneralSteponeList) { + if (generalStepone.getStepId().equals(workorderStep.getId())) { + workorderGeneralSteponeList.add(generalStepone); + } + } + workorderStep.setWorkorderGeneralSteponeList(workorderGeneralSteponeList); + + //工序卡2 + List workorderGeneralSteptwoList = new ArrayList<>(); + for (WorkorderGeneralSteptwo generalSteptwo : GeneralSteptwoList) { + if (generalSteptwo.getStepId().equals(workorderStep.getId())) { + workorderGeneralSteptwoList.add(generalSteptwo); + } + } + workorderStep.setWorkorderGeneralSteptwoList(workorderGeneralSteptwoList); + + //工序卡3 + List workorderGeneralStepthreeList = new ArrayList<>(); + for (WorkorderGeneralStepthree generalStepthree : GeneralStepthreeList) { + if (generalStepthree.getStepId().equals(workorderStep.getId())) { + workorderGeneralStepthreeList.add(generalStepthree); + } + } + workorderStep.setWorkorderGeneralStepthreeList(workorderGeneralStepthreeList); + + //工序卡4 + List workorderGeneralStepfourList = new ArrayList<>(); + for (WorkorderGeneralStepfour generalStepfour : GeneralStepfourList) { + if (generalStepfour.getStepId().equals(workorderStep.getId())) { + workorderGeneralStepfourList.add(generalStepfour); + } + } + workorderStep.setWorkorderGeneralStepfourList(workorderGeneralStepfourList); + } + + //四、将工序卡对象放入对应工单对象下 + workorderTemplate.setWorkorderStepList(stepList); + + return workorderTemplate; + } + +} diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelClassField.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelClassField.java new file mode 100644 index 0000000..a70023a --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelClassField.java @@ -0,0 +1,77 @@ +package org.jeecg.modules.workorderTemplate.util; + +import java.util.LinkedHashMap; + +/** + * @author sunnyzyq + * @date 2021/12/17 + */ +public class ExcelClassField { + + /** 字段名称 */ + private String fieldName; + + /** 表头名称 */ + private String name; + + /** 映射关系 */ + private LinkedHashMap kvMap; + + /** 示例值 */ + private Object example; + + /** 排序 */ + private int sort; + + /** 是否为注解字段:0-否,1-是 */ + private int hasAnnotation; + + public String getFieldName() { + return fieldName; + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LinkedHashMap getKvMap() { + return kvMap; + } + + public void setKvMap(LinkedHashMap kvMap) { + this.kvMap = kvMap; + } + + public Object getExample() { + return example; + } + + public void setExample(Object example) { + this.example = example; + } + + public int getSort() { + return sort; + } + + public void setSort(int sort) { + this.sort = sort; + } + + public int getHasAnnotation() { + return hasAnnotation; + } + + public void setHasAnnotation(int hasAnnotation) { + this.hasAnnotation = hasAnnotation; + } + +} diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelExport.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelExport.java new file mode 100644 index 0000000..322b15f --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelExport.java @@ -0,0 +1,28 @@ +package org.jeecg.modules.workorderTemplate.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author sunnyzyq + * @date 2021/12/17 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ExcelExport { + + /** 字段名称 */ + String value(); + + /** 导出排序先后: 数字越小越靠前(默认按Java类字段顺序导出) */ + int sort() default 0; + + /** 导出映射,格式如:0-未知;1-男;2-女 */ + String kv() default ""; + + /** 导出模板示例值(有值的话,直接取该值,不做映射) */ + String example() default ""; + +} diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelImport.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelImport.java new file mode 100644 index 0000000..af8ab50 --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelImport.java @@ -0,0 +1,31 @@ +package org.jeecg.modules.workorderTemplate.util; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author sunnyzyq + * @date 2021/12/17 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ExcelImport { + + /** 字段名称 */ + String value(); + + /** 导出映射,格式如:0-未知;1-男;2-女 */ + String kv() default ""; + + /** 是否为必填字段(默认为非必填) */ + boolean required() default false; + + /** 最大长度(默认255) */ + int maxLength() default 255; + + /** 导入唯一性验证(多个字段则取联合验证) */ + boolean unique() default false; + +} diff --git a/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelUtils.java b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelUtils.java new file mode 100644 index 0000000..481ba01 --- /dev/null +++ b/jeecg-boot-base/jeecg-boot-base-core/src/main/java/org/jeecg/modules/workorderTemplate/util/ExcelUtils.java @@ -0,0 +1,1006 @@ +package org.jeecg.modules.workorderTemplate.util; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.apache.poi.hssf.usermodel.HSSFDataValidation; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.usermodel.ClientAnchor.AnchorType; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.CellRangeAddressList; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFClientAnchor; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.lang.reflect.Field; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.net.URL; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.Map.Entry; +import java.util.regex.Pattern; + +/** + * Excel导入导出工具类 + * 原文链接(不定时增加新功能): https://zyqok.blog.csdn.net/article/details/121994504 + * + * @author sunnyzyq + * @date 2021/12/17 + */ +@SuppressWarnings("unused") +public class ExcelUtils { + + private static final String XLSX = ".xlsx"; + private static final String XLS = ".xls"; + public static final String ROW_MERGE = "row_merge"; + public static final String COLUMN_MERGE = "column_merge"; + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + private static final String ROW_NUM = "rowNum"; + private static final String ROW_DATA = "rowData"; + private static final String ROW_TIPS = "rowTips"; + private static final int CELL_OTHER = 0; + private static final int CELL_ROW_MERGE = 1; + private static final int CELL_COLUMN_MERGE = 2; + private static final int IMG_HEIGHT = 30; + private static final int IMG_WIDTH = 30; + private static final char LEAN_LINE = '/'; + private static final int BYTES_DEFAULT_LENGTH = 10240; + private static final NumberFormat NUMBER_FORMAT = NumberFormat.getNumberInstance(); + + + public static List readFile(File file, Class clazz) throws Exception { + JSONArray array = readFile(file); + return getBeanList(array, clazz); + } + + public static List readMultipartFile(MultipartFile mFile, Class clazz) throws Exception { + JSONArray array = readMultipartFile(mFile); + return getBeanList(array, clazz); + } + + public static JSONArray readFile(File file) throws Exception { + return readExcel(null, file); + } + + public static JSONArray readMultipartFile(MultipartFile mFile) throws Exception { + return readExcel(mFile, null); + } + + public static Map readFileManySheet(File file) throws Exception { + return readExcelManySheet(null, file); + } + + public static Map readFileManySheet(MultipartFile file) throws Exception { + return readExcelManySheet(file, null); + } + + private static List getBeanList(JSONArray array, Class clazz) throws Exception { + List list = new ArrayList<>(); + Map uniqueMap = new HashMap<>(16); + for (int i = 0; i < array.size(); i++) { + list.add(getBean(clazz, array.getJSONObject(i), uniqueMap)); + } + return list; + } + + /** + * 获取每个对象的数据 + */ + private static T getBean(Class c, JSONObject obj, Map uniqueMap) throws Exception { + T t = c.newInstance(); + Field[] fields = c.getDeclaredFields(); + List errMsgList = new ArrayList<>(); + boolean hasRowTipsField = false; + StringBuilder uniqueBuilder = new StringBuilder(); + int rowNum = 0; + for (Field field : fields) { + // 行号 + if (field.getName().equals(ROW_NUM)) { + rowNum = obj.getInteger(ROW_NUM); + field.setAccessible(true); + field.set(t, rowNum); + continue; + } + // 是否需要设置异常信息 + if (field.getName().equals(ROW_TIPS)) { + hasRowTipsField = true; + continue; + } + // 原始数据 + if (field.getName().equals(ROW_DATA)) { + field.setAccessible(true); + field.set(t, obj.toString()); + continue; + } + // 设置对应属性值 + setFieldValue(t, field, obj, uniqueBuilder, errMsgList); + } + // 数据唯一性校验 + if (uniqueBuilder.length() > 0) { + if (uniqueMap.containsValue(uniqueBuilder.toString())) { + Set rowNumKeys = uniqueMap.keySet(); + for (Integer num : rowNumKeys) { + if (uniqueMap.get(num).equals(uniqueBuilder.toString())) { + errMsgList.add(String.format("数据唯一性校验失败,(%s)与第%s行重复)", uniqueBuilder, num)); + } + } + } else { + uniqueMap.put(rowNum, uniqueBuilder.toString()); + } + } + // 失败处理 + if (errMsgList.isEmpty() && !hasRowTipsField) { + return t; + } + StringBuilder sb = new StringBuilder(); + int size = errMsgList.size(); + for (int i = 0; i < size; i++) { + if (i == size - 1) { + sb.append(errMsgList.get(i)); + } else { + sb.append(errMsgList.get(i)).append(";"); + } + } + // 设置错误信息 + for (Field field : fields) { + if (field.getName().equals(ROW_TIPS)) { + field.setAccessible(true); + field.set(t, sb.toString()); + } + } + return t; + } + + private static void setFieldValue(T t, Field field, JSONObject obj, StringBuilder uniqueBuilder, List errMsgList) { + // 获取 ExcelImport 注解属性 + ExcelImport annotation = field.getAnnotation(ExcelImport.class); + if (annotation == null) { + return; + } + String cname = annotation.value(); + if (cname.trim().length() == 0) { + return; + } + // 获取具体值 + String val = null; + if (obj.containsKey(cname)) { + val = getString(obj.getString(cname)); + } + if (val == null) { + return; + } + field.setAccessible(true); + // 判断是否必填 + boolean require = annotation.required(); + if (require && val.isEmpty()) { + errMsgList.add(String.format("[%s]不能为空", cname)); + return; + } + // 数据唯一性获取 + boolean unique = annotation.unique(); + if (unique) { + if (uniqueBuilder.length() > 0) { + uniqueBuilder.append("--").append(val); + } else { + uniqueBuilder.append(val); + } + } + // 判断是否超过最大长度 + int maxLength = annotation.maxLength(); + if (maxLength > 0 && val.length() > maxLength) { + errMsgList.add(String.format("[%s]长度不能超过%s个字符(当前%s个字符)", cname, maxLength, val.length())); + } + // 判断当前属性是否有映射关系 + LinkedHashMap kvMap = getKvMap(annotation.kv()); + if (!kvMap.isEmpty()) { + boolean isMatch = false; + for (String key : kvMap.keySet()) { + if (kvMap.get(key).equals(val)) { + val = key; + isMatch = true; + break; + } + } + if (!isMatch) { + errMsgList.add(String.format("[%s]的值不正确(当前值为%s)", cname, val)); + return; + } + } + // 其余情况根据类型赋值 + String fieldClassName = field.getType().getSimpleName(); + try { + if ("String".equalsIgnoreCase(fieldClassName)) { + field.set(t, val); + } else if ("boolean".equalsIgnoreCase(fieldClassName)) { + field.set(t, Boolean.valueOf(val)); + } else if ("int".equalsIgnoreCase(fieldClassName) || "Integer".equals(fieldClassName)) { + try { + field.set(t, Integer.valueOf(val)); + } catch (NumberFormatException e) { + errMsgList.add(String.format("[%s]的值格式不正确(当前值为%s)", cname, val)); + } + } else if ("double".equalsIgnoreCase(fieldClassName)) { + field.set(t, Double.valueOf(val)); + } else if ("long".equalsIgnoreCase(fieldClassName)) { + field.set(t, Long.valueOf(val)); + } else if ("BigDecimal".equalsIgnoreCase(fieldClassName)) { + field.set(t, new BigDecimal(val)); + } else if ("Date".equalsIgnoreCase(fieldClassName)) { + try { + field.set(t, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(val)); + } catch (Exception e) { + field.set(t, new SimpleDateFormat("yyyy-MM-dd").parse(val)); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static Map readExcelManySheet(MultipartFile mFile, File file) throws IOException { + Workbook book = getWorkbook(mFile, file); + if (book == null) { + return Collections.emptyMap(); + } + Map map = new LinkedHashMap<>(); + for (int i = 0; i < book.getNumberOfSheets(); i++) { + Sheet sheet = book.getSheetAt(i); + JSONArray arr = readSheet(sheet); + map.put(sheet.getSheetName(), arr); + } + book.close(); + return map; + } + + private static JSONArray readExcel(MultipartFile mFile, File file) throws IOException { + Workbook book = getWorkbook(mFile, file); + if (book == null) { + return new JSONArray(); + } + JSONArray array = readSheet(book.getSheetAt(0)); + book.close(); + return array; + } + + private static Workbook getWorkbook(MultipartFile mFile, File file) throws IOException { + boolean fileNotExist = (file == null || !file.exists()); + if (mFile == null && fileNotExist) { + return null; + } + // 解析表格数据 + InputStream in; + String fileName; + if (mFile != null) { + // 上传文件解析 + in = mFile.getInputStream(); + fileName = getString(mFile.getOriginalFilename()).toLowerCase(); + } else { + // 本地文件解析 + in = new FileInputStream(file); + fileName = file.getName().toLowerCase(); + } + Workbook book; + if (fileName.endsWith(XLSX)) { + book = new XSSFWorkbook(in); + } else if (fileName.endsWith(XLS)) { + POIFSFileSystem poifsFileSystem = new POIFSFileSystem(in); + book = new HSSFWorkbook(poifsFileSystem); + } else { + return null; + } + in.close(); + return book; + } + + private static JSONArray readSheet(Sheet sheet) { + // 首行下标 + int rowStart = sheet.getFirstRowNum(); + // 尾行下标 + int rowEnd = sheet.getLastRowNum(); + // 获取表头行 + Row headRow = sheet.getRow(rowStart); + if (headRow == null) { + return new JSONArray(); + } + int cellStart = headRow.getFirstCellNum(); + int cellEnd = headRow.getLastCellNum(); + Map keyMap = new HashMap<>(); + for (int j = cellStart; j < cellEnd; j++) { + // 获取表头数据 + String val = getCellValue(headRow.getCell(j)); + if (val != null && val.trim().length() != 0) { + keyMap.put(j, val); + } + } + // 如果表头没有数据则不进行解析 + if (keyMap.isEmpty()) { + return (JSONArray) Collections.emptyList(); + } + // 获取每行JSON对象的值 + JSONArray array = new JSONArray(); + // 如果首行与尾行相同,表明只有一行,返回表头数据 + if (rowStart == rowEnd) { + JSONObject obj = new JSONObject(); + // 添加行号 + obj.put(ROW_NUM, 1); + for (int i : keyMap.keySet()) { + obj.put(keyMap.get(i), ""); + } + array.add(obj); + return array; + } + for (int i = rowStart + 1; i <= rowEnd; i++) { + Row eachRow = sheet.getRow(i); + JSONObject obj = new JSONObject(); + // 添加行号 + obj.put(ROW_NUM, i + 1); + StringBuilder sb = new StringBuilder(); + for (int k = cellStart; k < cellEnd; k++) { + if (eachRow != null) { + String val = getCellValue(eachRow.getCell(k)); + // 所有数据添加到里面,用于判断该行是否为空 + sb.append(val); + obj.put(keyMap.get(k), val); + } + } + if (sb.length() > 0) { + array.add(obj); + } + } + return array; + } + + private static String getCellValue(Cell cell) { + // 空白或空 + if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) { + return ""; + } + // String类型 + if (cell.getCellTypeEnum() == CellType.STRING) { + String val = cell.getStringCellValue(); + if (val == null || val.trim().length() == 0) { + return ""; + } + return val.trim(); + } + // 数字类型 + if (cell.getCellTypeEnum() == CellType.NUMERIC) { + String s = cell.getNumericCellValue() + ""; + // 去掉尾巴上的小数点0 + if (Pattern.matches(".*\\.0*", s)) { + return s.split("\\.")[0]; + } else { + return s; + } + } + // 布尔值类型 + if (cell.getCellTypeEnum() == CellType.BOOLEAN) { + return cell.getBooleanCellValue() + ""; + } + // 错误类型 + return cell.getCellFormula(); + } + + public static void exportTemplate(HttpServletResponse response, String fileName, Class clazz) { + exportTemplate(response, fileName, fileName, clazz, false); + } + + public static void exportTemplate(HttpServletResponse response, String fileName, String sheetName, + Class clazz) { + exportTemplate(response, fileName, sheetName, clazz, false); + } + + public static void exportTemplate(HttpServletResponse response, String fileName, Class clazz, + boolean isContainExample) { + exportTemplate(response, fileName, fileName, clazz, isContainExample); + } + + public static void exportTemplate(HttpServletResponse response, String fileName, String sheetName, + Class clazz, boolean isContainExample) { + // 获取表头字段 + List headFieldList = getExcelClassFieldList(clazz); + // 获取表头数据和示例数据 + List> sheetDataList = new ArrayList<>(); + List headList = new ArrayList<>(); + List exampleList = new ArrayList<>(); + Map> selectMap = new LinkedHashMap<>(); + for (int i = 0; i < headFieldList.size(); i++) { + ExcelClassField each = headFieldList.get(i); + headList.add(each.getName()); + exampleList.add(each.getExample()); + LinkedHashMap kvMap = each.getKvMap(); + if (kvMap != null && kvMap.size() > 0) { + selectMap.put(i, new ArrayList<>(kvMap.values())); + } + } + sheetDataList.add(headList); + if (isContainExample) { + sheetDataList.add(exampleList); + } + // 导出数据 + export(response, fileName, sheetName, sheetDataList, selectMap); + } + + private static List getExcelClassFieldList(Class clazz) { + // 解析所有字段 + Field[] fields = clazz.getDeclaredFields(); + boolean hasExportAnnotation = false; + Map> map = new LinkedHashMap<>(); + List sortList = new ArrayList<>(); + for (Field field : fields) { + ExcelClassField cf = getExcelClassField(field); + if (cf.getHasAnnotation() == 1) { + hasExportAnnotation = true; + } + int sort = cf.getSort(); + if (map.containsKey(sort)) { + map.get(sort).add(cf); + } else { + List list = new ArrayList<>(); + list.add(cf); + sortList.add(sort); + map.put(sort, list); + } + } + Collections.sort(sortList); + // 获取表头 + List headFieldList = new ArrayList<>(); + if (hasExportAnnotation) { + for (Integer sort : sortList) { + for (ExcelClassField cf : map.get(sort)) { + if (cf.getHasAnnotation() == 1) { + headFieldList.add(cf); + } + } + } + } else { + headFieldList.addAll(map.get(0)); + } + return headFieldList; + } + + private static ExcelClassField getExcelClassField(Field field) { + ExcelClassField cf = new ExcelClassField(); + String fieldName = field.getName(); + cf.setFieldName(fieldName); + ExcelExport annotation = field.getAnnotation(ExcelExport.class); + // 无 ExcelExport 注解情况 + if (annotation == null) { + cf.setHasAnnotation(0); + cf.setName(fieldName); + cf.setSort(0); + return cf; + } + // 有 ExcelExport 注解情况 + cf.setHasAnnotation(1); + cf.setName(annotation.value()); + String example = getString(annotation.example()); + if (!example.isEmpty()) { + if (isNumeric(example) && example.length() < 8) { + cf.setExample(Double.valueOf(example)); + } else { + cf.setExample(example); + } + } else { + cf.setExample(""); + } + cf.setSort(annotation.sort()); + // 解析映射 + String kv = getString(annotation.kv()); + cf.setKvMap(getKvMap(kv)); + return cf; + } + + private static LinkedHashMap getKvMap(String kv) { + LinkedHashMap kvMap = new LinkedHashMap<>(); + if (kv.isEmpty()) { + return kvMap; + } + String[] kvs = kv.split(";"); + if (kvs.length == 0) { + return kvMap; + } + for (String each : kvs) { + String[] eachKv = getString(each).split("-"); + if (eachKv.length != 2) { + continue; + } + String k = eachKv[0]; + String v = eachKv[1]; + if (k.isEmpty() || v.isEmpty()) { + continue; + } + kvMap.put(k, v); + } + return kvMap; + } + + /** + * 导出表格到本地 + * + * @param file 本地文件对象 + * @param sheetData 导出数据 + */ + public static void exportFile(File file, List> sheetData) { + if (file == null) { + System.out.println("文件创建失败"); + return; + } + if (sheetData == null) { + sheetData = new ArrayList<>(); + } + Map>> map = new HashMap<>(); + map.put(file.getName(), sheetData); + export(null, file, file.getName(), map, null); + } + + /** + * 导出表格到本地 + * + * @param 导出数据类似,和K类型保持一致 + * @param filePath 文件父路径(如:D:/doc/excel/) + * @param fileName 文件名称(不带尾缀,如:学生表) + * @param list 导出数据 + * @throws IOException IO异常 + */ + public static File exportFile(String filePath, String fileName, List list) throws IOException { + File file = getFile(filePath, fileName); + List> sheetData = getSheetData(list); + exportFile(file, sheetData); + return file; + } + + /** + * 获取文件 + * + * @param filePath filePath 文件父路径(如:D:/doc/excel/) + * @param fileName 文件名称(不带尾缀,如:用户表) + * @return 本地File文件对象 + */ + private static File getFile(String filePath, String fileName) throws IOException { + String dirPath = getString(filePath); + String fileFullPath; + if (dirPath.isEmpty()) { + fileFullPath = fileName; + } else { + // 判定文件夹是否存在,如果不存在,则级联创建 + File dirFile = new File(dirPath); + if (!dirFile.exists()) { + boolean mkdirs = dirFile.mkdirs(); + if (!mkdirs) { + return null; + } + } + // 获取文件夹全名 + if (dirPath.endsWith(String.valueOf(LEAN_LINE))) { + fileFullPath = dirPath + fileName + XLSX; + } else { + fileFullPath = dirPath + LEAN_LINE + fileName + XLSX; + } + } + System.out.println(fileFullPath); + File file = new File(fileFullPath); + if (!file.exists()) { + boolean result = file.createNewFile(); + if (!result) { + return null; + } + } + return file; + } + + private static List> getSheetData(List list) { + // 获取表头字段 + List excelClassFieldList = getExcelClassFieldList(list.get(0).getClass()); + List headFieldList = new ArrayList<>(); + List headList = new ArrayList<>(); + Map headFieldMap = new HashMap<>(); + for (ExcelClassField each : excelClassFieldList) { + String fieldName = each.getFieldName(); + headFieldList.add(fieldName); + headFieldMap.put(fieldName, each); + headList.add(each.getName()); + } + // 添加表头名称 + List> sheetDataList = new ArrayList<>(); + sheetDataList.add(headList); + // 获取表数据 + for (T t : list) { + Map fieldDataMap = getFieldDataMap(t); + Set fieldDataKeys = fieldDataMap.keySet(); + List rowList = new ArrayList<>(); + for (String headField : headFieldList) { + if (!fieldDataKeys.contains(headField)) { + continue; + } + Object data = fieldDataMap.get(headField); + if (data == null) { + rowList.add(""); + continue; + } + ExcelClassField cf = headFieldMap.get(headField); + // 判断是否有映射关系 + LinkedHashMap kvMap = cf.getKvMap(); + if (kvMap == null || kvMap.isEmpty()) { + rowList.add(data); + continue; + } + String val = kvMap.get(data.toString()); + if (isNumeric(val)) { + rowList.add(Double.valueOf(val)); + } else { + rowList.add(val); + } + } + sheetDataList.add(rowList); + } + return sheetDataList; + } + + private static Map getFieldDataMap(T t) { + Map map = new HashMap<>(); + Field[] fields = t.getClass().getDeclaredFields(); + try { + for (Field field : fields) { + String fieldName = field.getName(); + field.setAccessible(true); + Object object = field.get(t); + map.put(fieldName, object); + } + } catch (IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); + } + return map; + } + + public static void exportEmpty(HttpServletResponse response, String fileName) { + List> sheetDataList = new ArrayList<>(); + List headList = new ArrayList<>(); + headList.add("导出无数据"); + sheetDataList.add(headList); + export(response, fileName, sheetDataList); + } + + public static void export(HttpServletResponse response, String fileName, List> sheetDataList) { + export(response, fileName, fileName, sheetDataList, null); + } + + public static void exportManySheet(HttpServletResponse response, String fileName, Map>> sheetMap) { + export(response, null, fileName, sheetMap, null); + } + + + public static void export(HttpServletResponse response, String fileName, String sheetName, + List> sheetDataList) { + export(response, fileName, sheetName, sheetDataList, null); + } + + public static void export(HttpServletResponse response, String fileName, String sheetName, + List> sheetDataList, Map> selectMap) { + + Map>> map = new HashMap<>(); + map.put(sheetName, sheetDataList); + export(response, null, fileName, map, selectMap); + } + + public static void export(HttpServletResponse response, String fileName, List list, Class template) { + // list 是否为空 + boolean lisIsEmpty = list == null || list.isEmpty(); + // 如果模板数据为空,且导入的数据为空,则导出空文件 + if (template == null && lisIsEmpty) { + exportEmpty(response, fileName); + return; + } + // 如果 list 数据,则导出模板数据 + if (lisIsEmpty) { + exportTemplate(response, fileName, template); + return; + } + // 导出数据 + List> sheetDataList = getSheetData(list); + export(response, fileName, sheetDataList); + } + + public static void export(HttpServletResponse response, String fileName, List> sheetDataList, Map> selectMap) { + export(response, fileName, fileName, sheetDataList, selectMap); + } + + private static void export(HttpServletResponse response, File file, String fileName, + Map>> sheetMap, Map> selectMap) { + // 整个 Excel 表格 book 对象 + SXSSFWorkbook book = new SXSSFWorkbook(); + // 每个 Sheet 页 + Set>>> entries = sheetMap.entrySet(); + for (Entry>> entry : entries) { + List> sheetDataList = entry.getValue(); + Sheet sheet = book.createSheet(entry.getKey()); + Drawing patriarch = sheet.createDrawingPatriarch(); + // 设置表头背景色(灰色) + CellStyle headStyle = book.createCellStyle(); + headStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.index); + headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + headStyle.setAlignment(HorizontalAlignment.CENTER); + headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index); + // 设置表身背景色(默认色) + CellStyle rowStyle = book.createCellStyle(); + rowStyle.setAlignment(HorizontalAlignment.CENTER); + rowStyle.setVerticalAlignment(VerticalAlignment.CENTER); + // 设置表格列宽度(默认为15个字节) + sheet.setDefaultColumnWidth(15); + // 创建合并算法数组 + int rowLength = sheetDataList.size(); + int columnLength = sheetDataList.get(0).size(); + int[][] mergeArray = new int[rowLength][columnLength]; + for (int i = 0; i < sheetDataList.size(); i++) { + // 每个 Sheet 页中的行数据 + Row row = sheet.createRow(i); + List rowList = sheetDataList.get(i); + for (int j = 0; j < rowList.size(); j++) { + // 每个行数据中的单元格数据 + Object o = rowList.get(j); + int v = 0; + if (o instanceof URL) { + // 如果要导出图片的话, 链接需要传递 URL 对象 + setCellPicture(book, row, patriarch, i, j, (URL) o); + } else { + Cell cell = row.createCell(j); + if (i == 0) { + // 第一行为表头行,采用灰色底背景 + v = setCellValue(cell, o, headStyle); + } else { + // 其他行为数据行,默认白底色 + v = setCellValue(cell, o, rowStyle); + } + } + mergeArray[i][j] = v; + } + } + // 合并单元格 + mergeCells(sheet, mergeArray); + // 设置下拉列表 + setSelect(sheet, selectMap); + } + // 写数据 + if (response != null) { + // 前端导出 + try { + write(response, book, fileName); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + // 本地导出 + FileOutputStream fos; + try { + fos = new FileOutputStream(file); + ByteArrayOutputStream ops = new ByteArrayOutputStream(); + book.write(ops); + fos.write(ops.toByteArray()); + fos.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 合并当前Sheet页的单元格 + * + * @param sheet 当前 sheet 页 + * @param mergeArray 合并单元格算法 + */ + private static void mergeCells(Sheet sheet, int[][] mergeArray) { + // 横向合并 + for (int x = 0; x < mergeArray.length; x++) { + int[] arr = mergeArray[x]; + boolean merge = false; + int y1 = 0; + int y2 = 0; + for (int y = 0; y < arr.length; y++) { + int value = arr[y]; + if (value == CELL_COLUMN_MERGE) { + if (!merge) { + y1 = y; + } + y2 = y; + merge = true; + } else { + merge = false; + if (y1 > 0) { + sheet.addMergedRegion(new CellRangeAddress(x, x, (y1 - 1), y2)); + } + y1 = 0; + y2 = 0; + } + } + if (y1 > 0) { + sheet.addMergedRegion(new CellRangeAddress(x, x, (y1 - 1), y2)); + } + } + // 纵向合并 + int xLen = mergeArray.length; + int yLen = mergeArray[0].length; + for (int y = 0; y < yLen; y++) { + boolean merge = false; + int x1 = 0; + int x2 = 0; + for (int x = 0; x < xLen; x++) { + int value = mergeArray[x][y]; + if (value == CELL_ROW_MERGE) { + if (!merge) { + x1 = x; + } + x2 = x; + merge = true; + } else { + merge = false; + if (x1 > 0) { + sheet.addMergedRegion(new CellRangeAddress((x1 - 1), x2, y, y)); + } + x1 = 0; + x2 = 0; + } + } + if (x1 > 0) { + sheet.addMergedRegion(new CellRangeAddress((x1 - 1), x2, y, y)); + } + } + } + + private static void write(HttpServletResponse response, SXSSFWorkbook book, String fileName) throws IOException { + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setCharacterEncoding("utf-8"); + String name = new String(fileName.getBytes("GBK"), "ISO8859_1") + XLSX; + response.addHeader("Content-Disposition", "attachment;filename=" + name); + ServletOutputStream out = response.getOutputStream(); + book.write(out); + out.flush(); + out.close(); + } + + private static int setCellValue(Cell cell, Object o, CellStyle style) { + // 设置样式 + cell.setCellStyle(style); + // 数据为空时 + if (o == null) { + cell.setCellType(CellType.STRING); + cell.setCellValue(""); + return CELL_OTHER; + } + // 是否为字符串 + if (o instanceof String) { + String s = o.toString(); + // 当数字类型长度超过8位时,改为字符串类型显示(Excel数字超过一定长度会显示为科学计数法) + if (isNumeric(s) && s.length() < 8) { + cell.setCellType(CellType.NUMERIC); + cell.setCellValue(Double.parseDouble(s)); + return CELL_OTHER; + } else { + cell.setCellType(CellType.STRING); + cell.setCellValue(s); + } + if (s.equals(ROW_MERGE)) { + return CELL_ROW_MERGE; + } else if (s.equals(COLUMN_MERGE)) { + return CELL_COLUMN_MERGE; + } else { + return CELL_OTHER; + } + } + // 是否为字符串 + if (o instanceof Integer || o instanceof Long || o instanceof Double || o instanceof Float) { + cell.setCellType(CellType.NUMERIC); + cell.setCellValue(Double.parseDouble(o.toString())); + return CELL_OTHER; + } + // 是否为Boolean + if (o instanceof Boolean) { + cell.setCellType(CellType.BOOLEAN); + cell.setCellValue((Boolean) o); + return CELL_OTHER; + } + // 如果是BigDecimal,则默认3位小数 + if (o instanceof BigDecimal) { + cell.setCellType(CellType.NUMERIC); + cell.setCellValue(((BigDecimal) o).setScale(3, RoundingMode.HALF_UP).doubleValue()); + return CELL_OTHER; + } + // 如果是Date数据,则显示格式化数据 + if (o instanceof Date) { + cell.setCellType(CellType.STRING); + cell.setCellValue(formatDate((Date) o)); + return CELL_OTHER; + } + // 如果是其他,则默认字符串类型 + cell.setCellType(CellType.STRING); + cell.setCellValue(o.toString()); + return CELL_OTHER; + } + + private static void setCellPicture(SXSSFWorkbook wb, Row sr, Drawing patriarch, int x, int y, URL url) { + // 设置图片宽高 + sr.setHeight((short) (IMG_WIDTH * IMG_HEIGHT)); + // (jdk1.7版本try中定义流可自动关闭) + try (InputStream is = url.openStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + byte[] buff = new byte[BYTES_DEFAULT_LENGTH]; + int rc; + while ((rc = is.read(buff, 0, BYTES_DEFAULT_LENGTH)) > 0) { + outputStream.write(buff, 0, rc); + } + // 设置图片位置 + XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, y, x, y + 1, x + 1); + // 设置这个,图片会自动填满单元格的长宽 + anchor.setAnchorType(AnchorType.MOVE_AND_RESIZE); + patriarch.createPicture(anchor, wb.addPicture(outputStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String formatDate(Date date) { + if (date == null) { + return ""; + } + SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT); + return format.format(date); + } + + private static void setSelect(Sheet sheet, Map> selectMap) { + if (selectMap == null || selectMap.isEmpty()) { + return; + } + Set>> entrySet = selectMap.entrySet(); + for (Entry> entry : entrySet) { + int y = entry.getKey(); + List list = entry.getValue(); + if (list == null || list.isEmpty()) { + continue; + } + String[] arr = new String[list.size()]; + for (int i = 0; i < list.size(); i++) { + arr[i] = list.get(i); + } + DataValidationHelper helper = sheet.getDataValidationHelper(); + CellRangeAddressList addressList = new CellRangeAddressList(1, 65000, y, y); + DataValidationConstraint dvc = helper.createExplicitListConstraint(arr); + DataValidation dv = helper.createValidation(dvc, addressList); + if (dv instanceof HSSFDataValidation) { + dv.setSuppressDropDownArrow(false); + } else { + dv.setSuppressDropDownArrow(true); + dv.setShowErrorBox(true); + } + sheet.addValidationData(dv); + } + } + + private static boolean isNumeric(String str) { + if (Objects.nonNull(str) && "0.0".equals(str)) { + return true; + } + for (int i = str.length(); --i >= 0; ) { + if (!Character.isDigit(str.charAt(i))) { + return false; + } + } + return true; + } + + private static String getString(String s) { + if (s == null) { + return ""; + } + if (s.isEmpty()) { + return s; + } + return s.trim(); + } + +} \ No newline at end of file