excel export watermarked

This excel add watermark, and may be a little different as you might think, just add a few excel above transparent picture of it. But also can be edited to delete. If the need can not be edited, then to the encrypted excel it. The code is in the underground, personally tested the online search, easy to use.

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

/ **
     * utility functions for Excel watermarked make sure their own parameter values, to ensure no cover between the watermark image. When calculating the position of the watermark, and not taking into account the cell merging, please note
     *
     * @param wb Excel Workbook
     * @param Sheet need to fight watermark Excel
     * @param waterRemarkPath watermark address, classPath, currently only supports png format picture,
     * because non-png format images hit problems after Excel may have pictures of red, not easy to make transparent effect.
     * Also note that incoming address format, should be similar to: "\\ \\ excelTemplate test.png"
     * @param startXCol watermark starting column
     * @param startYRow watermark starting line
     spacing between how much lateral * @param betweenXCol watermark column
     number row spacing between the longitudinal betweenYRow watermark @param *
     * @param XCOUNT lateral watermark total number
     * @param YCount longitudinal total number of watermarks
     * @Param waterRemarkWidth how many columns watermark image width
     * @param waterRemarkHeight watermark image height as many rows
     * @throws IOException
     * /
    public static void betweenYRow (the Workbook WB, String waterRemarkPath) throws Exception {
        int startXCol = 0; // start watermarking column
        int startYRow = 4; // watermark starting row
        int betweenXCol = 3; // watermark spacing between the transverse how many columns
        int betweenYRow = 6; // longitudinal spacing between the row number watermark
        int XCount = 0; // total lateral watermark how many
        int YCount = 0; // watermark portrait total number of
        int waterRemarkWidth = 0; // how many columns watermark image width
        int waterRemarkHeight = 0; // how many rows watermark image height

        // check incoming watermark image format
        IF (! WaterRemarkPath.endsWith ( "png") &&! WaterRemarkPath.endsWith ( "PNG")) {
            the throw new new RuntimeException ( "Excel to print the watermark to the above, now supports png format picture . ");
        }

        // Load picture
        ByteArrayOutputStream byteArrayOut new new ByteArrayOutputStream = ();
        the InputStream = new new ImageIN the FileInputStream (waterRemarkPath);
        IF (== null || ImageIN imageIn.available () <. 1) {
            the throw a RuntimeException new new ( "Excel above to print the watermark, the read failure to take the watermark image (. 1) ");.
        }
        the BufferedImage bufferImg = ImageIO.read (ImageIN);
        IF (bufferImg == null) {
            the throw a RuntimeException new new (" Excel above to print the watermark, the watermark image reading failure (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;
                }

            The catch} (Exception E) {
                XCOUNT =. 1;
            }
            the try {
                YCOUNT sheet.getLastRowNum = () / (betweenYRow); // longitudinal total number of watermark
                IF (YCOUNT <. 6) {
                    YCOUNT = 2;
                }

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

            System.err.println ( "XCOUNT:" + XCOUNT);
            System.err.println ( "YCOUNT:" + YCOUNT);
            // start playing watermark
            Drawing Drawing = sheet.createDrawingPatriarch ();
            // how many rows are in accordance totaling Print watermark loop
            for (int YCOUNT = 0; YCOUNT <YCOUNT; YCOUNT ++) {
                // how many watermarks need to print circulation each line
                for (int XCOUNT = 0; XCOUNT <XCOUNT; XCOUNT ++) {
                    // create a watermark image position
                    int + = startXCol xIndexInteger (waterRemarkWidth XCOUNT *) + (* betweenXCol XCOUNT);
                    int = startYRow yIndexInteger + (YCOUNT waterRemarkHeight *) + (* YCOUNT betweenYRow);
                    
                      / * Parameters are defined: The first parameter is a (start node of the x-axis); a second parameter (y-axis is the start node); a third parameter (x-axis is the end node); and a fourth parameter is (y-axis is the end node);
                      fifth parameter is (are inserted starting from the first picture of Excel columns, counting from 0); the sixth is (are inserted starting from the first picture of a few lines excel, counting from 0);
                      seventh parameter (image width, the total number of columns); 8th parameters (height of the image, the total number of lines); *
                     /
                    System.err.println ( "xIndexInteger:" + + xIndexInteger " , yIndexInteger: "+ yIndexInteger);
                    System.err.println (" + waterRemarkWidth xIndexInteger: "+ xIndexInteger waterRemarkWidth +
                            +", + waterRemarkHeight yIndexInteger: "+ + 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();

                }
            }
        }
    }

Guess you like

Origin blog.csdn.net/Peter_S/article/details/90166960