DEV Community


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.

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.

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;

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

        //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)

Editor guide