本文将介绍如何使用spire.xls for java在excel文档中插入和提取图片。
插入图片
import com.spire.xls.excelpicture;
import com.spire.xls.excelversion;
import com.spire.xls.workbook;
import com.spire.xls.worksheet;
public class insertimage {
public static void main(string[] args){
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("input.xlsx");
//获取第一张工作表
worksheet sheet = workbook.getworksheets().get(0);
//添加图片到工作表的指定位置
excelpicture pic = sheet.getpictures().add(4, 1,"image.jpg");
//设置图片的宽度和高度
pic.setwidth(500);
pic.setheight(300);
//保存文档
workbook.savetofile("insertimage.xlsx", excelversion.version2013);
}
}
提取图片
import com.spire.xls.excelpicture;
import com.spire.xls.workbook;
import com.spire.xls.worksheet;
import javax.imageio.imageio;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
public class readimage {
public static void main(string[] args) throws ioexception {
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("insertimage.xlsx");
//获取第一张工作表
worksheet sheet = workbook.getworksheets().get(0);
//获取工作表中第一张图片并保存到指定路径
excelpicture pic = sheet.getpictures().get(0);
bufferedimage loimage = pic.getpicture();
imageio.write(loimage,"jpg",new file("output/readimage.jpg"));
}
}