OpenXML C#:无法获取图表中嵌入的矩形(Word-doc)

问题描述 投票:0回答:0

我有一个带有图表的Word文档。该图表有两个矩形,我想让 txBody 更改文本。

我所知道的:

  • 图表有 rId17
  • Uri 是 /word/charts/chart2.xml (据我所知,DrawingML)
  • Uri og ChartDrawingPart 是 /word/drawings/drawing1.xml
  • drawing1.xml 有我需要的 txBody:
<cdr:txBody>
  <a:bodyPr xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" vertOverflow="clip" lIns="54000" rIns="54000"/>
  <a:lstStyle xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
    <a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
        <a:pPr algn="ctr"/>
        <a:r>
          <a:rPr lang="da-DK"/>
          **<a:t>BB</a:t>**
        </a:r>
    </a:p>
</cdr:txBody>
  • 如果我能以某种方式通过 DocumentFormat.OpenXml (C#) 掌握它,我应该能够更改文本

使用 C# 和 DocumentFormat.OpenXml 我尝试了很多不同的方法: (我设法更改文档中的段落、内容控件、图形/图表和表格,但未更改给定图表中的矩形)

(doc是WordprocessingDocument)

`doc.MainDocumentPart?.ChartParts;'

-> 迭代chartSpaceChildElem 我可以找到图表(chart2.xml)-(我可以更改数值,但找不到矩形) 据我所知,我正在处理chart2.xml,但我需要处理drawing1.xml

我尝试过:

doc.MainDocumentPart.Document.Descendants<Drawing>()
doc.MainDocumentPart.Document.Descendants<UserShapes>()
doc.MainDocumentPart.Document.Descendants<DocumentFormat.OpenXml.Drawing.Shape>()
doc.MainDocumentPart.Document.Descendants<Chart>()
doc.MainDocumentPart.Document.Descendants<ExternalData>()
doc.MainDocumentPart?.ImageParts
doc.MainDocumentPart?.DiagramDataParts
以及诸如
chartSpaceChildElem.Descendants<DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing.TxPrTextBody>()

之类的东西

这些都没有给我带来我想要的。

我想我可以获取MainDocumentPart(/word下)中的所有元素。 IE。文档、注释、脚注等。但 Drawing1.xml 位于 /word/drawings/ 下,我无法访问它?

有人可以告诉我如何使用 DocumentFormat.OpenXml 和 C# 来获取它吗? (我不想流式传输 xml。因为这需要我解压缩 word-doc)

c# ms-word openxml-sdk
© www.soinside.com 2019 - 2024. All rights reserved.