2013-07-09 89 views
1

我想將我的excel文件以及它的實體(圖表,圖像)轉換爲jpeg/png圖像。目前正在使用aspose。這裏是我的代碼Excel圖像轉換

public static int excelToImages(final String sourceFilePath, final String outFilePrefix) throws Exception { 
    int noOfImages = 0; 

    Workbook workbook = getWorkbook(sourceFilePath); 
    List<Worksheet> worksheets = getAllWorksheets(workbook); 

    if (worksheets != null) { 
     for (Worksheet worksheet : worksheets) { 
      if (worksheet.getCells().getCount() > 0) { 
       String outFilePath = FileUtils.getAbsoluteFilePath(outFilePrefix + (noOfImages++)); 

       SheetRender sr = new SheetRender(worksheet, getImageOrPrintOptions()); 
       sr.toImage(0, outFilePath); 
      } 
     } 
    } 
    return noOfImages; 
} 

private static ImageOrPrintOptions getImageOrPrintOptions() { 
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); 
    imgOptions.setImageFormat(ImageFormat.getJpeg()); 
    imgOptions.setOnePagePerSheet(true); 
    return imgOptions; 
} 

private static List<Worksheet> getAllWorksheets(final Workbook workbook) { 
    List<Worksheet> worksheets = new ArrayList<Worksheet>(); 

    WorksheetCollection worksheetCollection = workbook.getWorksheets(); 
    for (int i = 0; i < worksheetCollection.getCount(); i++) { 
     worksheets.add(worksheetCollection.get(i)); 
    } 
    return worksheets; 
} 

我的問題是輸出圖像的大小或者拆分成多個A4尺寸或單1張取決於是否擁有

imgOptions.setOnePagePerSheet(true); 

誰能告訴我怎麼可以自定義輸出圖像文件的大小?

回答

0

您可以使用imgOptions.setOnlyArea(true);來嘗試。這會將圖像的大小設置爲將所有內容放入圖像所需的最小值。但我不確定生成的圖像是否分割成A4部分。