Images are universally supported and can be seamlessly embedded and displayed in a variety of web platforms and documents. When you intend to publish Excel charts on websites or include them in videos or presentations, converting them to images is a practical option. By doing so, you can eliminate any potential compatibility issues and ensure that these charts are accessible to a wider audience. In this article, we will demonstrate how to convert Excel charts to images in Java using Spire.XLS for Java.
- Convert a Specific Chart in an Excel Worksheet to an Image in Java
- Convert All Charts in an Excel Worksheet to Images in Java
- Convert a Chart Sheet to an Image in Excel in Java
Install Spire.XLS for Java
First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls</artifactId> <version>14.8.2</version> </dependency> </dependencies>
Convert a Specific Chart in an Excel Worksheet to an Image in Java
Spire.XLS for Java provides the Workbook.saveChartAsImage(Worksheet worksheet, int chartIndex) method to convert a specific chart in a worksheet as an image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet by its index using Workbook.getWorksheets().get(int index) method.
- Save a specific chart in the worksheet as an image using Workbook.saveChartAsImage(Worksheet worksheet, int chartIndex) method.
- Save the image to a PNG file.
- Java
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 ConvertAExcelChartToImage { public static void main(String[] args) throws IOException { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load a sample Excel file workbook.loadFromFile("Charts.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Save the first chart in the first worksheet as an image BufferedImage image = workbook.saveChartAsImage(sheet, 0); //Save the image to a .png file ImageIO.write(image, "PNG", new File("output\\chart.png")); workbook.dispose(); } }
Convert All Charts in an Excel Worksheet to Images in Java
To convert all charts in an Excel worksheet to images, you can use the Workbook.saveChartAsImage(Worksheet worksheet) method. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific worksheet by its index using Workbook.getWorksheets().get(int index) method.
- Save all charts in the worksheet as images using Workbook.saveChartAsImage(Worksheet worksheet) method.
- Save the images to PNG files.
- Java
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 ConvertAllExcelChartsToImage { public static void main(String[] args) throws IOException { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load a sample Excel file workbook.loadFromFile("Charts.xlsx"); //Get the first worksheet Worksheet sheet = workbook.getWorksheets().get(0); //Save all charts in the first worksheet as images BufferedImage[] imgs = workbook.saveChartAsImage(sheet); //Save the images to .png files for (int i = 0; i < imgs.length; i++) { File file = new File("output\\" + String.format(("chart-%d.png"), i)); ImageIO.write(imgs[i], "PNG", file); } workbook.dispose(); } }
Convert a Chart Sheet to an Image in Excel in Java
A chart sheet in Excel is a separate sheet dedicated solely to displaying a chart. You can use the Workbook.saveChartAsImage(ChartSheet chartSheet) method to convert a chart sheet in an Excel workbook to an image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.loadFromFile() method.
- Get a specific chart sheet by its index using Workbook.getChartsheets().get(int index) method.
- Save the chart sheet as an image using Workbook.saveChartAsImage(ChartSheet chartSheet) method.
- Save the image to a .png file.
- Java
import com.spire.xls.ChartSheet; import com.spire.xls.Workbook; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ConvertExcelChartSheetToImage { public static void main(String[] args) throws IOException { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load a sample Excel file workbook.loadFromFile("ChartSheet.xlsx"); //Get the first chart sheet ChartSheet chartSheet = workbook.getChartsheets().get(0); //Save the first chart sheet as an image BufferedImage image = workbook.saveChartAsImage(chartSheet); //Save the image to a .png file ImageIO.write(image, "PNG", new File("output\\chartSheet.png")); workbook.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.