2013-06-26 52 views
1

如何將table/ListObject轉換爲圖像?Aspose cells java,如何從表/ ListObject創建圖像

在excel文件中我有表格,圖表和圖像,我正在使用aspose單元格java。我的要求是將所有這些細節作爲一個獨立的圖像獲取,並且有api將圖表和形狀轉換爲圖像,但對於表/ ListObject沒有api。因此,任何人可以知道表是如何轉化爲使用的Aspose圖像調用java的liberary

+0

請說清楚,並以良好的方式解釋讓他人理解你的問題。 –

+0

我認爲你可以使用OpenOffice API來執行此操作(http://www.openoffice.org/api/)。 – pepuch

+0

我只能使用aspose。 –

回答

2

轉換表/的ListObject圖像

  1. 獲取的ListObject

    的ListObject的ListObject = worksheet.getListObjects()得到( 0);

  2. 現在創建一個僅具有此listObject的新工作簿。


public static Workbook createWorkBook(final Worksheet worksheet, final ListObject listObject) throws Exception { 
    Workbook workbook = new Workbook(); 
    Cells cells = workbook.getWorksheets().get(0).getCells(); 

    Cells cellsTobeCopied = worksheet.getCells(); 

    int totalNoOfRows = listObject.getEndRow() - listObject.getStartRow() + 1; 
    int totalNoOfColumns = listObject.getEndColumn() - listObject.getStartColumn() + 1; 

    cells.setStandardHeight(cellsTobeCopied.getStandardHeight()); 
    cells.setStandardWidth(cellsTobeCopied.getStandardWidth()); 

    // Set height of each row as the height of actual rows of table 
    for (int row = 0; row < totalNoOfRows; row++) { 
     cells.setRowHeight(row, cellsTobeCopied.getRowHeight(row)); 
    } 

    // Set width of each column as the width of actual columns of table 
    for (int column = 0; column < totalNoOfColumns; column++) { 
     cells.setColumnWidth(column, cellsTobeCopied.getColumnWidth(column)); 
    } 

    // Copy data of table from worksheet to newly created workbook cells 
    for (int row = 0; row < totalNoOfRows; row++) { 
     for (int column = 0; column < totalNoOfColumns; column++) { 
      Cell copiedFrom = worksheet.getCells().get(listObject.getStartRow() + row, listObject.getStartColumn() + column); 
      Cell copyTo = cells.get(row, column); 

      copyTo.setHtmlString(copiedFrom.getHtmlString()); 
     } 
    } 

    // Create table in newly created workbook 
    ListObjectCollection tables = workbook.getWorksheets().get(0).getListObjects(); 
    tables.add(0, 0, totalNoOfRows - 1, totalNoOfColumns - 1, listObject.getShowHeaderRow()); 

    return workbook; 
} 

  1. 那麼這個工作簿轉換爲圖像與圖像選項物業'setOnlyArea'爲真


public static void toImage(final Workbook workbook, final String outputFilePath) throws Exception { 
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); 
    imgOptions.setImageFormat(ImageFormat.getPng()); 
    imgOptions.setOnlyArea(true); 

    Worksheet sheet = workbook.getWorksheets().get(0); 
    SheetRender sr = new SheetRender(sheet, imgOptions); 

    for (int j = 0; j < sr.getPageCount(); j++) { 
     sr.toImage(j, outputFilePath); 
    } 
} 

工作簿新建工作簿只能有塔的ListObject和輸出文件路徑是保存輸出圖像的絕對路徑。

鏈接:http://iandjava.blogspot.com/2013/07/convert-excel-document-to-images.html 使用此鏈接,您可以將excel及其組件轉換爲圖像。