1

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

4
  • Please Make it clear and explain in good manner to make others to understand your question. Commented Jun 26, 2013 at 6:35
  • I think that you can use OpenOffice API to do this (openoffice.org/api). Commented Jun 26, 2013 at 6:45
  • I have to use only aspose for that. Commented Jun 26, 2013 at 6:50
  • @downvoter please comment Commented Dec 26, 2013 at 5:34

1 Answer 1

2

To Convert table/ListObject to image

  1. Get ListObject

    ListObject listObject = worksheet.getListObjects().get(0);

  2. 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;
}

  1. Then convert this workbook as image with image option as property 'setOnlyArea' as true


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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.