轉換表/的ListObject圖像
獲取的ListObject
的ListObject的ListObject = worksheet.getListObjects()得到( 0);
現在創建一個僅具有此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;
}
- 那麼這個工作簿轉換爲圖像與圖像選項物業'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及其組件轉換爲圖像。
請說清楚,並以良好的方式解釋讓他人理解你的問題。 –
我認爲你可以使用OpenOffice API來執行此操作(http://www.openoffice.org/api/)。 – pepuch
我只能使用aspose。 –