DEV Community

loading...

Convert Excel Chart to Image in Java

jazzzzz profile image Jane ・1 min read

In some cases, when we create a chart in an Excel document but don't want the detailed data to be seen by others, we can convert just the Excel chart to image for better sharing. In this article, you will learn how to do this with Free Spire.XLS for Java.

Installation
Method 1: Download the Free Spire.XLS for Java and unzip it. Then add the Spire.Xls.jar file to your project as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls.free</artifactId>
        <version>3.9.1</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

The example Excel file:

Code Snippet

import com.spire.xls.Workbook;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ChartToImage {
    public static void main(String[] args) throws IOException {
        //Load the Excel file
        Workbook workbook = new Workbook();
        workbook.loadFromFile("ColumnChart.xlsx");

        //Save the first chart in the first worksheet as image
        BufferedImage image= workbook.saveChartAsImage(workbook.getWorksheets().get(0), 0);
        ImageIO.write(image,"png", new File("ChartToImage.png"));
    }
}

Enter fullscreen mode Exit fullscreen mode

The output image:

Discussion (0)

pic
Editor guide