Export AutoShapes in ExcelSheet as Image

Hi


Does Aspose.Cells Java have any API to read shaped or grouped auto-shapes in an excel (xlsx) and save them as image file.

If yes can someone provide a sample code.

Regards,

Rehan

Hi,


Thanks for your query.

See the following piece of code that renders images of shapes from the first worksheet in the workbook.

[Java]

Workbook excelDocument = new Workbook(“Book1.xlsx”);
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.setImageFormat(ImageFormat.getPng());
ShapeCollection shapes = excelDocument.getWorksheets().get(0).getShapes();
for(int i=0;i<shapes.getCount();i++)
{
Shape shape = shapes.get(i);
shape.toImage(“f:/files/output” + i + “.png”,imageOrPrintOptions);
}

You may browse Shape class (in the API Reference docs) to utilize different methods to know more about its type (e.g using Shape.getType()/getAutoShapeType() method), position, size and other details and attributes for the Shape class for your requirements.

Hope, this helps a bit.

Thank you.

Hi,


Tried above code , those smart shapes inserted within Excel or created within excel we can export but some how autoshapes that the attached file contain are not being loaded.

My original file is a Excel Macro-Enabled Workbook file(xlsm)

Any quick help in this regard will be highly appreciated and will also help us to finalize product selection to use ASPOSE.

Thanks and Best Regards,

Rehan

Hi,


Thanks for the template file.

Please try our latest version/fix: Aspose.Cells for Java v8.8.0.3 (attached).

I have tested using the following sample code with v8.8.0.3 using Sheet to Image feature and it works fine. I have also attached the output image for your reference.
e.g
Sample code:

String path = “RehanAzher.xlsx”;

Workbook workbook = new Workbook(path);

Worksheet worksheet = workbook.getWorksheets().get(0);

ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setOnePagePerSheet(true);
options.setImageFormat(ImageFormat.getPng());
SheetRender sr = new SheetRender(worksheet, options);
sr.toImage(0, “f:\files\out1.png”);

Let us know if you still have any issue.

Thank you.

Hi Amjad,


Thanks for the quick response , what you have done is to render the whole worksheet to image , our requirement is slightly different . We have many such objects in our excel worksheet and we need to export those one by one not the whole worksheet as one.

The file I attached previously was just for sample requirement purpose.

Thanks and Best regards,

Rehan

Hi,


Thanks for providing us further details.

I have evaluated the auto-shapes in your template file a bit. Well, all the shapes are grouped/merged into a group shape named “Group 1”, so you need to get that group shape (auto-shape) and render to image file for your requirements. I have updated the code segment to accomplish your task. See the following sample code for your reference:
e.g
Sample code:

Workbook excelDocument = new Workbook(“RehanAzher.xlsx”);
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.setImageFormat(ImageFormat.getPng());
imageOrPrintOptions.setOnePagePerSheet(true);//render one whole image
ShapeCollection shapes = excelDocument.getWorksheets().get(0).getShapes();
for(int i=0;i<shapes.getCount();i++)
{
Shape shape = shapes.get(i);
if(shape.getName().equals(“Group 1”))
{
shape.toImage(“f:/files/output1_new1” + i + “.png”,imageOrPrintOptions);
}
}

Hope, this helps a bit.

Thank you.

Hi Amjad,


Thanks for quick response again , i was able to get Shape using your code snippet above from both format xlsx and xlsm . Somehow same code snippet is not working on attached file . Any pointers on this please?

Thanks and Best Regards,

Rehan

Hi,


Thanks for the template file.

I have evaluated your issue a bit using your newly attached file. I found your template file has some external links/reference and MS Excel tried to connect updated data from the external sources, I got some error message boxes in MS Excel. Also and more importantly, some worksheets are hidden, so when you use the following line of code, you won’t get the underlying sheet, it would get the worksheet (whether hidden or visible) at zero indexed position in any case:
e.g
Sample code:
ShapeCollection shapes = excelDocument.getWorksheets().get(0).getShapes();

So, you need to change the line of code a bit, kindly access the worksheet via its name rather than indexed position, see the following sample code that works fine with our latest version/fix: v8.8.0.3 (attached), it renders the image fine.
e.g
Sample code:

Workbook excelDocument = new Workbook(“ImageNe2.xlsm”);
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.setImageFormat(ImageFormat.getPng());
imageOrPrintOptions.setOnePagePerSheet(true);
ShapeCollection shapes = excelDocument.getWorksheets().get(“1.1 Antenna System (RF)”).getShapes();
for(int i=0;i<shapes.getCount();i++)
{

Shape shape = shapes.get(i);
if(shape.getName().equals(“Group 1299”))
{
shape.toImage(“f:/files/output1_new2” + i + “.png”,imageOrPrintOptions);
}
}


Let us know if you still have any issue.

Thank you.