之前我们介绍了如何插入和读取excel文本批注,该文将介绍如何使用spire.xls for java给excel文档添加图片批注。
import com.spire.xls.*;
import javax.imageio.imageio;
import java.awt.image.bufferedimage;
import java.io.*;
public class test {
public static void main(string[] args)throws ioexception {
//加载示例文档
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//设置字体
excelfont font = workbook.createfont();
font.setfontname("arial");
font.setsize(11);
font.setknowncolor(excelcolors.orange);
cellrange range = sheet.getcellrange("d1");
//添加批注
excelcomment comment = range.addcomment();
//加载图片
bufferedimage bufferedimage = imageio.read(new file("logo.jpg"));
//使用图片填充批注
comment.getfill().custompicture(bufferedimage, "logo.jpg");
//设置批注高度和宽度
comment.setheight(bufferedimage.getheight());
comment.setwidth(bufferedimage.getwidth());
//设置批注可见
comment.setvisible(true);
//保存文档
workbook.savetofile("output/setimagecomment.xlsx", excelversion.version2013);
}
}
效果图: