簡體   English   中英

c# openXML 向表 w:instrText 添加交叉引用

[英]c# openXML add cross-reference to a table w:instrText

我正在嘗試為表格添加標題,但在任何其他帖子中都找不到我要查找的內容。

這是我試圖生成的 XML。

    <w:p>
        <w:r>
            <w:fldChar w:fldCharType="begin" />
        </w:r>
        <w:r>
            <w:instrText xml:space="preserve"> REF TableRef \h </w:instrText>
        </w:r>
        <w:r>
            <w:fldChar w:fldCharType="separate" />
        </w:r>
        <w:r>
            <w:t xml:space="preserve">Table </w:t>
            </w:r>
            <w:fldSimple w:instr="SEQ Table">
                <w:r>
                    <w:rPr>
                        <w:noProof />
                    </w:rPr>
                    <w:t xml:space="preserve"> </w:t>
                </w:r>
            </w:fldSimple>
            <w:r>
            <w:fldChar w:fldCharType="end" />
        </w:r>
    </w:p>

我不知道如何獲得 instrText 部分。

        <w:r>
            <w:instrText xml:space="preserve"> REF TableRef \h </w:instrText>
        </w:r>

這是 90% 的代碼,誰能幫我填寫一下待辦事項。

      DocumentFormat.OpenXml.Wordprocessing.Run run2 = new DocumentFormat.OpenXml.Wordprocessing.Run(new Text() { Text = "Table ", Space = SpaceProcessingModeValues.Preserve });
      SimpleField simpleField2 = new SimpleField(new DocumentFormat.OpenXml.Wordprocessing.Run(new RunProperties(new NoProof()), new Text() { Text = " ", Space = SpaceProcessingModeValues.Preserve }));
      simpleField2.Instruction = @"SEQ " + "Table";

      DocumentFormat.OpenXml.Wordprocessing.Paragraph refP = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
        new DocumentFormat.OpenXml.Wordprocessing.Run(new FieldChar() { FieldCharType = FieldCharValues.Begin }),
        //todo instrTxt bookmark ref
        new DocumentFormat.OpenXml.Wordprocessing.Run(new FieldChar() { FieldCharType = FieldCharValues.Separate }),
        run2,
        simpleField2,
        new DocumentFormat.OpenXml.Wordprocessing.Run(new FieldChar() { FieldCharType = FieldCharValues.End })

        );

謝謝

使用示例文檔和 Open XML SDK 附帶的文檔反射器可以輕松解決諸如編寫正確的 C# 代碼以生成特定的 Open XML 文檔之類的問題:

  1. 下載並安裝最新版本的 Open XML SDK ( OpenXMLSDKToolV25.msi )
  2. 打開位於C:\\Program Files (x86)\\Open XML SDK\\V2.5\\tool (安裝到默認位置時)的Open XML SDK Productivity Tool ( OpenXmlSdkTool.exe )。
  3. 創建具有所需格式/內容的示例文檔,然后在工具中打開示例文檔。
  4. 單擊反映代碼
  5. 將生成的代碼復制到您的項目並在需要的地方進行調整,例如用您的內容替換靜態文本。

對於其他人在幾年后絆倒這個問題。 <w:instrText>對應的 OpenXml 類是DocumentFormat.OpenXml.Wordprocessing.FieldCode

因此,要回答原始問題,請使用以下行:

//todo instrTxt bookmark ref

應改為:

new DocumentFormat.OpenXml.Wordprocessing.Run(new FieldCode(" REF TableRef \\h ") { Space = SpaceProcessingModeValues.Preserve }),

其中TableRef是書簽的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM