We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy paste chart from one sheet to another

I want to use Aspose APIs for copy pasting a chart from one sheet to another sheet (which belongs to separate workbook not the source one).


I can find out the same APIs for range like: targetRange.copy(sourceRange) but not for charts.

Can you please provide me the sample to achieve the same.

Currently I am using Aspose Cells 8.1.0

Thanks,
Neeraj

Hi,

If you need to copy Chart shape in between worksheets in the same workbook, it would work absolutely fine, see the document for your reference:

Chart Rendering

But, if you need to copy Chart object to other workbook sheet, it might not work properly due to external reference for the chart. You may also confirm this behavior (the charts won’t copy) when you copy a range including chart in the sheet and paste it into other workbook’s sheet.

I think the best way to accomplish this is to take the image of the chart using Chart to Image feature provided by Aspose.Cells and then paste that picture into your other workbook sheet accordingly.

Alternatively, you may create the chart from the scratch in your other workbook by yourself via Aspose.Cells APIs.

Thank you.

Thanks for the response.


As per your suggestions I tried getting the chart as EMF image using ChartToImage API and then tried to Paste that picture using following code:

FileInputStream fis = new FileInputStream(new File(“C:\temp\Chart 291.emf”));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
}
byte[] bytes = bos.toByteArray();

ByteArrayInputStream bis = new ByteArrayInputStream(bytes);

//Copy the Picture to Second Worksheet
Workbook targetWorkbook = new Workbook(FileFormatType.XLSX);

WorksheetCollection targetWsc = targetWorkbook.getWorksheets();
com.aspose.cells.Worksheet targetWs = (com.aspose.cells.Worksheet)targetWsc.get(0);
targetWs.getPictures().add(0,0, 10, 6, bis);

//Save the workbook
targetWorkbook.save(“c:\temp\EmfCopy.pdf”, SaveFormat.PDF);


I got the PDf output but with a little problem. There are squares being printed in the text of the chart image.

Attached are the emf (remove .txt from the filename) and generated PDF files for your reference.

Please let me know what going wrong here.

Thanks,
Neeraj

Hi,


Thanks for the output PDF and image file.

I noticed the issue as you mentioned. There are squares attached to the data labels text in the chart. To evaluate your issue thoroughly, please attach the template Excel file that should contain the underlying chart, so we could investigate your issue and also test the Chart to Image conversion. After confirmation of the issue, we will log a ticket into our database, so it could be fixed soon.

Thank you.

Here is the file from where I extracted the emf image file (using chartToImage API) that was being used in the new workbook.


Thanks,
Neeraj

Hi,


Thanks for the template file containing the chart.

After an initial test, I observed the issue as you mentioned by using Aspose.Cells APIs with your template file and sample image (I even take the image of the chart (which looks to me fine) first and then inserted the chart image into the spreadsheet before rendering to the final PDF output).
e.g
Sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook(“test.xlsx”);
WorksheetCollection ws = workbook.getWorksheets();
Worksheet sheet1 = ws.get(0);

//get the Chart from first worksheet
Chart chart = sheet1.getCharts().get(0);

chart.toImage(“img__Chart1.emf”, ImageFormat.getEmf());

//Copy the Picture to Second Worksheet
Workbook targetWorkbook = new Workbook(FileFormatType.XLSX);

WorksheetCollection targetWsc = targetWorkbook.getWorksheets();
com.aspose.cells.Worksheet targetWs = (com.aspose.cells.Worksheet)targetWsc.get(0);
targetWs.getPictures().add(0,0, 10, 6, “img__Chart1.emf”);

//Save the workbook
targetWorkbook.save(“out1.pdf”, SaveFormat.PDF);

I have logged a ticket with an id “CELLSJAVA-40979” for your issue. We will look into your issue to figure it out soon.

Thank you.
Thanks for logging the ticket for the same.

Just to inform you that I have observed that square boxes are shown whenever any line break character appears in the text of the chart.

Hope it would help to narrow down the problem.

Thanks,
Neeraj

Hi,


Thanks for providing us further details.

We will investigate if we could avoid square boxes placed for line break char in the Data Labels’ text for the chart.

Once we figure it out, we will let you know here.

Thank you.

Hi,

Thanks for using Aspose.Cells for Java.

Please download and try this fix: Aspose.Cells for Java (Latest Version) and let us know your feedback.

Hi,


Thanks for providing the fix.

I have tested the given issue with the patch build “Aspose.Cells for Java v8.2.0.3”. It worked fine with this build.

Thanks,
Neeraj


Hi Neeraj,

Thanks for your feedback and using Aspose.Cells.

We are pleased to know that your issue is fixed with the latest version. We have closed this thread now. Let us know if you encounter any other issue, we will be glad to look into it and help you further.

The issues you have found earlier (filed as CELLSJAVA-40979) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.