diff --git a/bonus-common/bonus-common-core/pom.xml b/bonus-common/bonus-common-core/pom.xml index 7dc51c1..65f2ae9 100644 --- a/bonus-common/bonus-common-core/pom.xml +++ b/bonus-common/bonus-common-core/pom.xml @@ -132,6 +132,25 @@ 6.0.18.Final + + org.freemarker + freemarker + 2.3.30 + + + + cn.afterturn + easypoi-base + 4.2.0 + compile + + + guava + com.google.guava + + + + diff --git a/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/ExcelStyleUtil.java b/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/ExcelStyleUtil.java new file mode 100644 index 0000000..3609698 --- /dev/null +++ b/bonus-common/bonus-common-core/src/main/java/com/bonus/common/core/utils/ExcelStyleUtil.java @@ -0,0 +1,197 @@ +package com.bonus.common.core.utils; + +import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; +import cn.afterturn.easypoi.excel.entity.params.ExcelForEachParams; +import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; +import org.apache.poi.ss.usermodel.*; + +/** + * @author 10488 + * @Auther: ccw + * @Date: 2022/05/12/16:22 + * @description: easypoi 导出表格样式 + */ +public class ExcelStyleUtil implements IExcelExportStyler { + private static final short STRING_FORMAT = (short) BuiltinFormats.getBuiltinFormat("TEXT"); + private static final short FONT_SIZE_TEN = 10; + private static final short FONT_SIZE_ELEVEN = 11; + private static final short FONT_SIZE_TWELVE = 12; + + /** + * 大标题样式 + */ + private CellStyle headerStyle; + + /** + * 每列标题样式 + */ + private CellStyle titleStyle; + + /** + * 数据行样式 + */ + private CellStyle styles; + + public ExcelStyleUtil(Workbook workbook) { + this.init(workbook); + } + + /** + * 初始化样式 + * + * @param workbook + */ + private void init(Workbook workbook) { + this.headerStyle = initHeaderStyle(workbook); + this.titleStyle = initTitleStyle(workbook); + this.styles = initStyles(workbook); + } + + /** + * 大标题样式 + * + * @param color + * @return + */ + + @Override + public CellStyle getHeaderStyle(short color) { + return headerStyle; + } + + /** + * 每列标题样式 + * + * @param color + * @return + */ + + @Override + public CellStyle getTitleStyle(short color) { + return titleStyle; + } + + /** + * 数据行样式 + * + * @param parity 可以用来表示奇偶行 + * @param entity 数据内容 + * @return 样式 + */ + + @Override + public CellStyle getStyles(boolean parity, ExcelExportEntity entity) { + return styles; + } + + /** + * 获取样式方法 + * + * @param dataRow 数据行 + * @param obj 对象 + * @param data 数据 + */ + + @Override + + public CellStyle getStyles(Cell cell, int dataRow, ExcelExportEntity entity, Object obj, Object data) { + return getStyles(true, entity); + } + + /** + * 模板使用的样式设置 + */ + + @Override + public CellStyle getTemplateStyles(boolean isSingle, ExcelForEachParams excelForEachParams) { + return null; + } + + /** + * 初始化--大标题样式 + * + * @param workbook + * @return + */ + + private CellStyle initHeaderStyle(Workbook workbook) { + CellStyle style = getBaseCellStyle(workbook); + style.setFont(getFont(workbook, FONT_SIZE_TWELVE, true)); + return style; + } + + /** + * 初始化--每列标题样式 + * + * @param workbook + * @return + */ + + private CellStyle initTitleStyle(Workbook workbook) { + CellStyle style = getBaseCellStyle(workbook); + style.setFont(getFont(workbook, FONT_SIZE_ELEVEN, false)); + //背景色 + style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); + style.setFillPattern(FillPatternType.SOLID_FOREGROUND); + return style; + + } + + /** + * 初始化--数据行样式 + * + * @param workbook + * @return + */ + private CellStyle initStyles(Workbook workbook) { + CellStyle style = getBaseCellStyle(workbook); + style.setFont(getFont(workbook, FONT_SIZE_TEN, false)); + style.setDataFormat(STRING_FORMAT); + return style; + } + + /** + * 基础样式 + * + * @return + */ + + private CellStyle getBaseCellStyle(Workbook workbook) { + CellStyle style = workbook.createCellStyle(); + //下边框 + style.setBorderBottom(BorderStyle.THIN); + //左边框 + style.setBorderLeft(BorderStyle.THIN); + //上边框 + style.setBorderTop(BorderStyle.THIN); + //右边框 + style.setBorderRight(BorderStyle.THIN); + //水平居中 + style.setAlignment(HorizontalAlignment.CENTER); + //上下居中 + style.setVerticalAlignment(VerticalAlignment.CENTER); + //设置自动换行 + style.setWrapText(true); + return style; + } + + /** + * 字体样式 + * + * @param size 字体大小 + * @param isBold 是否加粗 + * @return + */ + + private Font getFont(Workbook workbook, short size, boolean isBold) { + Font font = workbook.createFont(); + //字体样式 + font.setFontName("宋体"); + //是否加粗 + font.setBold(isBold); + //字体大小 + font.setFontHeightInPoints(size); + return font; + } +} + diff --git a/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/bracelet/exportVo/PersonExportVo.java b/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/bracelet/exportVo/PersonExportVo.java new file mode 100644 index 0000000..ab0dfd7 --- /dev/null +++ b/bonus-common/bonus-common-entity/src/main/java/com/bonus/common/entity/bracelet/exportVo/PersonExportVo.java @@ -0,0 +1,36 @@ +package com.bonus.common.entity.bracelet.exportVo; + +import cn.afterturn.easypoi.excel.annotation.Excel; +import lombok.Data; + +/** + * @className:PersonExportVo + * @author:cwchen + * @date:2024-07-16-21:04 + * @version:1.0 + * @description:人员导出-vo + */ +@Data +public class PersonExportVo { + + @Excel(name = "序号", width = 10.0,height = 20.0, orderNum = "0") + private String id; + + @Excel(name = "姓名", width = 20.0,height = 15.0,orderNum = "1") + private String name; + + @Excel(name = "性别", width = 20.0,height = 15.0,orderNum = "2") + private String sex; + + @Excel(name = "身份证号", width = 20.0,height = 15.0,orderNum = "3") + private String idCard; + + @Excel(name = "电话", width = 20.0,height = 15.0,orderNum = "4") + private String phone; + + @Excel(name = "安全帽编号", width = 20.0,height = 15.0,orderNum = "5") + private String aqmCode; + + @Excel(name = "马甲编号", width = 20.0,height = 15.0,orderNum = "6") + private String mjCode; +} diff --git a/bonus-modules/bonus-bracelet/src/main/java/com/bonus/bracelet/controller/DownLoadFileController.java b/bonus-modules/bonus-bracelet/src/main/java/com/bonus/bracelet/controller/DownLoadFileController.java new file mode 100644 index 0000000..7d53185 --- /dev/null +++ b/bonus-modules/bonus-bracelet/src/main/java/com/bonus/bracelet/controller/DownLoadFileController.java @@ -0,0 +1,59 @@ +package com.bonus.bracelet.controller; + + +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.util.IOUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.InputStream; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + + +/** + * @author:cwchen + * @date:2024-03-11-14:54 + * @version:1.0 + * @description:文件模板下载 + */ +@RestController +@RequestMapping("/download/") +@Slf4j +public class DownLoadFileController { + + /** + * 人员模板-下载模板 + * @param request + * @param response + * @return void + * @author cwchen + * @date 2024/7/16 20:40 + */ + @GetMapping("personFile") + public void personFile(HttpServletRequest request, HttpServletResponse response) { + InputStream inputStream = null; + ServletOutputStream servletOutputStream = null; + try { + String path = "download/" + "person_model.xlsx"; + inputStream = this.getClass().getClassLoader().getResourceAsStream(path); + response.setContentType("application/vnd.ms-excel"); + response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); + response.addHeader("charset", "utf-8"); + response.addHeader("Pragma", "no-cache"); + String encodeName = URLEncoder.encode("person_model.xlsx", StandardCharsets.UTF_8.toString()); + response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName); + servletOutputStream = response.getOutputStream(); + IOUtils.copy(inputStream, servletOutputStream); + response.flushBuffer(); + } catch (Exception e) { + log.error("人员模板-下载失败", e); + } + } + + +} diff --git a/bonus-modules/bonus-bracelet/src/main/java/com/bonus/bracelet/controller/ExportFileController.java b/bonus-modules/bonus-bracelet/src/main/java/com/bonus/bracelet/controller/ExportFileController.java new file mode 100644 index 0000000..3d1fd5d --- /dev/null +++ b/bonus-modules/bonus-bracelet/src/main/java/com/bonus/bracelet/controller/ExportFileController.java @@ -0,0 +1,78 @@ +package com.bonus.bracelet.controller; + +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; +import com.bonus.bracelet.service.IPersonMgeService; +import com.bonus.common.core.constant.BusinessConstants; +import com.bonus.common.core.utils.ExcelStyleUtil; +import com.bonus.common.entity.bracelet.BraceletParamsDto; +import com.bonus.common.entity.bracelet.exportVo.PersonExportVo; +import com.bonus.common.entity.bracelet.vo.PersonVo; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Workbook; +import org.springframework.beans.BeanUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + + +/** + * @author:cwchen + * @date:2024-03-11-14:55 + * @version:1.0 + * @description:导出文件-web层 + */ +@RestController +@RequestMapping("/export/") +@Slf4j +public class ExportFileController { + + + @Resource(name = "IPersonMgeService") + private IPersonMgeService iPersonMgeService; + + /** + * 导出人员 + * @param request + * @param response + * @param dto + * @return void + * @author cwchen + * @date 2024/7/16 21:13 + */ + @GetMapping("exportPerson") + public void exportPerson(HttpServletRequest request, HttpServletResponse response, BraceletParamsDto dto) { + try { + List personExportVoList = new ArrayList<>(); + List personLists = iPersonMgeService.getPersonLists(dto); + for (int i = 0; i < personLists.size(); i++) { + PersonExportVo exportVo = new PersonExportVo(); + PersonVo vo = personLists.get(i); + BeanUtils.copyProperties(vo, exportVo); + exportVo.setId((i + 1) + ""); + exportVo.setSex(vo.getSex() == BusinessConstants.TYPE ? "男" : vo.getSex() == BusinessConstants.TYPE2 ? "其他" : "女"); + personExportVoList.add(exportVo); + } + ExportParams exportParams = new ExportParams("人员", "人员", ExcelType.XSSF); + exportParams.setStyle(ExcelStyleUtil.class); + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, PersonExportVo.class, personExportVoList); + response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("人员" + ".xlsx", "UTF-8")); + ServletOutputStream outputStream = response.getOutputStream(); + workbook.write(outputStream); + outputStream.close(); + workbook.close(); + } catch (Exception e) { + log.error("导出人员", e); + } + } +} diff --git a/bonus-modules/bonus-bracelet/src/main/resources/download/person_model.xlsx b/bonus-modules/bonus-bracelet/src/main/resources/download/person_model.xlsx new file mode 100644 index 0000000..e3ac2f3 Binary files /dev/null and b/bonus-modules/bonus-bracelet/src/main/resources/download/person_model.xlsx differ