In this article, I will show you how to convert Excel to common image formats such as PNG, JPEG, TIFF and SVG programmatically in Java application. The article will be divided into the following three parts for demonstration:
- Convert Excel to Image (PNG, JPEG)
- Convert Excel to TIFF
- Convert Excel to SVG
Add dependencies
Free Spire.XLS for Java library is used to implement this task. If you use maven, you need to specify the following dependencies for Free Spire.XLS for Java library in your project’s pom.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <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> |
For non-maven projects, you can download Free Spire.XLS for Java pack from this website and add Spire.Xls.jar in the lib folder into your project as a dependency.
The input Excel file
Convert Excel to Image (PNG, JPEG)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import com.spire.xls.Workbook; import com.spire.xls.Worksheet; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; public class ExcelToImage { public static void main(String []args) throws Exception { //Load the Excel file Workbook workbook = new Workbook(); workbook.loadFromFile( "Input.xlsx" ); //Loop through worksheets for ( int i = 0 ; i < workbook.getWorksheets().size(); i++) { //Convert worksheet to image Worksheet sheet = workbook.getWorksheets().get(i); BufferedImage bufferedImage = sheet.toImage(sheet.getFirstRow(), sheet.getFirstColumn(), sheet.getLastRow(), sheet.getLastColumn()); ImageIO.write(bufferedImage, "PNG" , new File( "image/SheetToImage" +i+ ".png" )); } } } |
Output:
Convert Excel to TIFF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import com.spire.xls.Workbook; import com.spire.xls.Worksheet; public class ExcelToTiff { public static void main(String []args) throws Exception { //Load the Excel file Workbook workbook = new Workbook(); workbook.loadFromFile( "Input.xlsx" ); //Loop through worksheets for ( int i = 0 ; i < workbook.getWorksheets().size(); i++) { //Convert worksheet to tiff Worksheet sheet = workbook.getWorksheets().get(i); //Save the first worksheet to tiff sheet.saveToTiff( "tiff/SheetToTiff" + i + ".tif" ); } } } |
Output:
Convert Excel to SVG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import com.spire.xls.Workbook; import com.spire.xls.Worksheet; import java.io.FileOutputStream; public class ExcelToSvg { public static void main(String []args) throws Exception { //Open xls document Workbook workbook = new Workbook(); workbook.loadFromFile( "Input.xlsx" ); //Traverse worksheets for ( int i = 0 ; i < workbook.getWorksheets().size(); i++) { FileOutputStream stream = new FileOutputStream( "svg/sheet" + i + ".svg" ); //Convert worksheet to svg file Worksheet sheet = workbook.getWorksheets().get(i); sheet.toSVGStream(stream, sheet.getFirstRow(), sheet.getFirstColumn(), sheet.getLastRow(), sheet.getLastColumn()); stream.flush(); stream.close(); } } } |
Output:
Comments
Post a Comment