亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Java在Excel中添加水印的實現(單一水印、平鋪水印)

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

在Excel中沒有直接添加水印的功能,但依舊可以通過一定方式來實現類似水印效果。本文通過Java程序代碼介紹具體實現方法。可添加單一水印效果,即水印是以單個文本字樣來呈現;也可添加多個平鋪水印效果,即水印是以多個文本字樣來頁面中平鋪。詳細內容見下文。

程序環境:

測試文檔:Office Excel 2013

編譯環境:IntelliJ IDEA 2018

JDK版本:1.8.0

Excel庫: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測試文檔Workbook wb = new Workbook();wb.loadFromFile('test.xlsx');//設置文本和字體大小Font font = new Font('仿宋', Font.PLAIN, 40);for (int i =0;i<wb.getWorksheets().getCount();i++){ Worksheet sheet = wb.getWorksheets().get(i); //調用DrawText() 方法插入圖片 BufferedImage imgWtrmrk = drawText('內部專用', font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth()); //將圖片設置為頁眉 sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk); sheet.getPageSetup().setCenterHeader('&G'); //將顯示模式設置為Layout 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中添加水印的實現(單一水印、平鋪水印)

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測試文檔Workbook wb = new Workbook();wb.loadFromFile('test.xlsx');//設置文本和字體大小Font font = new Font('仿宋', Font.PLAIN, 25);for (int i =0;i<wb.getWorksheets().getCount();i++){ Worksheet sheet = wb.getWorksheets().get(i); //調用DrawText() 方法插入圖片 BufferedImage imgWtrmrk = drawText('內部專用 內部專用 內部專用 內部專用', font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth()); //將圖片設置為頁眉 sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk); sheet.getPageSetup().setCenterHeader('&G'); //將顯示模式設置為Layout 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中添加水印的實現(單一水印、平鋪水印)

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

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

標簽: excel
相關文章:
主站蜘蛛池模板: 国产黄频在线观看高清免费 | 毛片在线播放网址 | 性欧美巨大 | 青青青国产色视频在线观看 | 国产h在线观看 | 国产美女无遮挡免费网站 | 毛片精品 | 在线观看91香蕉国产免费 | 蕾丝视频在线观看www | 一级黄色小视频 | 女人被狂躁的视频免费一一 | 成人a级特黄毛片 | 亚洲综合日韩精品欧美综合区 | 黄色男女视频 | 亚洲国产成人久久精品影视 | 激情在线免费视频 | 99久久久精品免费观看国产 | 日韩a无v码在线播放免费 | 成 人 黄 色 大 片 | 欧美日韩在线视频专区免费 | 国产美女福利在线 | 亚洲国产成人精品小蝌蚪 | 国产护士恋夜各种姿势视频 | 国产精品国产三级国产an不卡 | 成 人 黄 色 大 片全部 | 亚洲第一网站 | 亚洲第一成年免费网站 | 美女大片高清特黄a大片 | 日本一区二区三区精品视频 | 日本妇丰满乱xxxxⅹ视频 | 中文日韩字幕 | 免费视频日本xvideos | xxx大片免费视频 | 久99久精品视频免费观看v | 欧美激情综合亚洲一二区 | 国产成人区 | 狠狠亚洲| 大陆一级毛片免费视频观看i | 国产精品麻豆va在线播放 | 国产呦精品一区二区三区网站 | 高清成人爽a毛片免费网站 高清成人综合 |