栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

POI Excel插入水印(只支持07版Excel文档)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

POI Excel插入水印(只支持07版Excel文档)

目录

1 Maven依赖

2 实现代码

2.1 createWaterMark

2.2  insertWaterRemark

3 调试代码

4 调试结果

 注:


1 Maven依赖
        
        
            com.alibaba
            easyexcel
            2.2.8
        
        
        
            cn.afterturn
            easypoi-base
            4.4.0
        
        
            org.apache.poi
            ooxml-schemas
            1.4
        

2 实现代码

2.1 createWaterMark

生成水印数据。

    
    public static byte[] createWaterMark(String content, int width, int height, java.awt.Color color, java.awt.Font font) throws IOException {
        // 获取bufferedImage对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取Graphics2d对象
        Graphics2D g2d = image.createGraphics();
        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        g2d.dispose();
        g2d = image.createGraphics();
        //设置字体颜色
        g2d.setColor(color);
        g2d.setStroke(new BasicStroke(1));
        // 设置字体
        g2d.setFont(font);
        //设置倾斜度
        g2d.rotate(-0.5, (double) image.getWidth() / 2, (double) image.getHeight() / 2);
        FontRenderContext context = g2d.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(content, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();
        double baseY = y + ascent;
        // 写入水印文字原定高度过小,所以累计写水印,增加高度
        g2d.drawString(content, (int) x, (int) baseY);
        // 设置透明度
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        // 释放对象
        g2d.dispose();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        return os.toByteArray();
    }

2.2  insertWaterRemark

插入水印,只支持07版Excel文档。

    
    public static void insertWaterRemark(XSSFSheet sheet, byte[] bytes) {
        //水印图片数据关联sheet对象
        XSSFWorkbook workbook = sheet.getWorkbook();
        int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        String relationId = sheet.addRelation(null, XSSFRelation.IMAGES, workbook.getAllPictures().get(pictureIdx))
                .getRelationship().getId();
        //将水印图片设置为sheet的背景颜色
        sheet.getCTWorksheet().addNewPicture().setId(relationId);
    }

3 调试代码
    
    @Test
    public void testInsertWaterMark() {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet();
            File file = new File("D:/poi/testInsertWaterMark.xlsx");
            FileUtil.createNewFile(file);
            //生成水印
            byte[] waterMarkBytes = createWaterMark("旭东怪YYDS", 200, 150, new Color(255, 0, 0, 100)
                    , new Font("微软雅黑", Font.BOLD, 20));
            //插入水印
            insertWaterRemark(sheet, waterMarkBytes);
            workbook.write(new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

4 调试结果

 注:

觉得这篇博客写的不错的可以前往Gitee点个Star,源码请查看Gitee的xudongbase项目poi分支。

xudongbase: 主要是项目中可以用到的共通方法,现有easyexcel分支在持续更新中。欢迎大家Star和提交Issues。easyexcel分支:批量设置样式,批量添加批注,批量合并单元格,设置冻结行和列,设置行高列宽,隐藏行和列,绑定下拉框数据 - Gitee.com

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831354.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号