XWPF Documentation (POI API Documentation) (apache.org)

Rely on

<! -- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<! -- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
Copy the code

XWPFDocument class

1. Construction method

  • XWPFDocument(java.io.InputStream is)Input stream read file open
XWPFDocument document = new XWPFDocument(new FileInputStream(path));
Copy the code
  • XWPFDocument(OPCPackage pkg)Open the document
XWPFDocument document = new XWPFDocument(XWPFDocument.openPackage("C:\Users\admin\Desktop\0.docx"));
Copy the code

Note: The difference between the two modes of file flow opening

New XWPFDocument(new FileInputStream(path)) open operation Word does not modify the original template output operation. New XWPFDocument(xwpfDocument.openPackage ("C:\Users\admin\Desktop\0.docx"))) The original Word template will be modifiedCopy the code

Commonly used 2.

  • createParagraph()Create a paragraph
  • createTable()Create an empty table with one row and one column default values. You can also specify rows and columns
  • createStyles()If the document does not already exist, create an empty style
  • getParagraphs()Returns the paragraph that holds header or footer text.
  • insertNewParagraph(org.apache.xmlbeans.XmlCursor cursor)Add a new paragraph at the cursor position. Cursor = cursor
  • removeBodyElement(int pos)Remove the body element from the Word document’s Body element array list (top to bottom)
  • write(java.io.OutputStream out)Write files

Examples of the demo

    public class Test004 {
        public static void main(String[] args) throws IOException, OpenXML4JException, XmlException {

            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\Users\admin\Desktop\0.docx"));

            XWPFParagraph paragraph = document.createParagraph();
            // Get all paragraphs
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            for (int i = 0; i < paragraphs.size(); i++) {
                XWPFParagraph paragraph1 = paragraphs.get(i);
                if (paragraph1.getText().equals("The beginning")) {// Append a new run to this section
                    XWPFRun r1=paragraph.createRun();
                    r1.addCarriageReturn();
    // Get the current cursor position
                    XmlCursor cursor = paragraph1.getCTP().newCursor();
                    cursor.toNextSibling();
    // Insert new paragraph at cursor position
                    XWPFParagraph newParagraph = document.insertNewParagraph(cursor);
                    XWPFRun run = newParagraph.createRun();
                    run.getCTR().addNewRPr().addNewHighlight().setVal(STHighlightColor.LIGHT_GRAY);
                    String s= "";

                    String s1 = "Insert current location !!!!";
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int j = 0; j < 64-s1.length(); j++) {
                        stringBuilder.append("");
                    }
                    run.setText(s1+stringBuilder);


                    document.removeBodyElement(document.getPosOfParagraph(paragraph1));
                }

                System.out.println("Dead loop"+i);
            }


            document.write(new FileOutputStream("C:\Users\admin\Desktop\001.docx")); document.close(); }}Copy the code
The effectCopy the code