1

I am using OpenXML SDK to generate Word Documents in C#. When it comes to chart, it does not support Edit Data in Excel functionality.

I found some sample code here to generate a chart. Please right-click the chart area and then choose Edit Data, nothing happens. https://code.msdn.microsoft.com/office/How-to-create-Chart-into-a7d424f6

enter image description here

The expected behavior is opening Excel with data. enter image description here

I also tried with another popular OpenSource library Xceed Docx https://github.com/xceedsoftware/docx

The same issue is there also. Syncfusion does support this feature with a license cost.

How can I achieve this feature using OpenXML SDK or DocX or any other open source library?

Edited Nov'30 - I am aware of creating a document from a template (the template has a chart already, and it supports Edit Excel feature) and then either copy it/generate code and then updates the chart values using OpenXMLSDK PowerTool/ OpenXMLSDK Productivity Tool.

ex: OpenXmlPowerTools.ChartUpdater.UpdateChart() http://ericwhite.com/blog/update-cached-data-and-embedded-xlsx-for-charts-in-docx-pptx/

I am looking for creating a chart in a fresh document like syncfusion component. Syncfusion doesn't expect any template.

Any help would be appreciated. Thanks

  • Your question title says "OpenXML SDK does not support Edit Data in Excel functionality in Chart", yet it does. And my answer shows you step by step how to do this. After I answered, you have changed your question (yet you left the title). My solution answers your original question before you changed it. You can ask a detailed question over at the Software Recommendation - see https://softwarerecs.meta.stackexchange.com/questions/8/is-it-alright-to-ask-for-programming-tools – Taterhead Nov 30 '17 at 21:42
  • Hi Taterhead, I agreed and changed the title also. In my scenario, the template approach I may not use. I assume that people will use the example code on the link I provided. I am waiting for an answer to a fresh document approach. if I do not get any answer after 7 days, I will accept this as an answer. – robert jebakumar2 Dec 01 '17 at 05:36
  • Hi Taterhead, Why the example code I mentioned is not allowing the Edit Excel feature. any ideas? One way is to unzipping and comparing the docx files of OpenXml SDK and manually inserting chart using Ribbon. Any help would be appreciated – robert jebakumar2 Dec 01 '17 at 05:47
  • I just downloaded the code and generated the docx with the chart from the MSDN link. I was successfully able to Edit the data in excel using the right click menu. I'm on Win10 with Excel/Word 2016. Maybe your excel process was hung and prevented Excel launch. Try clearing zombie Excel processes with either the task manager or a reboot. – Taterhead Dec 01 '17 at 18:27

1 Answers1

2

Yes, this is possible using the free OpenXML SDK and the OpenXML Productivity Tool. Follow these steps:

  1. Begin by installing both the OpenXML SDK and the OpenXML Productivity Tool onto your development machine.
  2. Using Word, generate a Word document containing a Pie Chart with your desired final requirements.
  3. Using the Productivity Tool, open the word doc created in step 2 and click the Reflect Code button at the top. The c# code needed to generate the document will be in the window on the right.
  4. Using Visual Studio, create a simple C# console app project called WordChartGenerator. For this project, type Install-Package DocumentFormat.OpenXML in the Package Manager console. This pulls in the necessary DLLs for your project to use OpenXML.
  5. Create a new class file for this project: Call it GeneratedClass.cs and replace the entire contents using code generated from step 3.
  6. Change the namespace line at the top of GeneratedClass.cs to be namespace WordChartGenerator
  7. Go back to the Program.cs file and inside of main, instantiate an instance of the GeneratedClass class and call CreatePackage with a path to the desired full file path name. Something like the following:

    class Program
    {
        static void Main(string[] args)
        {
            var wordGenerator = new GeneratedClass();
        wordGenerator.CreatePackage("C:\\Users\\jeff\\Documents\\WordWithChart.docx");
        }
    }
    
  8. run the program and it will generate your file in the directory you specify.

The below screen shot is from a file generated with these steps

enter image description here

Taterhead
  • 5,763
  • 4
  • 31
  • 40