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);}}
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);}}}}
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse