溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Java實(shí)現(xiàn)在Excel文件中添加水印效果

發(fā)布時(shí)間:2022-02-28 10:19:26 來源:億速云 閱讀:1092 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“如何使用Java實(shí)現(xiàn)在Excel文件中添加水印效果”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用Java實(shí)現(xiàn)在Excel文件中添加水印效果”吧!

程序環(huán)境:

測(cè)試文檔:Office Excel 2013

編譯環(huán)境:IntelliJ IDEA 2018

JDK版本:1.8.0

Excel庫(kù):Java系列free spire.xls.jar 3.9.1

1.單一水印效果

import com.spire.xls.*;

import java.awt.*;
import java.awt.image.BufferedImage;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

public class SingleWatermark {
    public static void main(String[] args) {
        //加載Excel測(cè)試文檔
        Workbook wb = new Workbook();
        wb.loadFromFile("test.xlsx");

        //設(shè)置文本和字體大小
        Font font = new Font("仿宋", Font.PLAIN, 40);

        for (int i =0;i<wb.getWorksheets().getCount();i++)
        {
            Worksheet sheet = wb.getWorksheets().get(i);
            //調(diào)用DrawText() 方法插入圖片
            BufferedImage imgWtrmrk = drawText("內(nèi)部專用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());

            //將圖片設(shè)置為頁(yè)眉
            sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);
            sheet.getPageSetup().setCenterHeader("&G");


            //將顯示模式設(shè)置為L(zhǎng)ayout
            sheet.setViewMode(ViewMode.Layout);
        }

        //保存文檔
        wb.saveToFile("SingleWatermark.xlsx", ExcelVersion.Version2013);
    }
    private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)
    {
        //定義圖片寬度和高度
        BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);

        Graphics2D loGraphic = img.createGraphics();

        //獲取文本size
        FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);
        int liStrWidth = loFontMetrics.stringWidth(text);
        int liStrHeight = loFontMetrics.getHeight();

        //文本顯示樣式及位置
        loGraphic.setColor(backColor);
        loGraphic.fillRect(0, 0, (int) width, (int) height);
        loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
        loGraphic.rotate(Math.toRadians(-45));

        loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);
        loGraphic.setFont(font);
        loGraphic.setColor(textColor);
        loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
        loGraphic.dispose();
        return img;
    }
}

2.平鋪水印效果

import com.spire.xls.*;

import java.awt.*;
import java.awt.image.BufferedImage;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;

public class TiledWatermark {
    public static void main(String[] args) {
        //加載Excel測(cè)試文檔
        Workbook wb = new Workbook();
        wb.loadFromFile("test.xlsx");

        //設(shè)置文本和字體大小
        Font font = new Font("仿宋", Font.PLAIN, 25);

        for (int i =0;i<wb.getWorksheets().getCount();i++)
        {
            Worksheet sheet = wb.getWorksheets().get(i);
            //調(diào)用DrawText() 方法插入圖片
            BufferedImage imgWtrmrk = drawText("內(nèi)部專用     內(nèi)部專用     內(nèi)部專用     內(nèi)部專用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth());

            //將圖片設(shè)置為頁(yè)眉
            sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk);
            sheet.getPageSetup().setCenterHeader("&G");

            //將顯示模式設(shè)置為L(zhǎng)ayout
            sheet.setViewMode(ViewMode.Layout);
        }

        //保存文檔
        wb.saveToFile("TiledWatermark.xlsx", ExcelVersion.Version2013);
    }
    private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width)
    {
        //定義圖片寬度和高度
        BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB);

        Graphics2D loGraphic = img.createGraphics();

        //獲取文本size
        FontMetrics loFontMetrics = loGraphic.getFontMetrics(font);
        int liStrWidth = loFontMetrics.stringWidth(text);
        int liStrHeight = loFontMetrics.getHeight();

        //文本顯示樣式及位置
        loGraphic.setColor(backColor);
        loGraphic.fillRect(0, 0, (int) width, (int) height);
        loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2);
        //loGraphic.rotate(Math.toRadians(-45));

        loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2);
        loGraphic.setFont(font);
        loGraphic.setColor(textColor);
        loGraphic.drawString(text, ((int) width - liStrWidth) /6 , ((int) height - liStrHeight) /6);
        loGraphic.drawString(text,((int) width - liStrWidth) /3, ((int) height - liStrHeight) /3);
        loGraphic.drawString(text,((int) width - liStrWidth) /2, ((int) height - liStrHeight) /2);
        loGraphic.dispose();
        return img;
    }
}

★ 需要注意的是:在添加完水印效果后,查看文檔時(shí),在“普通視圖”水印不可見,需在“頁(yè)面布局”模式或“打印預(yù)覽”模式下查看。

感謝各位的閱讀,以上就是“如何使用Java實(shí)現(xiàn)在Excel文件中添加水印效果”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)如何使用Java實(shí)現(xiàn)在Excel文件中添加水印效果這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI