简体   繁体   English

Aspose单元格Java,如何从table / ListObject创建图像

[英]Aspose cells java, How to create image from table/ListObject

How to convert table/ListObject to and image? 如何将表/ ListObject转换为和图像?

In excel file I have tables, charts and images and I am using aspose cells java. 在excel文件中,我有表格,图表和图像,并且正在使用aspose单元格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. 我的要求是将所有这些详细信息作为独立的图像获取,并且有用于将图表和形状转换为图像的api,但是对于表/ ListObject没有api。 So can anybody knows how tables are converted into images using aspose calls java liberary 所以有人知道如何使用aspose调用java liberary将表转换为图像吗?

To Convert table/ListObject to image 将表/ ListObject转换为图像

  1. Get ListObject 获取ListObject

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

  2. Now create a new workbook with having only this listObject. 现在,仅使用此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 然后将此工作簿转换为图像,并将图像选项的属性“ setOnlyArea”设置为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. 工作簿是仅具有listObject的新创建的工作簿,输出文件路径是保存输出图像的绝对路径。

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. 链接: http : //iandjava.blogspot.com/2013/07/convert-excel-document-to-images.html使用此链接,您可以将excel及其组件转换为图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM