Answered by:
how can i update charts' data in word document by using OpenXML

Question
-
There was 18 charts In my word document . I can get the charts by code like this:
private MainDocumentPart mainDocPart;
......
ChartPart c_p = mainDocPart.ChartParts.FirstOrDefault();
Then I can update the data of charts.But the problem is this:
I have 18 charts In the document , and the charts index was random.So I can't get the chart by index yet:
ChartPart c_p = mainDocPart.ChartParts.ElementAt(index);//index is Integer Value
So can I appoint the ID or Name for each of the 18 charts?And Can I get Them By ID or Name?
I can update one Charts data by Openxml.If neither,How can I update datas for so many charts by OpenXml?
赵召
- Edited by 赵召 Monday, May 5, 2014 7:28 AM
- Moved by Cindy Meister MVP Tuesday, May 6, 2014 3:52 PM Open XML question
Monday, May 5, 2014 7:26 AM
Answers
-
I think the following discussions may help you
http://social.msdn.microsoft.com/Forums/office/en-US/8e177aef-a43f-496e-bcf9-3d67f564cbf9/find-all-bookmarks-and-add-text-to-them?forum=oxmlsdk
http://social.msdn.microsoft.com/Forums/office/en-US/80b4b560-ee22-4bdb-94e9-cfabdad701d4/bookmark-on-checkbox-and-replace-the-text-with-open-xml?forum=oxmlsdk
http://social.msdn.microsoft.com/Forums/office/en-US/98d89ece-bd9b-413f-8782-6ea3fc4d8b01/how-to-delete-data-or-table-within-two-bookmarks-from-word-document-using-openxml-in-c?forum=oxmlsdk
http://social.msdn.microsoft.com/Forums/office/en-US/0c41e9d5-d9d7-4719-a429-2ba03b56c37d/how-to-put-content-controls-on-word-bookmarks-ranges-in-word?forum=oxmlsdkThe details will depend very much on how the chart is "embedded" between the BookmarkStart and the BookmarkEnd. Since bookmarks can more or less start and stop anywhere, this affects how you address what is between them. (Content Controls are more structured, making this easier.) In your place, I'd examine the underlying document.xml in the Open XML SDK Productivity Tool in order to see whether the chart structures need to be addressed by Descendant or Sibling, as a start.
Cindy Meister, VSTO/Word MVP, my blog
- Marked as answer by 赵召 Tuesday, May 13, 2014 12:39 AM
Thursday, May 8, 2014 9:53 AM -
Hi,
You can refer to Brian Jones’s blog about how to access chart data in content control and add the chart data to the content control:
http://blogs.msdn.com/b/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx
#SdtBlock Class
http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.sdtblock(v=office.14).aspx
Regards,
MarvinWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by 赵召 Tuesday, May 13, 2014 12:39 AM
Monday, May 12, 2014 10:10 AM
All replies
-
What you'd need to do is first inspect the MainDocPart.Document for the REFERENCES to the Charts. This should give you an ID value that you can "look up" in order to access the corresponding ChartPart.
Here's the underlying XML for a chart in a Word document. The key is the c:chart element and its r:id attribute. This is stored in document.xml.rels: <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart" Target="charts/chart1.xml"/> and points to the chart file in the zip package.
<w:drawing> <wp:inline distR="0" distL="0" distB="0" distT="0"> <wp:extent cy="3200400" cx="5486400"/> <wp:effectExtent r="19050" b="19050" t="0" l="38100"/> <wp:docPr name="Chart 1" id="1"/> <wp:cNvGraphicFramePr/> <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"> <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart"> <c:chart xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId8" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"/> </a:graphicData> </a:graphic> </wp:inline> </w:drawing>
In the Open XML SDK a Chart is in the namespace DocumentFormat.OpenXml.Drawing.Charts. If you have a Chart object, then you can look up the Reference using Chart.ChartReference.Id. This will give you the Relationship ID value. So get the Document DESCENDENTS of type ChartReference...
Cindy Meister, VSTO/Word MVP, my blog
Tuesday, May 6, 2014 4:39 PM -
Thank you Cindy Meister !
In the XML you'd shown to me.The name is 'Chart 1' and r:id is "rld8" .Can I change the name or r:id in Word.exe?
Now my Program's workflow like that:
1.Our Customer will edit the Word document by Word.exe. And there will be 18 charts in the document.
2.My program will handle the document:Changing the data of all the charts.
The problem is I can't find the right chart from the 18 charts."Chart 1" and "rld8" are generated automatically,is it?
So can I have some better way to solve this problem?
赵召
Wednesday, May 7, 2014 8:55 AM -
Yes, Word automatically generates the information. However, if your code gets the Descendants they should be in the order the charts appear in the document.
Other than that, it will depend on whether the charts are being inserted as InlineShapes or Shapes (with text flow formatting). A Shape object can have a NAME and that name can be set - depending on the version of Word the user can do this or code is required.
If an InlineShape than it would be best to either bookmark the chart or put it in a content control. In either case, this is something you can target directly in the Open XML, then pick up the chart inside.
Cindy Meister, VSTO/Word MVP, my blog
- Proposed as answer by Marvin_Guo Monday, May 12, 2014 10:00 AM
Wednesday, May 7, 2014 3:52 PM -
I get it.
But I had tried to pick up the chart by bookmark.By using Microsoft.Office.Interap.Word,that is very easy.
But by using OpenXML ,I am not know how to pick up the chart by bookmark.Can you give me some briefly code? Thank you very much.
赵召
Thursday, May 8, 2014 2:04 AM -
I think the following discussions may help you
http://social.msdn.microsoft.com/Forums/office/en-US/8e177aef-a43f-496e-bcf9-3d67f564cbf9/find-all-bookmarks-and-add-text-to-them?forum=oxmlsdk
http://social.msdn.microsoft.com/Forums/office/en-US/80b4b560-ee22-4bdb-94e9-cfabdad701d4/bookmark-on-checkbox-and-replace-the-text-with-open-xml?forum=oxmlsdk
http://social.msdn.microsoft.com/Forums/office/en-US/98d89ece-bd9b-413f-8782-6ea3fc4d8b01/how-to-delete-data-or-table-within-two-bookmarks-from-word-document-using-openxml-in-c?forum=oxmlsdk
http://social.msdn.microsoft.com/Forums/office/en-US/0c41e9d5-d9d7-4719-a429-2ba03b56c37d/how-to-put-content-controls-on-word-bookmarks-ranges-in-word?forum=oxmlsdkThe details will depend very much on how the chart is "embedded" between the BookmarkStart and the BookmarkEnd. Since bookmarks can more or less start and stop anywhere, this affects how you address what is between them. (Content Controls are more structured, making this easier.) In your place, I'd examine the underlying document.xml in the Open XML SDK Productivity Tool in order to see whether the chart structures need to be addressed by Descendant or Sibling, as a start.
Cindy Meister, VSTO/Word MVP, my blog
- Marked as answer by 赵召 Tuesday, May 13, 2014 12:39 AM
Thursday, May 8, 2014 9:53 AM -
You are Right!
So how can I pick up charts by Content Controls?
赵召
Friday, May 9, 2014 7:11 AM -
Hi,
You can refer to Brian Jones’s blog about how to access chart data in content control and add the chart data to the content control:
http://blogs.msdn.com/b/brian_jones/archive/2009/03/13/importing-charts-from-spreadsheets-to-wordprocessing-documents.aspx
#SdtBlock Class
http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.sdtblock(v=office.14).aspx
Regards,
MarvinWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by 赵召 Tuesday, May 13, 2014 12:39 AM
Monday, May 12, 2014 10:10 AM -
I'm using OpenXML's UpdateFieldsOnOpen class to 'touch' the word file and force a chart refresh.
See code example here: https://stackoverflow.com/a/50722219/120638
- Edited by dkitchen Wednesday, June 6, 2018 1:57 PM
Wednesday, June 6, 2018 1:57 PM