mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-17 10:56:39 +08:00
commit
51aa2e08ba
@ -282,6 +282,13 @@
|
||||
<version>${poi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 为Excel导出添加水印-->
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>ooxml-schemas</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
|
@ -1,7 +1,6 @@
|
||||
package net.lab1024.sa.admin.module.business.goods.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import net.lab1024.sa.admin.constant.AdminSwaggerTagConst;
|
||||
@ -11,17 +10,16 @@ import net.lab1024.sa.admin.module.business.goods.domain.form.GoodsUpdateForm;
|
||||
import net.lab1024.sa.admin.module.business.goods.domain.vo.GoodsExcelVO;
|
||||
import net.lab1024.sa.admin.module.business.goods.domain.vo.GoodsVO;
|
||||
import net.lab1024.sa.admin.module.business.goods.service.GoodsService;
|
||||
import net.lab1024.sa.admin.util.excel.ExcelUtils;
|
||||
import net.lab1024.sa.base.common.domain.PageResult;
|
||||
import net.lab1024.sa.base.common.domain.ResponseDTO;
|
||||
import net.lab1024.sa.base.common.domain.ValidateList;
|
||||
import net.lab1024.sa.base.common.util.SmartResponseUtil;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -87,18 +85,10 @@ public class GoodsController {
|
||||
@Operation(summary = "导出 @author 卓大")
|
||||
@GetMapping("/goods/exportGoods")
|
||||
@SaCheckPermission("goods:exportGoods")
|
||||
public void exportGoods(HttpServletResponse response) throws IOException {
|
||||
|
||||
public void exportGoods(HttpServletResponse response) {
|
||||
List<GoodsExcelVO> goodsList = goodsService.getAllGoods();
|
||||
|
||||
// 设置下载消息头
|
||||
SmartResponseUtil.setDownloadFileHeader(response, "商品列表.xls", null);
|
||||
|
||||
// 下载
|
||||
EasyExcel.write(response.getOutputStream(), GoodsExcelVO.class)
|
||||
.autoCloseStream(Boolean.FALSE)
|
||||
.sheet("商品")
|
||||
.doWrite(goodsList);
|
||||
ExcelUtils<GoodsExcelVO> utils = new ExcelUtils<>(GoodsExcelVO.class);
|
||||
utils.exportData(response,goodsList,"商品列表");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package net.lab1024.sa.admin.module.business.oa.enterprise;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -11,6 +10,7 @@ import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseEm
|
||||
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseExcelVO;
|
||||
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseListVO;
|
||||
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseVO;
|
||||
import net.lab1024.sa.admin.util.excel.ExcelUtils;
|
||||
import net.lab1024.sa.base.common.domain.PageResult;
|
||||
import net.lab1024.sa.base.common.domain.RequestUser;
|
||||
import net.lab1024.sa.base.common.domain.ResponseDTO;
|
||||
@ -23,7 +23,6 @@ import org.springframework.web.bind.annotation.*;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.Valid;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -53,21 +52,14 @@ public class EnterpriseController {
|
||||
|
||||
@Operation(summary = "导出企业信息 @author 卓大")
|
||||
@PostMapping("/oa/enterprise/exportExcel")
|
||||
public void exportExcel(@RequestBody @Valid EnterpriseQueryForm queryForm, HttpServletResponse response) throws IOException {
|
||||
public void exportExcel(@RequestBody @Valid EnterpriseQueryForm queryForm, HttpServletResponse response) {
|
||||
List<EnterpriseExcelVO> data = enterpriseService.getExcelExportData(queryForm);
|
||||
if (CollectionUtils.isEmpty(data)) {
|
||||
SmartResponseUtil.write(response, ResponseDTO.userErrorParam("暂无数据"));
|
||||
return;
|
||||
}
|
||||
|
||||
// 设置下载消息头
|
||||
SmartResponseUtil.setDownloadFileHeader(response, "企业基本信息.xls", null);
|
||||
|
||||
// 下载
|
||||
EasyExcel.write(response.getOutputStream(), EnterpriseExcelVO.class)
|
||||
.autoCloseStream(Boolean.FALSE)
|
||||
.sheet("企业信息")
|
||||
.doWrite(data);
|
||||
ExcelUtils<EnterpriseExcelVO> utils = new ExcelUtils<>(EnterpriseExcelVO.class);
|
||||
utils.exportData(response,data,"企业基本信息");
|
||||
}
|
||||
|
||||
@Operation(summary = "查询企业详情 @author 开云")
|
||||
|
@ -0,0 +1,105 @@
|
||||
package net.lab1024.sa.admin.util.excel;
|
||||
|
||||
|
||||
import com.alibaba.excel.write.handler.SheetWriteHandler;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
|
||||
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.openxml4j.opc.PackagePartName;
|
||||
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
||||
import org.apache.poi.openxml4j.opc.TargetMode;
|
||||
import org.apache.poi.xssf.usermodel.XSSFPictureData;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRelation;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author liuzhexian
|
||||
* @since 2024-04-16
|
||||
* @email 1037512352@qq.com
|
||||
*/
|
||||
@Slf4j
|
||||
public class CustomWaterMarkHandler implements SheetWriteHandler {
|
||||
|
||||
private final Watermark watermark;
|
||||
|
||||
public CustomWaterMarkHandler(Watermark watermark) {
|
||||
this.watermark = watermark;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
|
||||
BufferedImage bufferedImage = createWatermarkImage();
|
||||
XSSFWorkbook workbook = (XSSFWorkbook) writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook();
|
||||
try {
|
||||
// 添加水印的具体操作
|
||||
addWatermarkToSheet(workbook, bufferedImage);
|
||||
} catch (Exception e) {
|
||||
log.error("添加水印出错:", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建水印图片
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private BufferedImage createWatermarkImage() {
|
||||
// 获取水印相关参数
|
||||
Font font = watermark.getFont();
|
||||
int width = watermark.getWidth();
|
||||
int height = watermark.getHeight();
|
||||
Color color = watermark.getColor();
|
||||
String text = watermark.getContent();
|
||||
|
||||
// 创建带有透明背景的 BufferedImage
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
||||
Graphics2D g = image.createGraphics();
|
||||
|
||||
// 设置画笔字体、平滑、颜色
|
||||
g.setFont(font);
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g.setColor(color);
|
||||
|
||||
// 计算水印位置和角度
|
||||
int y = watermark.getYAxis();
|
||||
int x = watermark.getXAxis();
|
||||
AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(-watermark.getAngle()), 0, y);
|
||||
g.setTransform(transform);
|
||||
// 绘制水印文字
|
||||
g.drawString(text, x, y);
|
||||
|
||||
// 释放资源
|
||||
g.dispose();
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
private void addWatermarkToSheet(XSSFWorkbook workbook, BufferedImage watermarkImage) {
|
||||
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
|
||||
ImageIO.write(watermarkImage, "png", os);
|
||||
int pictureIdx = workbook.addPicture(os.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG);
|
||||
XSSFPictureData pictureData = workbook.getAllPictures().get(pictureIdx);
|
||||
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
||||
// 获取每个Sheet表
|
||||
XSSFSheet sheet = workbook.getSheetAt(i);
|
||||
PackagePartName ppn = pictureData.getPackagePart().getPartName();
|
||||
String relType = XSSFRelation.IMAGES.getRelation();
|
||||
PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
|
||||
sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 处理ImageIO.write可能抛出的异常
|
||||
log.error("添加水印图片时发生错误", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,96 @@
|
||||
package net.lab1024.sa.admin.util.excel;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.lab1024.sa.admin.module.system.login.domain.LoginResultVO;
|
||||
import net.lab1024.sa.admin.module.system.login.service.LoginService;
|
||||
import net.lab1024.sa.admin.util.AdminRequestUtil;
|
||||
import net.lab1024.sa.base.common.util.SmartResponseUtil;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author liuzhexian
|
||||
* @since 2024-04-16
|
||||
* @email 1037512352@qq.com
|
||||
*/
|
||||
@Slf4j
|
||||
public class ExcelUtils<T> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
||||
/**
|
||||
* 导出文件后缀
|
||||
*/
|
||||
public static final String XLSX = ".xlsx";
|
||||
|
||||
/**
|
||||
* 实体对象
|
||||
*/
|
||||
public Class<T> clazz;
|
||||
|
||||
public ExcelUtils(Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用导出方法
|
||||
*
|
||||
* @param response response
|
||||
* @param list 导出的列表
|
||||
* @param title 导出的名称
|
||||
*/
|
||||
public void exportData(HttpServletResponse response, List<T> list, String title) {
|
||||
// 设置下载消息头
|
||||
SmartResponseUtil.setDownloadFileHeader(response, title + XLSX, null);
|
||||
// 根据配置文件中的参数判断导出是否带水印
|
||||
String show = SpringUtil.getBean(Environment.class).getProperty("export.show-watermark");
|
||||
if(Boolean.parseBoolean(show)) {
|
||||
exportSheetWithWatermark(response, list, title);
|
||||
} else {
|
||||
exportSheet(response, list, title);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用导出方法(不带水印)
|
||||
*
|
||||
* @param response response
|
||||
* @param list 导出的列表
|
||||
* @param title 导出的名称
|
||||
*/
|
||||
public void exportSheet(HttpServletResponse response, List<T> list, String title) {
|
||||
try {
|
||||
EasyExcel.write(response.getOutputStream(), this.clazz).sheet(title).doWrite(list);
|
||||
} catch (Exception e) {
|
||||
log.error("{}导出异常", title, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用导出方法(带水印)
|
||||
*
|
||||
* @param response response
|
||||
* @param exportList 导出的列表
|
||||
* @param title 导出的名称
|
||||
*/
|
||||
public void exportSheetWithWatermark(HttpServletResponse response, List<T> exportList, String title) {
|
||||
try {
|
||||
LoginResultVO loginResult = SpringUtil.getBean(LoginService.class).getLoginResult(AdminRequestUtil.getRequestUser());
|
||||
// 注册水印处理器 水印 员工昵称 + 日期
|
||||
Watermark watermark = new Watermark(String.format("%s %s",loginResult.getActualName(), DateUtil.formatDate(new Date())));
|
||||
// 一定要inMemory
|
||||
EasyExcel.write(response.getOutputStream(), this.clazz)
|
||||
.inMemory(true).sheet(title).registerWriteHandler(new CustomWaterMarkHandler(watermark)).doWrite(exportList);
|
||||
} catch (Exception e) {
|
||||
log.error("{}导出异常", title, e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package net.lab1024.sa.admin.util.excel;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* @author liuzhexian
|
||||
* @since 2024-04-16
|
||||
* @email 1037512352@qq.com
|
||||
*/
|
||||
@Data
|
||||
public class Watermark {
|
||||
|
||||
public Watermark(String content) {
|
||||
this.content = content;
|
||||
init();
|
||||
}
|
||||
|
||||
public Watermark(String content, Color color, Font font, double angle) {
|
||||
this.content = content;
|
||||
this.color = color;
|
||||
this.font = font;
|
||||
this.angle = angle;
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据水印内容长度自适应水印图片大小,简单的三角函数
|
||||
*/
|
||||
private void init() {
|
||||
FontMetrics fontMetrics = new JLabel().getFontMetrics(this.font);
|
||||
int stringWidth = fontMetrics.stringWidth(this.content);
|
||||
int charWidth = fontMetrics.charWidth('A');
|
||||
this.width = (int) Math.abs(stringWidth * Math.cos(Math.toRadians(this.angle))) + 2 * charWidth;
|
||||
this.height = (int) Math.abs(stringWidth * Math.sin(Math.toRadians(this.angle))) + 2 * charWidth;
|
||||
this.yAxis = this.height;
|
||||
this.xAxis = charWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* 水印内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 画笔颜色
|
||||
*/
|
||||
private Color color = new Color(239,239,239);
|
||||
|
||||
/**
|
||||
* 字体样式
|
||||
*/
|
||||
private Font font = new Font("Microsoft YaHei", Font.BOLD, 15);
|
||||
|
||||
/**
|
||||
* 水印宽度
|
||||
*/
|
||||
private int width;
|
||||
|
||||
/**
|
||||
* 水印高度
|
||||
*/
|
||||
private int height;
|
||||
|
||||
/**
|
||||
* 倾斜角度,非弧度制
|
||||
*/
|
||||
private double angle = 25;
|
||||
|
||||
/**
|
||||
* 字体的y轴位置
|
||||
*/
|
||||
private int yAxis = 50;
|
||||
|
||||
/**
|
||||
* 字体的X轴位置
|
||||
*/
|
||||
private int xAxis;
|
||||
|
||||
/**
|
||||
* 水平倾斜度
|
||||
*/
|
||||
private double shearX = 0.1;
|
||||
|
||||
/**
|
||||
* 垂直倾斜度
|
||||
*/
|
||||
private double shearY = -0.26;
|
||||
}
|
@ -243,6 +243,11 @@
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>ooxml-schemas</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-scratchpad</artifactId>
|
||||
|
@ -3,7 +3,7 @@ spring:
|
||||
datasource:
|
||||
url: jdbc:p6spy:mysql://127.0.0.1:3306/smart_admin_v3?autoReconnect=true&useServerPreparedStmts=false&rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
|
||||
username: root
|
||||
password: Zhuoda.123
|
||||
password: 123456
|
||||
initial-size: 2
|
||||
min-idle: 2
|
||||
max-active: 10
|
||||
@ -142,3 +142,6 @@ sa-token:
|
||||
is-print: false
|
||||
# 是否从cookie读取token
|
||||
is-read-cookie: false
|
||||
|
||||
export:
|
||||
show-watermark: true
|
@ -144,3 +144,6 @@ sa-token:
|
||||
is-print: false
|
||||
# 是否从cookie读取token
|
||||
is-read-cookie: false
|
||||
|
||||
export:
|
||||
show-watermark: true
|
@ -144,3 +144,6 @@ sa-token:
|
||||
is-print: false
|
||||
# 是否从cookie读取token
|
||||
is-read-cookie: false
|
||||
|
||||
export:
|
||||
show-watermark: true
|
@ -144,3 +144,6 @@ sa-token:
|
||||
is-print: false
|
||||
# 是否从cookie读取token
|
||||
is-read-cookie: false
|
||||
|
||||
export:
|
||||
show-watermark: true
|
Loading…
Reference in New Issue
Block a user