Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)
在Excel中沒有直接添加水印的功能,但依舊可以通過一定方式來實(shí)現(xiàn)類似水印效果。本文通過Java程序代碼介紹具體實(shí)現(xiàn)方法??商砑訂我凰⌒Ч?,即水印是以單個(gè)文本字樣來呈現(xiàn);也可添加多個(gè)平鋪水印效果,即水印是以多個(gè)文本字樣來頁(yè)面中平鋪。詳細(xì)內(nèi)容見下文。
程序環(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ù)覽”模式下查看。
到此這篇關(guān)于Java 在Excel中添加水印(單一水印、平鋪水?。┑奈恼戮徒榻B到這了,更多相關(guān)Java 在Excel中添加水印(單一水印、平鋪水印)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java.imageIo給圖片添加水印的實(shí)現(xiàn)代碼
- 教你怎么用Java實(shí)現(xiàn)給圖片打上水印
- Java添加Word文本水印和圖片水印
- Java如何給Word文檔添加多行文字水印
- Java在Word中添加多行圖片水印
- java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印
- java實(shí)現(xiàn)圖片加水印效果
- java實(shí)現(xiàn)word文檔轉(zhuǎn)pdf并添加水印的方法詳解
- java pdf加水印的方法
- 如何通過javacv實(shí)現(xiàn)圖片去水?。ǜ酱a)
- 如何通過Java添加水印到Word文檔
- Java實(shí)現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法示例
- Java用自帶的Image IO給圖片添加水印
相關(guān)文章
Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案
這篇文章主要介紹了Spring @Cacheable redis異常不影響正常業(yè)務(wù)方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Java中JDBC連接池的基本原理及實(shí)現(xiàn)方式
本文詳細(xì)講解了Java中JDBC連接池的基本原理及實(shí)現(xiàn)方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12@ConfigurationProperties在IDEA中出現(xiàn)紅色波浪線問題解決方法
本文介紹了在Springboot項(xiàng)目中,當(dāng)@ConfigurationProperties注解出現(xiàn)紅色波浪線時(shí)的解決方法,文中有詳細(xì)的解決方案供大家參考,需要的朋友可以參考下2024-09-09Java Swing中的表格(JTable)和樹(JTree)組件使用實(shí)例
這篇文章主要介紹了Java Swing中的表格(JTable)和樹(JTree)組件使用實(shí)例,本文同時(shí)講解了表格和樹的基本概念、常用方法、代碼實(shí)例,需要的朋友可以參考下2014-10-10java實(shí)現(xiàn)堆的操作方法(建堆,插入,刪除)
下面小編就為大家分享一篇java實(shí)現(xiàn)堆的操作方法(建堆,插入,刪除),具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12springboot?@PostConstruct無效的解決
這篇文章主要介紹了springboot?@PostConstruct無效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11