diff --git a/src/main/java/com/gmh/controller/ExcelController.java b/src/main/java/com/gmh/controller/ExcelController.java index 56860bd..1817054 100644 --- a/src/main/java/com/gmh/controller/ExcelController.java +++ b/src/main/java/com/gmh/controller/ExcelController.java @@ -8,6 +8,7 @@ import com.gmh.entity.RowCellNum; import com.gmh.entity.SysBaijiaxing; import com.gmh.entity.vo.ReadNameVo; import com.gmh.service.SysBaijiaxingService; +import com.gmh.utils.CommonUtils; import com.gmh.utils.POIExcelUtil; import com.gmh.utils.RedisUtils; import com.gmh.utils.RedisUtilsDefault; @@ -18,12 +19,16 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import javax.validation.constraints.Pattern; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.*; import java.util.stream.Collectors; @@ -126,41 +131,29 @@ public class ExcelController { } @RequestMapping("/cellNumSearch") - public R analysisCell(String cellNum) { - String cellStrNum = cellNum.replaceAll("[^a-z^A-Z]", ""); - String rowNum = cellNum.replaceAll("[^0-9]", ""); - if (cellStrNum.length() > 2) { - return R.error(cellNum + "单元格不存在"); - } + public R analysisCell(@Validated RowCellNum rowCellNum) { if (this.templateWorkbook == null) { return R.error("模板文件出问题了,请重新上传"); } Sheet sheet = this.templateWorkbook.getSheetAt(this.templateSheetNum); - Row row = sheet.getRow(Integer.parseInt(rowNum) - 1); + rowCellNum = POIExcelUtil.analysisCellRowNum(rowCellNum); + Row row = sheet.getRow(rowCellNum.getRowNum()); if (row == null) { - return R.error(cellNum + "单元格无内容"); + return R.ok().setData(""); } - int i = CellReference.convertColStringToIndex(cellStrNum); - Cell cell = row.getCell(i); + Cell cell = row.getCell(rowCellNum.getCellNum()); if (cell == null) { - return R.error(cellNum + "单元格无内容"); + return R.ok().setData(""); } String stringCellVal = POIExcelUtil.getStringCellVal(cell); if (StrUtil.isEmpty(stringCellVal)) { - return R.error(cellNum + "单元格无内容"); + return R.ok().setData(""); } return R.ok().setData(stringCellVal); } - public RowCellNum analysisCellNum(String cellNum) { - return null; - } - - - @RequestMapping("/executeTemplate") - public R executeTemplate(String newCellData) throws IOException { - System.out.println(newCellData); + public void executeTemplate(String newCellData, String cellRowStr, HttpServletResponse response) throws IOException { String expr = newCellData.substring(newCellData.indexOf("@{"), newCellData.indexOf("}") + 1); String exprVal = newCellData.substring(newCellData.indexOf("@{") + 2, newCellData.indexOf("}")); String[] exprArr = exprVal.split("\\."); @@ -172,32 +165,44 @@ public class ExcelController { .filter(item -> item.getSheetName().equalsIgnoreCase(sheetName)) .findFirst(); if (!first.isPresent()) { - return R.error("未找到sheet页,可能是表达式错误"); + throw new RuntimeException("未找到sheet页,可能是表达式错误"); } ReadNameVo readNameVo = first.get(); - POIExcelUtil.removeModelSheet(this.templateWorkbook, sheetName); + POIExcelUtil.removeModelSheet(this.templateWorkbook, this.templateSheetNum); POIExcelUtil.batchCloneSheet(this.templateWorkbook, this.templateSheetNum, readNameVo.getNameList().size()); ArrayList gmhUsers = new ArrayList<>(readNameVo.getNameList()); + RowCellNum rowCellNum = POIExcelUtil.analysisCellRowNum(cellRowStr); for (int i = 0; i < this.templateWorkbook.getNumberOfSheets(); i++) { Sheet sheet = templateWorkbook.getSheetAt(i); if (sheet == null) { continue; } - Row row = sheet.getRow(2); - Cell cell = row.getCell(0); - cell.setCellValue(newCellData.replace(expr, gmhUsers.get(i).getName())); + Row row = sheet.getRow(rowCellNum.getRowNum()); + Cell cell = row.getCell(rowCellNum.getCellNum()); + cell.setCellValue(newCellData.replace(expr, CommonUtils.nameFormat(gmhUsers.get(i).getName()))); } - try { - FileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\tempfile\\output\\out的222.xls"); - out.flush(); - templateWorkbook.write(out); - out.close(); - System.out.println("文件输出完成"); - } catch (IOException e) { - e.printStackTrace(); - } - return R.ok(); + // TODO 文件保存到本地,文件信息入库 + + // 输出Excel文件 + OutputStream output = response.getOutputStream(); + response.reset(); + // 设置文件头 + response.setHeader("Content-Disposition", + "attchement;filename=" + new String((sheetName + ".xls").getBytes("gb2312"), "ISO8859-1")); + response.setContentType("application/msexcel"); + this.templateWorkbook.write(output); + this.templateWorkbook.close(); + +// try { +// FileOutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\tempfile\\output\\out的222.xls"); +// out.flush(); +// templateWorkbook.write(out); +// out.close(); +// System.out.println("文件输出完成"); +// } catch (IOException e) { +// e.printStackTrace(); +// } } @Autowired diff --git a/src/main/java/com/gmh/controller/GlobalExceptionController.java b/src/main/java/com/gmh/controller/GlobalExceptionController.java new file mode 100644 index 0000000..dcf60df --- /dev/null +++ b/src/main/java/com/gmh/controller/GlobalExceptionController.java @@ -0,0 +1,23 @@ +package com.gmh.controller; + +import com.gmh.entity.R; +import lombok.extern.slf4j.Slf4j; +import org.springframework.validation.BindException; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import java.util.List; + + +@Slf4j +@RestControllerAdvice +public class GlobalExceptionController { + + @ExceptionHandler(BindException.class) + public R handleBindException(BindException e) { + log.error(e.getMessage()); + return R.error(e.getAllErrors().get(0).getDefaultMessage()); + } + +} diff --git a/src/main/java/com/gmh/controller/TestController.java b/src/main/java/com/gmh/controller/TestController.java index 1c08f7b..f746a34 100644 --- a/src/main/java/com/gmh/controller/TestController.java +++ b/src/main/java/com/gmh/controller/TestController.java @@ -5,6 +5,8 @@ import com.gmh.entity.R; import com.gmh.entity.SysBaijiaxing; import com.gmh.service.GmhUserService; import com.gmh.service.SysBaijiaxingService; +import com.gmh.service.TestService; +import com.gmh.service.impl.EnTestServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -22,6 +24,12 @@ public class TestController { private SysBaijiaxingService baijiaxingService; + @Autowired + private TestService enTestServiceImpl; + + @Autowired + private TestService haTestServiceImpl; + @RequestMapping("/test") public R test() { List list = gmhUserService.list(); diff --git a/src/main/java/com/gmh/dao/FileDao.java b/src/main/java/com/gmh/dao/FileDao.java new file mode 100644 index 0000000..ea37e62 --- /dev/null +++ b/src/main/java/com/gmh/dao/FileDao.java @@ -0,0 +1,10 @@ +package com.gmh.dao; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.gmh.entity.FileEntity; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface FileDao extends BaseMapper { + +} \ No newline at end of file diff --git a/src/main/java/com/gmh/entity/BaseEntity.java b/src/main/java/com/gmh/entity/BaseEntity.java new file mode 100644 index 0000000..9f785ee --- /dev/null +++ b/src/main/java/com/gmh/entity/BaseEntity.java @@ -0,0 +1,58 @@ +package com.gmh.entity; + +import com.baomidou.mybatisplus.annotation.*; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; + +import java.io.Serializable; +import java.time.LocalDateTime; + +@Data +public abstract class BaseEntity implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键 + */ + @TableId(value = "id", type = IdType.ASSIGN_ID) + private String id; + + /** + * 创建时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @TableField(value = "create_time", fill = FieldFill.INSERT) + private LocalDateTime createTime; + + /** + * 创建人 + */ + @TableField(value = "create_by", fill = FieldFill.INSERT) + private String createBy; + + /** + * 更新时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) + private LocalDateTime updateTime; + + /** + * 更新人 + */ + @TableField(value = "update_by", fill = FieldFill.INSERT_UPDATE) + private String updateBy; + + /** + * 状态(0不启用,1启用) + */ + @TableField(value = "status", fill = FieldFill.INSERT) + private String status; + + /** + * 备注 + */ + @TableField(value = "remark") + private String remark; +} diff --git a/src/main/java/com/gmh/entity/FileEntity.java b/src/main/java/com/gmh/entity/FileEntity.java new file mode 100644 index 0000000..15e3843 --- /dev/null +++ b/src/main/java/com/gmh/entity/FileEntity.java @@ -0,0 +1,53 @@ +package com.gmh.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +/** + *

+ * + *

+ * + * @author yuwei + * @since 2019-09-17 + */ +@Data +@EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) +@TableName("tbl_file") +public class FileEntity extends BaseEntity { + + private static final long serialVersionUID = 1L; + + /** + * 文件原始名称 + */ + private String originalFilename; + + /** + * 文件名称 + */ + private String fileName; + + /** + * 文件大小 + */ + private Long fileSize; + + /** + * 访问路径 + */ + private String filePath; + + /** + * 文件类型 + */ + private String contentType; + + /** + * 文件来源 + */ + private String fileType; +} diff --git a/src/main/java/com/gmh/entity/RowCellNum.java b/src/main/java/com/gmh/entity/RowCellNum.java index 62c2388..7af1f80 100644 --- a/src/main/java/com/gmh/entity/RowCellNum.java +++ b/src/main/java/com/gmh/entity/RowCellNum.java @@ -1,11 +1,19 @@ package com.gmh.entity; import lombok.Data; +import lombok.experimental.Accessors; + +import javax.validation.constraints.Pattern; @Data +@Accessors(chain = true) public class RowCellNum { private Integer rowNum; private Integer cellNum; + + @Pattern(regexp = "^[A-Za-z]{1,3}[0-9]{1,4}", message = "单元格位置格式不正确") + private String cellRowStr; + } diff --git a/src/main/java/com/gmh/service/FileService.java b/src/main/java/com/gmh/service/FileService.java new file mode 100644 index 0000000..7b1066d --- /dev/null +++ b/src/main/java/com/gmh/service/FileService.java @@ -0,0 +1,12 @@ +package com.gmh.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.gmh.entity.FileEntity; +import org.springframework.web.multipart.MultipartFile; + +public interface FileService extends IService { + + FileEntity uploadFile(MultipartFile file); + + void deleteFileById(String id); +} diff --git a/src/main/java/com/gmh/service/TestService.java b/src/main/java/com/gmh/service/TestService.java new file mode 100644 index 0000000..3fcc6e1 --- /dev/null +++ b/src/main/java/com/gmh/service/TestService.java @@ -0,0 +1,8 @@ +package com.gmh.service; + +public interface TestService { + + String getTest(); + + String giao(String giao); +} diff --git a/src/main/java/com/gmh/service/impl/EnTestServiceImpl.java b/src/main/java/com/gmh/service/impl/EnTestServiceImpl.java new file mode 100644 index 0000000..4e78912 --- /dev/null +++ b/src/main/java/com/gmh/service/impl/EnTestServiceImpl.java @@ -0,0 +1,18 @@ +package com.gmh.service.impl; + +import com.gmh.service.TestService; +import org.springframework.stereotype.Service; + +@Service +public class EnTestServiceImpl implements TestService { + + @Override + public String getTest() { + return "EnTest"; + } + + @Override + public String giao(String giao) { + return "EnTest Giao :" + giao; + } +} diff --git a/src/main/java/com/gmh/service/impl/HaTestServiceImpl.java b/src/main/java/com/gmh/service/impl/HaTestServiceImpl.java new file mode 100644 index 0000000..5787cac --- /dev/null +++ b/src/main/java/com/gmh/service/impl/HaTestServiceImpl.java @@ -0,0 +1,18 @@ +package com.gmh.service.impl; + +import com.gmh.service.TestService; +import org.springframework.stereotype.Service; + +@Service +public class HaTestServiceImpl implements TestService { + + @Override + public String getTest() { + return "HaTest"; + } + + @Override + public String giao(String giao) { + return "HaTest Giao :" + giao; + } +} diff --git a/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java b/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java index 8d411ad..d822f52 100644 --- a/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java +++ b/src/main/java/com/gmh/service/impl/SysBaijiaxingServiceImpl.java @@ -7,6 +7,7 @@ import com.gmh.dao.SysBaijiaxingDao; import com.gmh.entity.SysBaijiaxing; import com.gmh.service.SysBaijiaxingService; import com.gmh.utils.RedisUtils; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -17,6 +18,7 @@ import java.util.Set; import java.util.stream.Collectors; @Service +@Slf4j public class SysBaijiaxingServiceImpl extends ServiceImpl implements SysBaijiaxingService, InitializingBean { @Autowired @@ -40,10 +42,12 @@ public class SysBaijiaxingServiceImpl extends ServiceImpl list = list(); Set collect = list.stream().map(SysBaijiaxing::getXingshi).collect(Collectors.toSet()); for (String xingshi : collect) { redisUtils.setCacheObject(KEY_XINGSHI + xingshi, xingshi); } + log.info("baijiaxing cache loading completed!"); } } diff --git a/src/main/java/com/gmh/utils/CommonUtils.java b/src/main/java/com/gmh/utils/CommonUtils.java new file mode 100644 index 0000000..99b5b69 --- /dev/null +++ b/src/main/java/com/gmh/utils/CommonUtils.java @@ -0,0 +1,14 @@ +package com.gmh.utils; + +public class CommonUtils { + + public static String nameFormat(String name) { + StringBuilder newName = new StringBuilder(name.replaceAll("\\s*", "")); + if (newName.length() == 2) { + newName.insert(1, " "); + return newName.toString(); + } + return name; + + } +} diff --git a/src/main/java/com/gmh/utils/POIExcelUtil.java b/src/main/java/com/gmh/utils/POIExcelUtil.java index e291c1c..c175a67 100644 --- a/src/main/java/com/gmh/utils/POIExcelUtil.java +++ b/src/main/java/com/gmh/utils/POIExcelUtil.java @@ -2,6 +2,7 @@ package com.gmh.utils; import cn.hutool.core.util.StrUtil; import cn.hutool.poi.excel.ExcelFileUtil; +import com.gmh.entity.RowCellNum; import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; @@ -238,4 +239,25 @@ public class POIExcelUtil { //设置默认显示第一页 wb.setActiveSheet(0); } + + /** + * 将单元格逻辑坐标转换为物理坐标 + * @param cellRowStr 字符串形式的单元格坐标 如:A3 + * @return + */ + public static RowCellNum analysisCellRowNum(String cellRowStr) { + return analysisCellRowNum(new RowCellNum().setCellRowStr(cellRowStr)); + } + + /** + * 将单元格逻辑坐标转换为物理坐标 + * @param rowCellNum + * @return + */ + public static RowCellNum analysisCellRowNum(RowCellNum rowCellNum) { + String cellStrNum = rowCellNum.getCellRowStr().replaceAll("[^a-z^A-Z]", ""); + String rowNum = rowCellNum.getCellRowStr().replaceAll("[^0-9]", ""); + int cellIndex = CellReference.convertColStringToIndex(cellStrNum); + return rowCellNum.setCellNum(cellIndex).setRowNum(Integer.parseInt(rowNum) - 1); + } } \ No newline at end of file