在Java编程中,如何对Word文档中的文本应用边框?
注意:需要访问网址:http://poi.apache.org/download.html , 下载一个Apache POI软件包。这里下载最新版本:poi-bin-3.17-20170915.tar.gz解压并将全部.jar文件导入。
需要导入全部包,如下图所示 -
以下是如何使用Java对Word文档中的文本应用边框的程序。
package com.yiibai;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class BordersToText {
public static void main(String[] args)throws Exception {
//Blank Document
XWPFDocument document = new XWPFDocument();
//Write the Document in file system
FileOutputStream out = new FileOutputStream(
new File("applyingborder.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set bottom border to paragraph
paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
//Set left border to paragraph
paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
//Set right border to paragraph
paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
//Set top border to paragraph
paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
XWPFRun run = paragraph.createRun();
run.setText("易百教程(www.yiibai.com)是因特网上最大的 IT技术学习和开发者资源,其中包括全面的教程、完善的参考手册以及庞大的代码库。易百教程每月接受成千上万的用户访问,并产生几十万以上的页面浏览量。"
);
document.write(out);
out.close();
System.out.println("applyingborder.docx written successully");
}
}
执行上面示例代码,得到以下结果 -
applyingborder.docx written successully
生成的World文档,得到以下结果 -