From 696678930c8cfc7a02f97cafb8a4b0ac4b33211f Mon Sep 17 00:00:00 2001 From: "yunhao.wang" Date: Sat, 8 Dec 2018 22:11:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E5=8F=B7=E8=A7=84=E5=88=99=E7=AE=A1?= =?UTF-8?q?=E7=90=86excel=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/busi/SysOrderNoRuleController.java | 82 +++++++++++++++----- .../i3plus/core/apiservice/util/ExcelUtil.java | 88 +++++++++++++--------- 2 files changed, 117 insertions(+), 53 deletions(-) diff --git a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysOrderNoRuleController.java b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysOrderNoRuleController.java index 35b170b..b1a696e 100644 --- a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysOrderNoRuleController.java +++ b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/controller/busi/SysOrderNoRuleController.java @@ -65,14 +65,8 @@ public class SysOrderNoRuleController extends CoreBaseController { @ApiOperation(value = "新增单号规则") public ResultBean insertSysOrderNoRule(SysOrderNoRule sysOrderNoRule){ try { - ValidatorBean.beginValid(sysOrderNoRule) - .notNull("name",sysOrderNoRule.getName()) - .notNull("orderNoRuleCode",sysOrderNoRule.getOrderNoRuleCode()) - .notNull("orderNoRule",sysOrderNoRule.getOrderNoRule()) - .checkNotZero("serialNoSeed",sysOrderNoRule.getSerialNoSeed()) - .checkNotZero("serialNoIncrement",sysOrderNoRule.getSerialNoIncrement()) - .checkNotZero("serialNoLength",sysOrderNoRule.getSerialNoLength()) - .checkNotZero("isCycle",sysOrderNoRule.getIsCycle()); + // 添加校验 + validatorSysOrderNoRule(sysOrderNoRule); sysOrderNoRule.setOrderNoRuleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()); sysOrderNoRule.setSerialNo(CommonEnumUtil.PARENT.DEFAULT.getValue()); @@ -276,12 +270,20 @@ public class SysOrderNoRuleController extends CoreBaseController { } } - @GetMapping(value = "/import") + @PostMapping(value = "/import") @ApiOperation(value = "导入单号规则") public ResultBean importSysOrderNoRule(@RequestParam("file") MultipartFile file){ try { - List sysOrderNoRuleList = ExcelUtil.importData(file.getName(),file.getInputStream(),SysOrderNoRule.class); - sysOrderNoRuleService.insertSysOrderNoRuleList(sysOrderNoRuleList); + List sysOrderNoRuleList = ExcelUtil.importData(file.getOriginalFilename(),file.getInputStream(),SysOrderNoRule.class); + for (SysOrderNoRule item : sysOrderNoRuleList) { + //校验及初始化数据 + validatorSysOrderNoRule(item); + item.setOrderNoRuleStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()); + item.setSerialNo(CommonEnumUtil.PARENT.DEFAULT.getValue()); + + sysOrderNoRuleService.insertSysOrderNoRule(item); + } + return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()); }catch(ImppBusiException busExcep){ return ResultBean.fail(busExcep); @@ -290,17 +292,44 @@ public class SysOrderNoRuleController extends CoreBaseController { } } - @GetMapping(value = "/import-template") + @GetMapping(value = "/down-template") @ApiOperation(value = "下载导入模板") - public ResultBean importSysOrderNoRuleTemplate(HttpServletResponse response, String[] colName){ + public ResultBean downSysOrderNoRuleTemplate(){ try { - response.setContentType("application/force-download");// 设置强制下载不打开 - response.addHeader("Content-Disposition", "attachment;fileName=" + SysOrderNoRule.class.getSimpleName() + "Template.xls");// 设置文件名 - response.addHeader("Content-type", "application/octet-stream");// 设置文件名 - -// ExcelUtil.importTemplate(response.getOutputStream(),SysOrderNoRule.class, colName); + FastDFSFile fastDFSFile = new FastDFSFile(SysOrderNoRule.class.getSimpleName() + "ImportTemplate.xls", + ExcelUtil.importTemplate(SysOrderNoRule.class),"xls"); + + String[] fileAbsolutePath = dfsClient.upload(fastDFSFile); + + // 保证系统文件表中 + String filePath = dfsClient.getHostUi() + "/" + fileAbsolutePath[0] + "/" + fileAbsolutePath[1]; + SysDictionary dictionary = sysDictionaryService.getSysDictionaryByParentCodeAndCode( + CommonConstWords.DICTIONARY_FILE_TYPE,fastDFSFile.getExt()); + + SysFile sysFile = new SysFile(); + sysFile.setDfsGroupName(fileAbsolutePath[0]); + sysFile.setDfsFileName(fileAbsolutePath[1]); + sysFile.setFileSize(fastDFSFile.getContent().length); + sysFile.setFilePath(filePath); + sysFile.setFileOriginalName(fastDFSFile.getName()); + sysFile.setFileTypeId(dictionary.getId()); + sysFile.setFileTypeName(dictionary.getName()); + sysFile.setDownloadNum(0); + sysFileService.insertSysFile(sysFile); + + return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(sysFile); + }catch(ImppBusiException busExcep){ + return ResultBean.fail(busExcep); + }catch(Exception e){ + return ImppExceptionBuilder.newInstance().buildExceptionResult(e); + } + } - return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()); + @GetMapping("/get-col") + @ApiOperation(value = "获取单号规则字段") + public ResultBean getSysOrderNoRuleColName(){ + try { + return ResultBean.success("导出成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultMap(ExcelUtil.getColName(SysOrderNoRule.class)); }catch(ImppBusiException busExcep){ return ResultBean.fail(busExcep); }catch(Exception e){ @@ -308,4 +337,19 @@ public class SysOrderNoRuleController extends CoreBaseController { } } + /** + * 对象添加校验 + * @param sysOrderNoRule 校验对象 + * @throws NoSuchFieldException + */ + private void validatorSysOrderNoRule(SysOrderNoRule sysOrderNoRule) throws NoSuchFieldException { + ValidatorBean.beginValid(sysOrderNoRule) + .notNull("name",sysOrderNoRule.getName()) + .notNull("orderNoRuleCode",sysOrderNoRule.getOrderNoRuleCode()) + .notNull("orderNoRule",sysOrderNoRule.getOrderNoRule()) + .checkNotZero("serialNoSeed",sysOrderNoRule.getSerialNoSeed()) + .checkNotZero("serialNoIncrement",sysOrderNoRule.getSerialNoIncrement()) + .checkNotZero("serialNoLength",sysOrderNoRule.getSerialNoLength()) + .checkNotZero("isCycle",sysOrderNoRule.getIsCycle()); + } } diff --git a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/util/ExcelUtil.java b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/util/ExcelUtil.java index b8fd500..532433f 100644 --- a/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/util/ExcelUtil.java +++ b/modules/i3plus-core-apiservice/src/main/java/cn/estsh/i3plus/core/apiservice/util/ExcelUtil.java @@ -63,8 +63,7 @@ public class ExcelUtil { HSSFWorkbook workbook = new HSSFWorkbook(); try { //创建临时文件 - System.out.println(exportClass.getSimpleName() + TimeTool.getNowTime(true)); - excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(),".xls"); + excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), ".xls"); LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath()); fos = new FileOutputStream(excelFile); @@ -192,11 +191,14 @@ public class ExcelUtil { Row row; Object obj; Object cellValue = null; - for (int i = 0; i < sheet.getLastRowNum(); i++) { + for (int i = 1; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); obj = importClass.newInstance(); for (int j = 0; j < fields.length; j++) { - if (fields[j].getType() == String.class) { + row.getCell(j).setCellType(CellType.STRING); + if("".equals(row.getCell(j).getStringCellValue())){ + cellValue = null; + }else if (fields[j].getType() == String.class) { cellValue = row.getCell(j).getStringCellValue(); } else if (fields[j].getType() == Integer.class) { cellValue = Integer.parseInt(row.getCell(j).getStringCellValue()); @@ -204,7 +206,7 @@ public class ExcelUtil { cellValue = Long.parseLong(row.getCell(j).getStringCellValue()); } - importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[i].getName()), fields[i].getType()) + importClass.getDeclaredMethod("set" + StringTool.toUpperCaseFirstOne(fields[j].getName()), fields[j].getType()) .invoke(obj, cellValue); } dataList.add(obj); @@ -252,13 +254,19 @@ public class ExcelUtil { * 导入模板 * * @param exportClass - * @param exportCol */ - public static byte[] importTemplate(Class exportClass, String[] exportCol) { + public static byte[] importTemplate(Class exportClass) { + File excelFile = null; + FileOutputStream fos = null; //创建HSSFWorkbook对象(excel的文档对象) HSSFWorkbook workbook = new HSSFWorkbook(); try { + //创建临时文件 + excelFile = File.createTempFile(exportClass.getSimpleName() + new Date().getTime(), "Tp.xls"); + LOGGER.info("临时文件所在的本地路径:" + excelFile.getCanonicalPath()); + fos = new FileOutputStream(excelFile); + //建立新的sheet对象(excel的表单) HSSFSheet sheet = workbook.createSheet(exportClass.getSimpleName()); @@ -268,27 +276,23 @@ public class ExcelUtil { // 类数据 Field[] declaredFields = exportClass.getDeclaredFields(); - Field[] fields = new Field[exportCol.length]; ApiParam fieldAnno; + int col = 0; - for (int i = 0; i < exportCol.length; i++) { - for (Field field : declaredFields) { - if (field.getName().equals(exportCol[i])) { - fields[i] = field; - - if (field.isAnnotationPresent(ApiParam.class)) { - fieldAnno = field.getAnnotation(ApiParam.class); - tableHeader.createCell(i, CellType.STRING).setCellValue(fieldAnno.value()); + for (int i = 0; i < declaredFields.length; i++) { + if (declaredFields[i].isAnnotationPresent(ApiParam.class)) { + fieldAnno = declaredFields[i].getAnnotation(ApiParam.class); + if (!fieldAnno.hidden()) { + tableHeader.createCell(col, CellType.STRING).setCellValue(fieldAnno.value()); - tableData.createCell(i, CellType.STRING).setCellValue(fieldAnno.example()); - } + tableData.createCell(col, CellType.STRING).setCellValue(fieldAnno.example()); + col++; } } } - ByteArrayOutputStream os = new ByteArrayOutputStream(); - workbook.write(os); - return os.toString("utf-8").getBytes(); + workbook.write(fos); + return file2Byte(excelFile); } catch (IOException e) { throw ImppExceptionBuilder.newInstance() .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode()) @@ -298,12 +302,36 @@ public class ExcelUtil { } finally { try { workbook.close(); + fos.close(); + excelFile.deleteOnExit(); } catch (IOException e) { LOGGER.error("IOException!", e); } } } + /** + * 获取实体列集合 + * + * @param pojoClass + * @return + */ + public static Map getColName(Class pojoClass) { + Map colName = new LinkedHashMap(); + Field[] field = pojoClass.getDeclaredFields(); + ApiParam fieldAnno; + + for (int i = 0; i < field.length; i++) { + if (field[i].isAnnotationPresent(ApiParam.class)) { + fieldAnno = field[i].getAnnotation(ApiParam.class); + if (!fieldAnno.hidden()) { + colName.put(field[i].getAnnotation(ApiParam.class).value(), field[i].getName()); + } + } + } + + return colName; + } public static void main(String[] args) { List sysTools = new ArrayList<>(); @@ -327,30 +355,22 @@ public class ExcelUtil { } - - public static byte[] file2Byte(File file) - { + public static byte[] file2Byte(File file) { byte[] buffer = null; - try - { + try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; - while ((n = fis.read(b)) != -1) - { + while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); - } - catch (FileNotFoundException e) - { + } catch (FileNotFoundException e) { e.printStackTrace(); - } - catch (IOException e) - { + } catch (IOException e) { e.printStackTrace(); } return buffer;