如何将table / ListObject转换为图像?
在excel文件中我有表格,图表和图像,我使用的是aspose cells java。我的要求是将所有这些细节作为一个独立的图像,并有转换图表和形状转换为图像的api,但没有api表/ ListObject。所以任何人都可以使用aspose调用java liberary
知道如何将表转换为图像答案 0 :(得分:2)
将table / ListObject转换为图像
获取ListObject
ListObject listObject = worksheet.getListObjects()。get(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;
}
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及其组件转换为图像。