之前的代码是通过poi包导出xls文件,在poi相关类里面操作水印,尝试了好几次都没有成功。后来换成poi-ooxml包导出xlsx添加水印成功。下面是导出xlsx添加水印的相关代码。
1、添加相关依赖
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-compress</artifactid>
<version>1.22</version>
</dependency>
<!-- apache poi for excel -->
<dependency>
<groupid>org.apache.poi</groupid>
<artifactid>poi</artifactid>
<version>5.2.3</version>
</dependency>
<dependency>
<groupid>org.apache.poi</groupid>
<artifactid>poi-ooxml</artifactid>
<version>5.2.3</version>
</dependency>
<!-- apache poi dependencies -->
<dependency>
<groupid>org.apache.xmlbeans</groupid>
<artifactid>xmlbeans</artifactid>
<version>5.1.1</version>
</dependency>2、生成水印代码
public static bytearrayoutputstream createwatermark(string content)
throws ioexception {
// 这是水印字体的宽和高,可以调整这个大小来控制
int width = 200;
int height = 150;
bufferedimage image = new bufferedimage(width, height,
bufferedimage.type_int_rgb);// 获取bufferedimage对象
string fonttype = "微软雅黑";
int fontstyle = font.bold;
// 这是水印显示字体的大小
int fontsize = 20;
font font = new font(fonttype, fontstyle, fontsize);
graphics2d g2d = image.creategraphics(); // 获取graphics2d对象
image = g2d.getdeviceconfiguration().createcompatibleimage(width,
height, transparency.translucent);
g2d.dispose();
g2d = image.creategraphics();
g2d.setcolor(new color(0, 0, 0, 20)); // 设置字体颜色和透明度,最后一个参数为透明度
g2d.setstroke(new basicstroke(1)); // 设置字体
g2d.setfont(font); // 设置字体类型 加粗 大小
g2d.rotate(-0.5, (double) image.getwidth() / 2,
(double) image.getheight() / 2);// 设置倾斜度
fontrendercontext context = g2d.getfontrendercontext();
rectangle2d bounds = font.getstringbounds(content, context);
double x = (width - bounds.getwidth()) / 2;
double y = (height - bounds.getheight()) / 2;
double ascent = -bounds.gety();
double basey = y + ascent;
// 写入水印文字原定高度过小,所以累计写水印,增加高度
g2d.drawstring(content, (int) x, (int) basey);
// 设置透明度
g2d.setcomposite(alphacomposite.getinstance(alphacomposite.src_over));
// 释放对象
g2d.dispose();
bytearrayoutputstream os = new bytearrayoutputstream();
imageio.write(image, "png", os);
return os;
}目前生成图片的效果图如下,如果像调整谁
3、excel调用水印代码
public static void main(string[] args) throws ioexception {
xssfworkbook workbook = new xssfworkbook();
xssfsheet sheet = workbook.createsheet("sheet1");
bytearrayoutputstream bytearrayoutputstream = createwatermark("水印内容");
int pictureidx = workbook.addpicture(bytearrayoutputstream.tobytearray()
,workbook.picture_type_png);
string rid = sheet.addrelation(null, xssfrelation.images,workbook
.getallpictures().get(pictureidx)).getrelationship().getid();
sheet.getctworksheet().addnewpicture().setid(rid);
// 创建一个行对象
xssfrow row = sheet.createrow(0); // 第一行
// 在行中创建单元格并设置值
xssfcell cell = row.createcell(0); // 第一列
cell.setcellvalue("hello, excel!");
// 写入到文件系统
try (fileoutputstream outputstream = new fileoutputstream("example.xlsx")) {
workbook.write(outputstream);
} catch (ioexception e) {
e.printstacktrace();
} finally {
try {
workbook.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
4、输出效果

到此这篇关于java实现excel导出并添加水印的文章就介绍到这了,更多相关java excel导出内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论