Reputation: 3026
How to convert table/ListObject to and image?
In excel file I have tables, charts and images and I am using aspose cells java. My requirement is to get all these details as an independent images and there are api's to convert charts and shapes to convert into an image but there is no api for tables/ListObject. So can anybody knows how tables are converted into images using aspose calls java liberary
Upvotes: 1
Views: 1389
Reputation: 3026
To Convert table/ListObject to image
Get ListObject
ListObject listObject = worksheet.getListObjects().get(0);
Now create a new workbook with having only this 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);
}
}
Workbook is the newly created workbook only having tha listObject and output file path is the absolute path where to save the output image.
Link: http://iandjava.blogspot.com/2013/07/convert-excel-document-to-images.html with this link you can convert excel and its components to images.
Upvotes: 2