av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)

瀏覽:114日期:2022-05-23 13:43:42

在Excel中沒(méi)有直接添加水印的功能,但依舊可以通過(guò)一定方式來(lái)實(shí)現(xiàn)類(lèi)似水印效果。本文通過(guò)Java程序代碼介紹具體實(shí)現(xiàn)方法。可添加單一水印效果,即水印是以單個(gè)文本字樣來(lái)呈現(xiàn);也可添加多個(gè)平鋪水印效果,即水印是以多個(gè)文本字樣來(lái)頁(yè)面中平鋪。詳細(xì)內(nèi)容見(jiàn)下文。

程序環(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)部專(zhuān)用', 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();//獲取文本sizeFontMetrics 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; }}

單一水印效果:

Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)

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)部專(zhuān)用 內(nèi)部專(zhuān)用 內(nèi)部專(zhuān)用 內(nèi)部專(zhuān)用', 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();//獲取文本sizeFontMetrics 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; }}

平鋪水印效果:

Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)

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

到此這篇關(guān)于Java 在Excel中添加水印(單一水印、平鋪水印)的文章就介紹到這了,更多相關(guān)Java 在Excel中添加水印(單一水印、平鋪水印)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: excel
相關(guān)文章:
主站蜘蛛池模板: 青青久久 | 久久久精品国产 | 视频一区二区中文字幕 | 99久久视频 | 亚洲精选一区二区 | www久久av | 久久精品无码一区二区三区 | 成人日韩 | 久久一 | 午夜三级在线观看 | 欧美一级网站 | 日本久草视频 | 99久久精品国产毛片 | 亚洲精品99| 亚洲看片| 国产精品欧美一区二区三区 | 在线观看日本高清二区 | 久久久久久国模大尺度人体 | 美人の美乳で授乳プレイ | 91就要激情 | av毛片免费| 日韩一区二区三区在线观看视频 | 久久网国产 | 日韩精品一区二区三区在线播放 | 成人av在线播放 | 国产综合区 | 一区二区三区久久 | 四虎影院美女 | 久久久久se| 久久国产亚洲精品 | www.99热这里只有精品 | 国产精品视频免费观看 | 嫩草视频在线看 | 午夜精品久久久久久久久久久久久 | 欧美日韩久久 | 欧美福利 | 草久网| av中文字幕在线 | 日韩一区二区在线视频 | 国产男人的天堂 | 在线欧美亚洲 |