如何使用 apache POI 在 docx 中插入当前日期字段

How to insert current date field in docx using apache POI

我不知道如何将日期和时间字段插入 docx。 我想我必须使用类似

run.getCTR().addNewFldChar().setFldCharType(STFldCharType.???) 

但我不知道怎么办。

Bellow 是一个 SSCCE,其中 insertCurrentXxxxField() 函数未按需要运行。

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class InsertCurrentDateAndTimeInDocxUsingApachePOI {

    public static void main(String[] args) throws IOException {

        XWPFDocument  document  = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun       run       = paragraph.createRun();

        run.setText("Current date:");
        insertCurrentDateField(run);

        run.setText(" current time:");
        insertCurrentTimeField(run);

        FileOutputStream out = new FileOutputStream(new File("CurrentDateAndTime.docx"));
        document.write(out);
    }

    private static void insertCurrentDateField(XWPFRun run){
        run.setText("${here should be the current date field DD.MM.YY}");
    }
    private static void insertCurrentTimeField(XWPFRun run){
        run.setText("${here should be the current time field HH:MM:SS}");
    }

}

您可以使用 java.util.Calendarjava.text.DateFormat 获取当前日期和时间。


示例代码:

    java.text.DateFormat dateFormat = new java.text.SimpleDateFormat("DD.MM.YY HH:mm:ss");
    java.text.DateFormat dateFormat1 = new java.text.SimpleDateFormat("DD.MM.YY");
    java.text.DateFormat dateFormat2 = new java.text.SimpleDateFormat("HH:mm:ss");
    java.util.Calendar cal = java.util.Calendar.getInstance();

    String currDateTime = dateFormat.format(cal.getTime()); 
    String currDate = dateFormat1.format(cal.getTime()); 
    String currTime = dateFormat2.format(cal.getTime()); 

    System.out.println(currDateTime);// e.g. 28.01.16 15:36:27
    System.out.println(currDate);// e.g. 28.01.16
    System.out.println(currTime);// e.g. 15:36:27

Word 中,字段在 Paragraph 中,而不是在 Run 中。但是Run必须在字段前关闭,并且新的Run必须在字段后打开。

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class InsertCurrentDateAndTimeInDocxUsingApachePOI {

    public static void main(String[] args) throws IOException {

        XWPFDocument  document  = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun       run       = paragraph.createRun();

        run.setText("Current date:");
        insertCurrentDateField(paragraph);

        run = paragraph.createRun();
        run.setText(" current time:");
        insertCurrentTimeField(paragraph);

        FileOutputStream out = new FileOutputStream(new File("CurrentDateAndTime.docx"));
        document.write(out);
    }

    private static void insertCurrentDateField(XWPFParagraph paragraph){
        XWPFRun run = paragraph.createRun(); 
        paragraph.getCTP().addNewFldSimple().setInstr("DATE \@ \"yyyy-MM-dd\" \* MERGEFORMAT");
    }
    private static void insertCurrentTimeField(XWPFParagraph paragraph){
        XWPFRun run = paragraph.createRun();
        paragraph.getCTP().addNewFldSimple().setInstr("TIME \@ \"HH:mm:ss\" \* MERGEFORMAT");
    }

}