Home >Java >javaTutorial >How to use Java to add, read and delete pictures in Excel?

How to use Java to add, read and delete pictures in Excel?

WBOY
WBOYforward
2023-04-24 23:49:102066browse

Introduction

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:

How to use Java to add, read and delete pictures in Excel?

##Java code example

Example 1 Add image

import 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();
    }
}

Picture adding effect:

How to use Java to add, read and delete pictures in Excel?

Example 2 Reading pictures

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"));
    }
}

Picture reading results:

How to use Java to add, read and delete pictures in Excel?

Example 3 Deleting pictures

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();
    }
}

After running the program, the generated file can view the picture deletion effect.

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete
Previous article:How does Java ORM work?Next article:How does Java ORM work?