簡體   English   中英

如何使用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}");
    }

}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM