Java POI 生成Word文档

在开发中有时候我们需要导出MS word文档。最近因为需要做一个生成word文件的功能。就将这块拿出来和大家分享。

      生成word文件和我们写word文档是相同的概念,只不过在这里我们换成了用代码来操作。下面的例子中主要有添加页眉,页脚,正文(段落,表格)。在正文中,段落包含文字字体和背景的设置。表格主要是数据的填充和样式(有无边框)。这里写的例子给出的内容只是Java POI 方式生成word文件的极少数的一些方法,需要使用更多方法的还是要自己根据自己的需求去查看API。

那就直接先上代码吧:

[java]  view plain  copy
  1. package com.seawater.controller;  
  2.   
  3. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;  
  4. import org.apache.poi.xwpf.usermodel.*;  
  5. import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;  
  6.   
  7. import java.io.File;  
  8. import java.io.FileOutputStream;  
  9. import java.math.BigInteger;  
  10.   
  11.   
  12. /** 
  13.  * Created by zhouhs on 2017/1/9. 
  14.  */  
  15. public class WordExportController {  
  16.   
  17.     public static void main(String[] args)throws Exception {  
  18.         //Blank Document  
  19.         XWPFDocument document= new XWPFDocument();  
  20.   
  21.         //Write the Document in file system  
  22.         FileOutputStream out = new FileOutputStream(new File("create_table.docx"));  
  23.   
  24.   
  25.         //添加标题  
  26.         XWPFParagraph titleParagraph = document.createParagraph();  
  27.         //设置段落居中  
  28.         titleParagraph.setAlignment(ParagraphAlignment.CENTER);  
  29.   
  30.         XWPFRun titleParagraphRun = titleParagraph.createRun();  
  31.         titleParagraphRun.setText("Java PoI");  
  32.         titleParagraphRun.setColor("000000");  
  33.         titleParagraphRun.setFontSize(20);  
  34.   
  35.   
  36.         //段落  
  37.         XWPFParagraph firstParagraph = document.createParagraph();  
  38.         XWPFRun run = firstParagraph.createRun();  
  39.         run.setText("Java POI 生成word文件。");  
  40.         run.setColor("696969");  
  41.         run.setFontSize(16);  
  42.   
  43.         //设置段落背景颜色  
  44.         CTShd cTShd = run.getCTR().addNewRPr().addNewShd();  
  45.         cTShd.setVal(STShd.CLEAR);  
  46.         cTShd.setFill("97FFFF");  
  47.   
  48.   
  49.         //换行  
  50.         XWPFParagraph paragraph1 = document.createParagraph();  
  51.         XWPFRun paragraphRun1 = paragraph1.createRun();  
  52.         paragraphRun1.setText("\r");  
  53.   
  54.   
  55.         //基本信息表格  
  56.         XWPFTable infoTable = document.createTable();  
  57.         //去表格边框  
  58.         infoTable.getCTTbl().getTblPr().unsetTblBorders();  
  59.   
  60.   
  61.         //列宽自动分割  
  62.         CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();  
  63.         infoTableWidth.setType(STTblWidth.DXA);  
  64.         infoTableWidth.setW(BigInteger.valueOf(9072));  
  65.   
  66.   
  67.         //表格第一行  
  68.         XWPFTableRow infoTableRowOne = infoTable.getRow(0);  
  69.         infoTableRowOne.getCell(0).setText("职位");  
  70.         infoTableRowOne.addNewTableCell().setText(": Java 开发工程师");  
  71.   
  72.         //表格第二行  
  73.         XWPFTableRow infoTableRowTwo = infoTable.createRow();  
  74.         infoTableRowTwo.getCell(0).setText("姓名");  
  75.         infoTableRowTwo.getCell(1).setText(": seawater");  
  76.   
  77.         //表格第三行  
  78.         XWPFTableRow infoTableRowThree = infoTable.createRow();  
  79.         infoTableRowThree.getCell(0).setText("生日");  
  80.         infoTableRowThree.getCell(1).setText(": xxx-xx-xx");  
  81.   
  82.         //表格第四行  
  83.         XWPFTableRow infoTableRowFour = infoTable.createRow();  
  84.         infoTableRowFour.getCell(0).setText("性别");  
  85.         infoTableRowFour.getCell(1).setText(": 男");  
  86.   
  87.         //表格第五行  
  88.         XWPFTableRow infoTableRowFive = infoTable.createRow();  
  89.         infoTableRowFive.getCell(0).setText("现居地");  
  90.         infoTableRowFive.getCell(1).setText(": xx");  
  91.   
  92.   
  93.         //两个表格之间加个换行  
  94.         XWPFParagraph paragraph = document.createParagraph();  
  95.         XWPFRun paragraphRun = paragraph.createRun();  
  96.         paragraphRun.setText("\r");  
  97.   
  98.   
  99.   
  100.         //工作经历表格  
  101.         XWPFTable ComTable = document.createTable();  
  102.   
  103.   
  104.         //列宽自动分割  
  105.         CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();  
  106.         comTableWidth.setType(STTblWidth.DXA);  
  107.         comTableWidth.setW(BigInteger.valueOf(9072));  
  108.   
  109.         //表格第一行  
  110.         XWPFTableRow comTableRowOne = ComTable.getRow(0);  
  111.         comTableRowOne.getCell(0).setText("开始时间");  
  112.         comTableRowOne.addNewTableCell().setText("结束时间");  
  113.         comTableRowOne.addNewTableCell().setText("公司名称");  
  114.         comTableRowOne.addNewTableCell().setText("title");  
  115.   
  116.         //表格第二行  
  117.         XWPFTableRow comTableRowTwo = ComTable.createRow();  
  118.         comTableRowTwo.getCell(0).setText("2016-09-06");  
  119.         comTableRowTwo.getCell(1).setText("至今");  
  120.         comTableRowTwo.getCell(2).setText("seawater");  
  121.         comTableRowTwo.getCell(3).setText("Java开发工程师");  
  122.   
  123.         //表格第三行  
  124.         XWPFTableRow comTableRowThree = ComTable.createRow();  
  125.         comTableRowThree.getCell(0).setText("2016-09-06");  
  126.         comTableRowThree.getCell(1).setText("至今");  
  127.         comTableRowThree.getCell(2).setText("seawater");  
  128.         comTableRowThree.getCell(3).setText("Java开发工程师");  
  129.   
  130.   
  131.         CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();  
  132.         XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);  
  133.   
  134.         //添加页眉  
  135.         CTP ctpHeader = CTP.Factory.newInstance();  
  136.         CTR ctrHeader = ctpHeader.addNewR();  
  137.         CTText ctHeader = ctrHeader.addNewT();  
  138.         String headerText = "Java POI create MS word file.";  
  139.         ctHeader.setStringValue(headerText);  
  140.         XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);  
  141.         //设置为右对齐  
  142.         headerParagraph.setAlignment(ParagraphAlignment.RIGHT);  
  143.         XWPFParagraph[] parsHeader = new XWPFParagraph[1];  
  144.         parsHeader[0] = headerParagraph;  
  145.         policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);  
  146.   
  147.   
  148.         //添加页脚  
  149.         CTP ctpFooter = CTP.Factory.newInstance();  
  150.         CTR ctrFooter = ctpFooter.addNewR();  
  151.         CTText ctFooter = ctrFooter.addNewT();  
  152.         String footerText = "http://blog.csdn.net/zhouseawater";  
  153.         ctFooter.setStringValue(footerText);  
  154.         XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);  
  155.         headerParagraph.setAlignment(ParagraphAlignment.CENTER);  
  156.         XWPFParagraph[] parsFooter = new XWPFParagraph[1];  
  157.         parsFooter[0] = footerParagraph;  
  158.         policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);  
  159.   
  160.   
  161.         document.write(out);  
  162.         out.close();  
  163.         System.out.println("create_table document written success.");  
  164.     }  
  165.   
  166.   
  167. }  
代码我放到这一个文件当中了。下面我就一些代码做一些解释,因为有的是我在做的过程中遇到的问题。大部分的代码大家都是一眼就可以看懂的。

[java]  view plain  copy
  1. //设置段落背景颜色  
  2.         CTShd cTShd = run.getCTR().addNewRPr().addNewShd();  
  3.         cTShd.setVal(STShd.CLEAR);  
  4.         cTShd.setFill("97FFFF");  
这段代码设置段落的背景颜色。

如果我们的表格不需要边框呢就加下面的代码:

[java]  view plain  copy
  1. infoTable.getCTTbl().getTblPr().unsetTblBorders();  
infoTable换成自己的table名称就可以了。
建立一个表格的时候设置列宽跟随内容伸缩

[java]  view plain  copy
  1. CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();  
  2.         infoTableWidth.setType(STTblWidth.DXA);  
  3.         infoTableWidth.setW(BigInteger.valueOf(9072));  
其他的代码我就不解释了。运行就可以得到我们的word文件了。

结果:

1-1

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值