Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Convert Excel to Image in Java

With the help of Spire.XLS for Java, we can easily convert Excel workbook and Excel worksheets to image with high quality in Java applications. In this article, I will explain the solutions of converting the Excel file to bitmap images in PNG, BMP, JPEG etc. And when we convert Excel to Images, we can set the DPI for the image and convert the Excel to image by page breaks.

Converting Excel to Image and set the image DPI. Spire.XLS for Java offers sheet.saveToImage() method to save Excel as image directly. And we could also use sheet.saveToImage (result, firstRow, firstColumn, lastRow, lastColumn); to convert the defined cell range to images.

importcom.spire.xls.*;publicclassExceltoImage{publicstaticvoidmain(String[]args){//Load the sample documentWorkbookwb=newWorkbook();wb.loadFromFile("Sample.xlsx");//set the image DPIwb.getConverterSetting().setXDpi(200);wb.getConverterSetting().setYDpi(200);//get the first worksheetWorksheetsheet=wb.getWorksheets().get(0);//Save to imagesheet.saveToImage("ExceltoImage.png");//save the defined cell range to imagesheet.saveToImage("CellrangesToImg.png",5,1,14,4);}}
Enter fullscreen modeExit fullscreen mode

Convert the Excel to Images by page breaks. When we deal with the Excel with a large of data, we will add the page breaks to the Excel file to ensure it can be printed and converted correctly. Spire.XLS for java supports to convert Excel to images by page breaks.

importcom.spire.xls.*;importjava.util.List;importjava.util.Map;publicclassExceltoImagebyPagebreaks{publicstaticvoidmain(String[]args)throwsException{//Load the sample documentWorkbookworkbook=newWorkbook();workbook.loadFromFile("Sample.xlsx");intcount=1;//get the page split info.List<Map<Integer,PageColRow>>pageInfoList=workbook.getSplitPageInfo();//Convert to images by pagebreaksfor(inti=0;i<workbook.getWorksheets().getCount();i++){Worksheetsheet=workbook.getWorksheets().get(i);Map<Integer,PageColRow>integerPageColRowMap=pageInfoList.get(i);for(Map.Entry<Integer,PageColRow>entry:integerPageColRowMap.entrySet()){PageColRowcolRow=entry.getValue();Stringresult=("output/out"+(count++)+".png");sheet.saveToImage(result,colRow.StartRow,colRow.StartCol,colRow.EndRow,colRow.EndCol);}}}}
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Your Office Development Master
  • Location
    Chengdu, SiChuan, China
  • Work
    Dev at E-iceblue
  • Joined

More fromE-iceblue Product Family

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp