概述
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Date;
public class FontImage {
public static BufferedImage createBufferedImage(String name, Font font, int i, int i1, boolean b) {
Watermark watermark = new Watermark();
watermark.setEnable(b);
watermark.setDateFormat("yyyy-MM-dd HH:mm");
watermark.setText(name);
watermark.setColor("#C5CBCF");
return createWatermarkImage(watermark);
}
public static BufferedImage createBufferedImage(String name, Font font, int width, int height, String color, String dateFormat, boolean enable) {
Watermark watermark = new Watermark();
watermark.setEnable(enable);
watermark.setDateFormat(dateFormat);
watermark.setText(name);
watermark.setWidth(width);
watermark.setHeight(height);
watermark.setColor(color);
watermark.setFont(font);
return createWatermarkImage(watermark);
}
@Data
public static class Watermark {
private Boolean enable;
private String text;
private String dateFormat;
private int width;
private int height;
private Font font;
private String color;
}
public static BufferedImage createWatermarkImage(Watermark watermark) {
if (watermark == null) {
watermark = new FontImage.Watermark();
watermark.setEnable(true);
watermark.setText("内部资料");
watermark.setColor("#87CEFA");
watermark.setDateFormat("yyyy-MM-dd HH:mm");
} else {
if (StringUtils.isEmpty(watermark.getDateFormat())) {
watermark.setDateFormat("yyyy-MM-dd HH:mm");
} else if (watermark.getDateFormat().length() == 16) {
watermark.setDateFormat("yyyy-MM-dd HH:mm");
} else if (watermark.getDateFormat().length() == 10) {
watermark.setDateFormat("yyyy-MM-dd");
}
if (StringUtils.isEmpty(watermark.getText())) {
watermark.setText("内部资料");
}
if (StringUtils.isEmpty(watermark.getColor())) {
watermark.setColor("#87CEFA");
}
}
String[] textArray = watermark.getText().split("n");
Font font;
if (watermark.getFont() == null) {
font = new Font("microsoft-yahei", Font.PLAIN, 20);
} else {
font = watermark.getFont();
}
int width = watermark.getWidth();
int height = watermark.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 背景透明 开始
Graphics2D graphics2D = image.createGraphics();
image = graphics2D.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
graphics2D.dispose();
// 背景透明 结束
graphics2D = image.createGraphics();
// 设定画笔颜色
graphics2D.setColor(new Color(Integer.parseInt(watermark.getColor().substring(1), 16)));
graphics2D.setFont(font);// 设置画笔字体
graphics2D.shear(0.1, -0.26);// 设定倾斜度
//设置字体平滑
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int y = 50;
for (int i = 0; i < textArray.length; i++) {
// 画出字符串
graphics2D.drawString(textArray[i], 0, y);
y = y + font.getSize();
}
// graphics2D.drawString(DateUtil.formatDateYMD_CN(new Date()), 0, y);// 画出字符串
graphics2D.dispose();// 释放画笔
return image;
}
}
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @author subaodong
* @date 2020/9/9
* 水印工具类 传入的text可以为多行 可用 ‘n’分割
*/
public class WatermarkUtil {
public static void WriteWaterMark(XSSFWorkbook workbook, String text) throws IOException {
BufferedImage image = FontImage.createBufferedImage(text, new Font("microsoft-yahei", Font.PLAIN, 20), 300, 100, "#87CEFA", "yyyy-MM-dd HH:mm", true);
// 导出到字节流B
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
int pictureIdx = workbook.addPicture(os.toByteArray(), Workbook.PICTURE_TYPE_PNG);
POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx);
//获取每个Sheet表
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
String relType = XSSFRelation.IMAGES.getRelation();
//add relation from sheet to the picture data
PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
//set background picture to sheet
sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
}
}
public static void WriteWaterMark(XSSFWorkbook workbook, String text, Font font, int width, int height, String color, String dateFormat, boolean enable) throws IOException {
if (!enable) {
return;
}
BufferedImage image = FontImage.createBufferedImage(text, font, width, height, color, dateFormat, enable);
// 导出到字节流B
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, "png", os);
int pictureIdx = workbook.addPicture(os.toByteArray(), Workbook.PICTURE_TYPE_PNG);
POIXMLDocumentPart poixmlDocumentPart = workbook.getAllPictures().get(pictureIdx);
//获取每个Sheet表
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
XSSFSheet sheet = workbook.getSheetAt(i);
PackagePartName ppn = poixmlDocumentPart.getPackagePart().getPartName();
String relType = XSSFRelation.IMAGES.getRelation();
//add relation from sheet to the picture data
PackageRelationship pr = sheet.getPackagePart().addRelationship(ppn, TargetMode.INTERNAL, relType, null);
//set background picture to sheet
sheet.getCTWorksheet().addNewPicture().setId(pr.getId());
}
}
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
}
最后
以上就是积极牛排为你收集整理的EXCEL 加入水印 亲测可用的全部内容,希望文章能够帮你解决EXCEL 加入水印 亲测可用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复