How to change the content of the table in the Text box of docX file

311 views Asked by At

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();

    }
}
0

There are 0 answers