這篇文章主要介紹了Java如何在Excel中添加水印,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Java的基本數據類型分為:1、整數類型,用來表示整數的數據類型。2、浮點類型,用來表示小數的數據類型。3、字符類型,字符類型的關鍵字是“char”。4、布爾類型,是表示邏輯值的基本數據類型。
測試文檔:Office Excel 2013
編譯環境:IntelliJ IDEA 2018
JDK版本:1.8.0
Excel庫:Java系列free spire.xls.jar 3.9.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();
//獲取文本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;
}
}單一水印效果:

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();
//獲取文本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;
}
}平鋪水印效果:

★ 需要注意的是:在添加完水印效果后,查看文檔時,在“普通視圖”水印不可見,需在“頁面布局”模式或“打印預覽”模式下查看。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java如何在Excel中添加水印”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。