codecamp

Apache POI Word - 边框

在本章中,您将学习如何使用Java编程将边框应用到段落。

应用边框

以下代码用于在文档中应用边框:

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 ApplyingBorder 
{
   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("At tutorialspoint.com, we strive hard to " +
   "provide quality tutorials for self-learning " +
   "purpose in the domains of Academics, Information " +
   "Technology, Management and Computer Programming " +
   "Languages.");
        
   document.write(out);
   out.close();
   System.out.println("applyingborder.docx written successully");
   }
}

将上述代码保存在名为 ApplyingBorder.java的文件中,从命令提示符处编译并执行它,如下所示:

$javac ApplyingBorder.java
$java ApplyingBorder

如果系统配置了POI库,那么它将编译并执行,以在当前目录中生成名为 appliedborder.docx 的Word文档,并显示以下输出:

applyingborder.docx written successfully

appliedborder.docx 文件如下所示:


Apache POI Word - 段落
Apache POI Word - 表格
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }