本文介绍如何使用spire.xls for java添加图片到excel单元格并设置超链接。
import com.spire.xls.*;
public class insertimagehyperlink {
public static void main(string[] args) {
//创建workbook对象并获取第一个工作表
workbook wb = new workbook();
worksheet sheet = wb.getworksheets().get(0);
//在单元格a1中添加文字
sheet.getcellrange("a1").settext(" 图片超链接");
sheet.getcellrange("a1").getstyle().setverticalalignment(verticalaligntype.top);
//添加图片到单元格a1
string picpath = "c:\\users\\administrator\\desktop\\logo.png";
excelpicture picture = sheet.getpictures().add(1,1,picpath);
//为图片设置超链接
picture.sethyperlink("http://www.e-iceblue.cn", true);
//设置行高和列宽
sheet.getcolumns()[0].setcolumnwidth(22);
sheet.getrows()[0].setrowheight(140);
picture.settoprowoffset(25);
//保存文档
wb.savetofile("imagehyperlink.xlsx", excelversion.version2016);
}
}