excel 导出加水印

这个excel加水印,和你想象的可能有点区别,只是在excel上面添加了几张透明的图片吧。而且还是可编辑可删除的。要是需要不可编辑的,那么给这个excel加密吧。地下代码也是在网上搜索的,亲自测试,好用。

-------------------------------------

/**
     * 为Excel打上水印工具函数 请自行确保参数值,以保证水印图片之间不会覆盖。 在计算水印的位置的时候,并没有考虑到单元格合并的情况,请注意
     *
     * @param wb                Excel Workbook
     * @param sheet             需要打水印的Excel
     * @param waterRemarkPath   水印地址,classPath,目前只支持png格式的图片,
     *                          因为非png格式的图片打到Excel上后可能会有图片变红的问题,且不容易做出透明效果。
     *                          同时请注意传入的地址格式,应该为类似:"\\excelTemplate\\test.png"
     * @param startXCol         水印起始列
     * @param startYRow         水印起始行
     * @param betweenXCol       水印横向之间间隔多少列
     * @param betweenYRow       水印纵向之间间隔多少行
     * @param XCount            横向共有水印多少个
     * @param YCount            纵向共有水印多少个
     * @param waterRemarkWidth  水印图片宽度为多少列
     * @param waterRemarkHeight 水印图片高度为多少行
     * @throws IOException
     */
    public static void betweenYRow(Workbook wb, String waterRemarkPath) throws Exception {
        int startXCol = 0; // 水印起始列
        int startYRow = 4; // 水印起始行
        int betweenXCol = 3; // 水印横向之间间隔多少列
        int betweenYRow = 6; // 水印纵向之间间隔多少行
        int XCount = 0; // 横向共有水印多少个
        int YCount = 0; // 纵向共有水印多少个
        int waterRemarkWidth = 0; // 水印图片宽度为多少列
        int waterRemarkHeight = 0; // 水印图片高度为多少行

        // 校验传入的水印图片格式
        if (!waterRemarkPath.endsWith("png") && !waterRemarkPath.endsWith("PNG")) {
            throw new RuntimeException("向Excel上面打印水印,目前支持png格式的图片。");
        }

        // 加载图片
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        InputStream imageIn = new FileInputStream(waterRemarkPath);
        if (null == imageIn || imageIn.available() < 1) {
            throw new RuntimeException("向Excel上面打印水印,读取水印图片失败(1)。");
        }
        BufferedImage bufferImg = ImageIO.read(imageIn);
        if (null == bufferImg) {
            throw new RuntimeException("向Excel上面打印水印,读取水印图片失败(2)。");
        }
        ImageIO.write(bufferImg, "png", byteArrayOut);

        for (int i = 0; i < wb.getNumberOfSheets(); i++) { // wb.getNumberOfSheets()
            System.err.println("num:" + wb.getNumberOfSheets());
            System.err.println("i:" + i);
            Sheet sheet = wb.getSheetAt(i);
            sheet.protectSheet("123456");//加密加锁excel,密码  123456
            try {
                XCount = (sheet.getRow(0).getLastCellNum()); // 横向共有水印多少个
                if (XCount%2 == 1) {
                    startXCol = XCount/2;
                }else {
                    startXCol = (XCount/2)-1;
                }
                if (XCount >= 0 && XCount <= 5 ) {
                    XCount = 1;
                }
                
                if (XCount >= 5) {
                    XCount = 1;
                }

            } catch (Exception e) {
                XCount = 1;
            }
            try {
                YCount = sheet.getLastRowNum() / (betweenYRow); // 纵向共有水印多少个
                if (YCount < 6) {
                    YCount = 2;
                }

            } catch (Exception e) {
                YCount = 50;
            }

            System.err.println("XCount:" + XCount);
            System.err.println("YCount:" + YCount);
            // 开始打水印
            Drawing drawing = sheet.createDrawingPatriarch();
            // 按照共需打印多少行水印进行循环
            for (int yCount = 0; yCount < YCount; yCount++) {
                // 按照每行需要打印多少个水印进行循环
                for (int xCount = 0; xCount < XCount; xCount++) {
                    // 创建水印图片位置
                    int xIndexInteger = startXCol + (xCount * waterRemarkWidth) + (xCount * betweenXCol);
                    int yIndexInteger = startYRow + (yCount * waterRemarkHeight) + (yCount * betweenYRow);
                    
                      /*参数定义: 第一个参数是(x轴的开始节点); 第二个参数是(是y轴的开始节点); 第三个参数是(是x轴的结束节点); 第四个参数是(是y轴的结束节点);
                      第五个参数是(是从Excel的第几列开始插入图片,从0开始计数); 第六个参数是(是从excel的第几行开始插入图片,从0开始计数);
                      第七个参数是(图片宽度,共多少列); 第8个参数是(图片高度,共多少行);
                     */
                    System.err.println("xIndexInteger:" + xIndexInteger + ", yIndexInteger:" + yIndexInteger);
                    System.err.println("xIndexInteger+waterRemarkWidth:" + xIndexInteger + waterRemarkWidth
                            + ", yIndexInteger+waterRemarkHeight:" + yIndexInteger + waterRemarkHeight);
                    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, xIndexInteger, yIndexInteger,
                            xIndexInteger + waterRemarkWidth, yIndexInteger + waterRemarkHeight);

                    Picture pic = drawing.createPicture(anchor,
                            wb.addPicture(byteArrayOut.toByteArray(), Workbook.PICTURE_TYPE_PNG));
                    pic.resize();

                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/Peter_S/article/details/90166960
今日推荐