There have been several requests made by people asking how to import a chart from one document type to another document type. In a previous post, I showed you how to push data into a spreadsheet to create a chart. Today, I am going to show you how to import a chart from a spreadsheet to a Word document.
To import a chart from a spreadsheet to a Word document we need to take the following actions:
My post will talk about using version 2 of the SDK.
For the sake of this post, let's say I am starting with the following spreadsheet and chart:
Also, let's say I am starting with the following Word document, which contains a placeholder content control:
If you just want to jump straight into the code, feel free to download this solution here.
As described in the solution section above, steps two through four require us to open the Word document, find the content control that will contain the imported chart, and nuke the placeholder content of the content control to get it ready to contain the chart. Below is the code snippet necessary to accomplish those tasks:
You might notice the code used to find the proper content control looks very much like the code I used in my previous post for assembling Word documents. By nuking all the children under the paragraph within the content control, we have made it ready to contain a chart.
Instead of having run and text elements, the content control needs to have a run with an inline drawing element so that we can reference the chart properly. This task is accomplished with the following code snippet:
Notice that I have hardcoded the dimensions of the chart I will be importing. The dimensions I used are based on what I thought would work best for my template. Feel free to choose any dimension that works best for your document. At this point, the Word document is ready to have the chart imported and be contained within the content control. To import the chart we need to first get the proper chart part and then add that part to my Word document. These tasks are accomplished with the following code snippet:
The chart part is now part of my Word document package. All we have to do now is reference the chart part properly from the inline drawing contained in the content control. There are two main items we need to include to get everything to work:
#1 is easy to accomplish since we can reuse the same graphic objects from the spreadsheet. The main difference is we need to make sure the relationship is pointing to the appropriate chart part within the Word document. Here is the code snippet to accomplish item #1:
#2 requires us to calculate a unique id for the chart. Here is a method that can accomplish this task:
Almost done! The last bit of work we need to do is add the unique name and id to chart drawing and then add everything to the inline drawing object. Here is the code snippet to accomplish these tasks:
Putting everything together and running this code, we end up with a Word document, which contains the chart imported from my spreadsheet.
Here is a screenshot of the final document: