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



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

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

但我不知道怎么做。

波纹管是一个 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}");
    }
}

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");
    }
}
您可以使用

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

相关内容

  • 没有找到相关文章

最新更新