首先要先下載一個aspose.cell.*的包 能夠百度去找一下java
代碼以下測試
package com.pl.Excel; import java.io.File; import com.aspose.cells.*; /** * Excel轉PDF */ public class ExcelToPdf { public static void ConvertToImage() { Workbook book=null; try { book=new Workbook( "E:\\測試.xlsx" ); String path="E:\\測試.xlsx"; //獲取到sheet Worksheet sheet=book.getWorksheets().get( 5 ); //設置截取範圍 String area=7 + ":" + 31;//這個地方能夠選取截取的範圍 System.out.println( "area: " + area ); sheet.getPageSetup().setPrintArea( area ); // 設置圖片數據的邊距 sheet.getPageSetup().setLeftMargin( -20 );//設置左邊距 sheet.getPageSetup().setRightMargin( 0 );//設置右邊距 sheet.getPageSetup().setBottomMargin( 0 );//設置下邊距 sheet.getPageSetup().setTopMargin( 0 ); // 定義ImageOrPrintOptions ImageOrPrintOptions imgOptions=new ImageOrPrintOptions(); /* 指定圖像格式*/ imgOptions.setImageFormat( ImageFormat.getJpeg() ); imgOptions.setCellAutoFit( true ); imgOptions.setOnePagePerSheet( true ); //imgOptions.setDesiredSize(1000,800); // 根據指定的圖像/打印選項渲染圖紙 SheetRender render=new SheetRender( sheet, imgOptions ); // Render the image for the sheet //render.toImage(0, dataDir + "SheetImage.jpg"); render.toImage( 0, "E:\\報表.jpg" ); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { ConvertToImage(); } }