0

How to change the content of the table in the Text box of docX file?When xmlcursor.getobject () instanceof XmlAnyTypeImpl is instanceof XmlAnyTypeImpl, I cannot change the contents of the table in the text box. How can I change the contents of the table in the text box?Here's my code, but he can't change it properly。


import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;

import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
import org.apache.xmlbeans.XmlCursor;

import javax.xml.namespace.QName;

import java.util.List;
import java.util.ArrayList;

public class WordReadAllTables {

    public static void main(String[] args) throws Exception {
        XWPFDocument document = new XWPFDocument(new FileInputStream(System.getProperty("user.dir") + "\\template\\" + "test.docx"));
        CTBody ctbody = document.getDocument().getBody();
        XmlCursor xmlcursor = ctbody.newCursor();
        QName qnameTbl = new QName("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "tbl", "w");
        QName qnameFallback = new QName("http://schemas.openxmlformats.org/markup-compatibility/2006", "Fallback", "mc");
        List<CTTbl> allCTTbls = new ArrayList<CTTbl>();
        while (xmlcursor.hasNextToken()) {
            XmlCursor.TokenType tokentype = xmlcursor.toNextToken();
            if (tokentype.isStart()) {
                if (qnameTbl.equals(xmlcursor.getName())) {
                    if (xmlcursor.getObject() instanceof CTTbl) {
                        allCTTbls.add((CTTbl) xmlcursor.getObject());
                    } else if (xmlcursor.getObject() instanceof XmlAnyTypeImpl) {
                        allCTTbls.add(CTTbl.Factory.parse(xmlcursor.getObject().toString()));
                    }
                } else if (qnameFallback.equals(xmlcursor.getName())) {
                    xmlcursor.toEndToken();
                }
            }
        }

        for (CTTbl cTTbl : allCTTbls) {
            StringBuffer tableHTML = new StringBuffer();
            tableHTML.append("<table>\n");
            for (CTRow cTRow : cTTbl.getTrList()) {
                for (CTTc cTTc : cTRow.getTcList()) {
                    for (CTP cTP : cTTc.getPList()) {
                        for (CTR cTR : cTP.getRList()) {
                            for (CTText cTText : cTR.getTList()) {
                                tableHTML.append(cTText.getStringValue() + "-");
                                if (cTText.getStringValue().contains("select")) {
                                    cTText.setStringValue("1111");
                                }
                            }
                        }
                    }
                }
            }
        }

        FileOutputStream stream = new FileOutputStream(new File("D://aa.docx"));
        document.write(stream);
        stream.close();
        document.close();

    }
}
  • 1
    This seems to be my code from https://stackoverflow.com/questions/46054823/apache-poi-get-table-from-text-box/46059749#46059749. But that is to **read** tables only. You cannot expect that changes of the any-type-XML will be written back into the document. Btw.: `CTTbl.Factory.parse(xmlcursor.getObject().toString())` should better be `CTTbl.Factory.parse(xmlcursor.getObject().newInputStream())`. I have changed this in my answer. – Axel Richter Apr 20 '22 at 06:08
  • Thank you for your advice. Do you know how to change the contents of the table in the text box? – 大驴大驴 Apr 20 '22 at 07:45
  • Not that general. What is needed is to get the text into `XWPFRun` elements to make sure they are written while `XWPFDocument.write`. Like in https://stackoverflow.com/questions/46802369/replace-text-in-text-box-of-docx-by-using-apache-poi/46894499#46894499. `cursor.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//*/w:txbxContent//*/w:p/w:r");` should catch the text runs in a table. But the text box would must be in default text body. If it is in a content control (SDT) then it becomes much more complex. – Axel Richter Apr 20 '22 at 11:22

0 Answers0