Search code examples
openxmldocxopenxml-sdkxceednovacode-docx

OpenXML SDK does not support Edit Data in Excel functionality in Chart in a fresh document


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


Solution

  • 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