POI XWPF在书签中插入来自另一个文档的内容



APACHE POI XWPF问题:

.docx主文档中,我有一个带有书签的单元格:

<w:bookmarkStart w:id="8" w:name="PAR_1"/> 
<w:r>
<w:rPr>
<w:rFonts w:cs="Arial"/>
<w:lang w:val="fr-FR"/>
</w:rPr>
<w:t>[[DOCX_CONTENT]]</w:t>
</w:r>
<w:bookmarkEnd w:id="8"/>

我需要将字符串[[DOCX_CONTENT]](位于bookmarkStart和bookmarkEnd之间(替换为外部.DOCX.的内容

external.docx还包含以下引用:

<w:r w:rsidR="00E520CA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
<w:instrText xml:space="preserve"> REF * CHARFORMAT refName * MERGEFORMAT</w:instrText>
</w:r>

我尝试的所有解决方案只复制外部.docx文本,但不复制引用

这是一个外部.docx的示例,我需要在我的主文档的bookmarkStart和bookmarkEnd之间包含:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex"
xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex"
xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex"
xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex"
xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex"
xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex"
xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex"
xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex"
xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink"
xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml"
xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex"
xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid"
xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml"
xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash"
xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex"
xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup"
xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk"
xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"
xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape"
mc:Ignorable="w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14">
<w:body>
<w:p w14:paraId="075E9F39"
w14:textId="6116E8CC"
w:rsidR="00E520CA"
w:rsidRDefault="00516D45"
w:rsidP="00516D45">
<w:pPr>
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
</w:pPr>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>bancadatilegali</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r>
<w:t xml:space="preserve">: </w:t>
</w:r>

<w:bookmarkStart w:id="0"
w:name="Cess_CR_Art1"/>

<w:r w:rsidR="00E520CA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
<w:fldChar w:fldCharType="begin"/>
</w:r>

<w:r w:rsidR="00E520CA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
<w:instrText xml:space="preserve"> REF * CHARFORMAT refName * MERGEFORMAT </w:instrText>
</w:r>
<w:r w:rsidR="00E520CA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
<w:fldChar w:fldCharType="separate"/>
</w:r>

<w:r w:rsidR="00E520CA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
<w:b/>
<w:bCs/>
</w:rPr>
<w:t>Errore. origine riferimento non è stata trovata.</w:t>
</w:r>
<w:r w:rsidR="00E520CA">
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
<w:fldChar w:fldCharType="end"/>
</w:r>
</w:p>
<w:p w14:paraId="60D800C9"
w14:textId="77777777"
w:rsidR="00E520CA"
w:rsidRPr="00273050"
w:rsidRDefault="00E520CA"
w:rsidP="00273050">
<w:pPr>
<w:tabs>
<w:tab w:val="left"
w:pos="0"/>
</w:tabs>
<w:spacing w:line="240"
w:lineRule="atLeast"/>
<w:rPr>
<w:rFonts w:cs="Arial"/>
</w:rPr>
</w:pPr>
</w:p>
<w:bookmarkEnd w:id="0"/>

<w:p w14:paraId="3DB7024E"
w14:textId="4274EB2A"
w:rsidR="000D5B22"
w:rsidRPr="00273050"
w:rsidRDefault="000D5B22"
w:rsidP="00273050"/>
<w:sectPr w:rsidR="000D5B22"
w:rsidRPr="00273050">
<w:pgSz w:w="11906"
w:h="16838"/>
<w:pgMar w:top="1417"
w:right="1134"
w:bottom="1134"
w:left="1134"
w:header="708"
w:footer="708"
w:gutter="0"/>
<w:cols w:space="708"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>

谢谢你的帮助。

我修改为按照建议使用XML和外部par的所有内容(包括参考资料和格式(。

但仍然存在一个问题:复制的部分在我的书签之前

...
...
XWPFDocument  mainDoc = new XWPFDocument (new FileInputStream(pathMainDoc));
//get cell with bookmark
XWPFTable table = doc.getTables().get(0);
XWPFTableRow row = griglia.getRow(rowNum);
XWPFTableCell cell = row.getCell(cellNum);
//here is contained the bookmark:
XWPFParagraph cellPar = cell.getParagraphs().get(0);
XWPFDocument externalDoc= new XWPFDocument (new FileInputStream(pathExternalDoc));
for (XWPFParagraph externalPar : externalDoc.getParagraphs()) {
XWPFParagraph newPar = cell.addParagraph();
newPar.getCTPPr().addNewRPr().set(externalPar.getCTP());
} //for pars

XWPFParagraph newPar = cell.addParagraph();在书签之前的单元格开始处创建一个新的par。

如何在书签中创建新的par

相关内容

最新更新