簡體   English   中英

使用Apache-POI進行Word自動格式化

[英]Word autoformat with Apache-POI

我想在XWPFDocument中將word的自動格式化功能與Apache-POI一起使用。

通過自動套用格式,我的意思是,如果您鍵入“ ---”並按回車鍵,則會在word文檔的頁面上繪制一條水平線。

我想在標題中使用它。

我試過了

XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.LEFT);
run = paragraph.createRun();
run.setText("---\r");

要么

run.setText("---\r\n");

要么

run.setText("---");
run.addCarriageReturn();

這些都不起作用。

POI甚至可以使用自動格式化功能嗎?

問候,邁克

我正在使用POI 4.0.0,順便說一句...

自動套用格式是Word的GUI的功能。 但是apache poi正在創建存儲在*.docx文件中的內容。 在自動套用格式將“ ---” Enter替換為段落的底部邊框線之后,僅該段落的底部邊框線存儲在文件中。

所以:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.wp.usermodel.HeaderFooterType;

public class CreateWordHeader {

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

  XWPFDocument doc = new XWPFDocument();

  // the body content
  XWPFParagraph paragraph = doc.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("The Body...");

  // create header
  XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
  paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);
  run = paragraph.createRun();
  run.setText("First Line in Header...");

  // bottom border line of the paragraph = what Autoformat creates after "---"[Enter]
  paragraph.setBorderBottom(Borders.SINGLE);

  paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.LEFT);
  run = paragraph.createRun();
  run.setText("Next Line in Header...");

  FileOutputStream out = new FileOutputStream("CreateWordHeader.docx");
  doc.write(out);
  doc.close();
  out.close();


 }
}

暫無
暫無

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

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