|
| 1 | +package net.lab1024.sa.base.common.util; |
| 2 | + |
| 3 | +import com.alibaba.excel.EasyExcel; |
| 4 | +import com.alibaba.excel.write.handler.SheetWriteHandler; |
| 5 | +import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
| 6 | +import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.poi.openxml4j.opc.PackagePartName; |
| 10 | +import org.apache.poi.openxml4j.opc.PackageRelationship; |
| 11 | +import org.apache.poi.openxml4j.opc.TargetMode; |
| 12 | +import org.apache.poi.xssf.usermodel.XSSFPictureData; |
| 13 | +import org.apache.poi.xssf.usermodel.XSSFRelation; |
| 14 | +import org.apache.poi.xssf.usermodel.XSSFSheet; |
| 15 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| 16 | + |
| 17 | +import javax.imageio.ImageIO; |
| 18 | +import javax.servlet.http.HttpServletResponse; |
| 19 | +import javax.swing.*; |
| 20 | +import java.awt.*; |
| 21 | +import java.awt.geom.AffineTransform; |
| 22 | +import java.awt.image.BufferedImage; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.Collection; |
| 26 | + |
| 27 | +/** |
| 28 | + * |
| 29 | + * excel 工具类 |
| 30 | + * |
| 31 | + * @Author 1024创新实验室-主任:卓大 |
| 32 | + * @Date 2024/4/22 22:49:07 |
| 33 | + * @Wechat zhuoda1024 |
| 34 | + |
| 35 | + * @Copyright 1024创新实验室 ( https://1024lab.net ),2012-2024 |
| 36 | + */ |
| 37 | +public final class SmartExcelUtil { |
| 38 | + |
| 39 | + /** |
| 40 | + * 通用单sheet导出 |
| 41 | + */ |
| 42 | + public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, Class head,Collection<?> data) throws IOException { |
| 43 | + // 设置下载消息头 |
| 44 | + SmartResponseUtil.setDownloadFileHeader(response, fileName, null); |
| 45 | + // 下载 |
| 46 | + EasyExcel.write(response.getOutputStream(), head) |
| 47 | + .autoCloseStream(Boolean.FALSE) |
| 48 | + .sheet(sheetName) |
| 49 | + .doWrite(data); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * 通用单 sheet水印 导出 |
| 54 | + */ |
| 55 | + public static void exportExcelWithWatermark(HttpServletResponse response, String fileName, String sheetName, Class head,Collection<?> data, String watermarkString) throws IOException { |
| 56 | + // 设置下载消息头 |
| 57 | + SmartResponseUtil.setDownloadFileHeader(response, fileName, null); |
| 58 | + // 水印 |
| 59 | + Watermark watermark = new Watermark(watermarkString); |
| 60 | + // 一定要inMemory |
| 61 | + EasyExcel.write(response.getOutputStream(), head) |
| 62 | + .inMemory(true) |
| 63 | + .sheet(sheetName) |
| 64 | + .registerWriteHandler(new CustomWaterMarkHandler(watermark)) |
| 65 | + .doWrite(data); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + @Slf4j |
| 70 | + private static class CustomWaterMarkHandler implements SheetWriteHandler { |
| 71 | + |
| 72 | + private final Watermark watermark; |
| 73 | + |
| 74 | + public CustomWaterMarkHandler(Watermark watermark) { |
| 75 | + this.watermark = watermark; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { |
| 80 | + BufferedImage bufferedImage = createWatermarkImage(); |
| 81 | + XSSFWorkbook workbook = (XSSFWorkbook) writeSheetHolder.getParentWriteWorkbookHolder().getWorkbook(); |
| 82 | + try { |
| 83 | + // 添加水印的具体操作 |
| 84 | + addWatermarkToSheet(workbook, bufferedImage); |
| 85 | + } catch (Exception e) { |
| 86 | + log.error("添加水印出错:", e); |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 创建水印图片 |
| 93 | + * |
| 94 | + * @return |
| 95 | + */ |
| 96 | + private BufferedImage createWatermarkImage() { |
| 97 | + // 获取水印相关参数 |
| 98 | + Font font = watermark.getFont(); |
| 99 | + int width = watermark.getWidth(); |
| 100 | + int height = watermark.getHeight(); |
| 101 | + Color color = watermark.getColor(); |
| 102 | + String text = watermark.getContent(); |
| 103 | + |
| 104 | + // 创建带有透明背景的 BufferedImage |
| 105 | + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| 106 | + Graphics2D g = image.createGraphics(); |
| 107 | + |
| 108 | + // 设置画笔字体、平滑、颜色 |
| 109 | + g.setFont(font); |
| 110 | + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 111 | + g.setColor(color); |
| 112 | + |
| 113 | + // 计算水印位置和角度 |
| 114 | + int y = watermark.getYAxis(); |
| 115 | + int x = watermark.getXAxis(); |
| 116 | + AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(-watermark.getAngle()), 0, y); |
| 117 | + g.setTransform(transform); |
| 118 | + // 绘制水印文字 |
| 119 | + g.drawString(text, x, y); |
| 120 | + |
| 121 | + // 释放资源 |
| 122 | + g.dispose(); |
| 123 | + |
| 124 | + return image; |
| 125 | + } |
| 126 | + |
| 127 | + private void addWatermarkToSheet(XSSFWorkbook workbook, BufferedImage watermarkImage) { |
| 128 | + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { |
| 129 | + ImageIO.write(watermarkImage, "png", os); |
| 130 | + int pictureIdx = workbook.addPicture(os.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG); |
| 131 | + XSSFPictureData pictureData = workbook.getAllPictures().get(pictureIdx); |
| 132 | + for (int i = 0; i < workbook.getNumberOfSheets(); i++) { |
| 133 | + // 获取每个Sheet表 |
| 134 | + XSSFSheet sheet = workbook.getSheetAt(i); |
| 135 | + PackagePartName ppn = pictureData.getPackagePart().getPartName(); |
| 136 | + String relType = XSSFRelation.IMAGES.getRelation(); |
| 137 | + PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null); |
| 138 | + sheet.getCTWorksheet().addNewPicture().setId(pr.getId()); |
| 139 | + } |
| 140 | + } catch (Exception e) { |
| 141 | + // 处理ImageIO.write可能抛出的异常 |
| 142 | + log.error("添加水印图片时发生错误", e); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Data |
| 148 | + private static class Watermark { |
| 149 | + |
| 150 | + public Watermark(String content) { |
| 151 | + this.content = content; |
| 152 | + init(); |
| 153 | + } |
| 154 | + |
| 155 | + public Watermark(String content, Color color, Font font, double angle) { |
| 156 | + this.content = content; |
| 157 | + this.color = color; |
| 158 | + this.font = font; |
| 159 | + this.angle = angle; |
| 160 | + init(); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * 根据水印内容长度自适应水印图片大小,简单的三角函数 |
| 165 | + */ |
| 166 | + private void init() { |
| 167 | + FontMetrics fontMetrics = new JLabel().getFontMetrics(this.font); |
| 168 | + int stringWidth = fontMetrics.stringWidth(this.content); |
| 169 | + int charWidth = fontMetrics.charWidth('A'); |
| 170 | + this.width = (int) Math.abs(stringWidth * Math.cos(Math.toRadians(this.angle))) + 5 * charWidth; |
| 171 | + this.height = (int) Math.abs(stringWidth * Math.sin(Math.toRadians(this.angle))) + 5 * charWidth; |
| 172 | + this.yAxis = this.height; |
| 173 | + this.xAxis = charWidth; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * 水印内容 |
| 178 | + */ |
| 179 | + private String content; |
| 180 | + |
| 181 | + /** |
| 182 | + * 画笔颜色 |
| 183 | + */ |
| 184 | + private Color color = new Color(239,239,239); |
| 185 | + |
| 186 | + /** |
| 187 | + * 字体样式 |
| 188 | + */ |
| 189 | + private Font font = new Font("Microsoft YaHei", Font.BOLD, 26); |
| 190 | + |
| 191 | + /** |
| 192 | + * 水印宽度 |
| 193 | + */ |
| 194 | + private int width; |
| 195 | + |
| 196 | + /** |
| 197 | + * 水印高度 |
| 198 | + */ |
| 199 | + private int height; |
| 200 | + |
| 201 | + /** |
| 202 | + * 倾斜角度,非弧度制 |
| 203 | + */ |
| 204 | + private double angle = 25; |
| 205 | + |
| 206 | + /** |
| 207 | + * 字体的y轴位置 |
| 208 | + */ |
| 209 | + private int yAxis = 50; |
| 210 | + |
| 211 | + /** |
| 212 | + * 字体的X轴位置 |
| 213 | + */ |
| 214 | + private int xAxis; |
| 215 | + |
| 216 | + /** |
| 217 | + * 水平倾斜度 |
| 218 | + */ |
| 219 | + private double shearX = 0.1; |
| 220 | + |
| 221 | + /** |
| 222 | + * 垂直倾斜度 |
| 223 | + */ |
| 224 | + private double shearY = -0.26; |
| 225 | + } |
| 226 | +} |
0 commit comments