Tool:Free Spire.XLS for Java (free version)
Note: You can download the package through the official website and unzip it Import the jar file in the lib folder into the java program; or download and import it through the maven warehouse.
JarImport effect:
##Java code exampleExample 1 Add imageimport com.spire.xls.*; public class AddImage { public static void main(String[] args) { //加载文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //获取工作表 Worksheet sheet = wb.getWorksheets().get(0); //添加图片 ExcelPicture picture = sheet.getPictures().add(7,2,"tp.png"); picture.setHeight(270);//设置图片高度 picture.setWidth(550);//设置图片宽度 picture.setRotation(20);//设置图片旋转角度 picture.setAlternativeText("Picture1");//设置图片可选文本 picture.setHyperLink("http://www.baidu.com",true);//添加超链接到图片 //保存文档 wb.saveToFile("AddImage.xlsx", ExcelVersion.Version2010); wb.dispose(); } }
import com.spire.xls.*; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ExtractImage { public static void main(String[] args) throws IOException { //加载文档 Workbook wb = new Workbook(); wb.loadFromFile("AddImage.xlsx"); //获取第一张工作表 Worksheet sheet = wb.getWorksheets().get(0); //获取工作表中第一张图片并保存到指定路径 ExcelPicture pic = sheet.getPictures().get(0); BufferedImage loImage = pic.getPicture(); ImageIO.write(loImage,"jpg",new File("ExtractedImage.jpg")); } }
import com.spire.xls.*; public class RemoveImage { public static void main(String[] args) { //加载文档 Workbook wb = new Workbook(); wb.loadFromFile("AddImage.xlsx"); //获取指定工作表 Worksheet sheet = wb.getWorksheets().get(0); //获取指定图片,删除 sheet.getPictures().get(0).remove(); //保存文档 wb.saveToFile("RemoveImage.xlsx",ExcelVersion.Version2010); wb.dispose(); } }
The above is the detailed content of How to use Java to add, read and delete pictures in Excel?. For more information, please follow other related articles on the PHP Chinese website!