poi生成word文件

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

一、简介

对于poi来说,poi可以完成对Word、Excel、PPT的处理。word目前有两种文件格式,一种是doc后缀、另一种是docx后缀的。2007之前的版本都是doc后缀的,这种格式poi使用HWPF进行处理。HWPF也能有限多地对旧的word6和word95格式的文件处理提供支持。2007(包括)之后都是docx后缀的,poi使用XWPF进行处理。HWPF 和 XWPF的特性有些相似,但是目前两者没有共用接口。

HWPF和XWPF可以被描述为“适度功能”。对于一些例子来说,管理文本的提取,提供强有力的支持。对于其他莱斯,支持是有限或不完整的,需要深入研究低级别的代码。错误检测已经被移除,所有可能会创建格式错误的文件。

HWPF 包含在poi-scratchpad-XXX.jar包中,而XWPF 包含在 poi-ooxml-XXX.jar包中。我们可以根据我们的需要来将这些包添加都classpath里面。HWPF与XWPF网址为:http://poi.apache.org/document/index.html 。

二、实例

1、依赖如下:

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.12</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.12</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.12</version></dependency>

2、示例代码如下:

1packagecom.test.word;23importjava.io.FileOutputStream;4importjava.io.IOException;5importjava.math.BigInteger;6importjava.util.List;78importorg.apache.poi.xwpf.usermodel.ParagraphAlignment;9importorg.apache.poi.xwpf.usermodel.TextAlignment;10importorg.apache.poi.xwpf.usermodel.XWPFDocument;11importorg.apache.poi.xwpf.usermodel.XWPFParagraph;12importorg.apache.poi.xwpf.usermodel.XWPFRun;13importorg.apache.poi.xwpf.usermodel.XWPFTable;14importorg.apache.poi.xwpf.usermodel.XWPFTableCell;15importorg.junit.Test;16importorg.openxmlformats.schemas.wordPRocessingml.x2006.main.CTTblPr;17importorg.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;1819/**20* 创建word文档21*/22publicclassWordCreate {23/**24* 2007word文档创建25*/26@Test27publicvoidcreateWord2007() {28XWPFDocument doc =newXWPFDocument();29XWPFParagraph p1 =doc.createParagraph();3031XWPFTable table = doc.createTable(11, 4);32//CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();33CTTblPr tblPr =table.getCTTbl().getTblPr();34tblPr.getTblW().setType(STTblWidth.DXA);35tblPr.getTblW().setW(newBigInteger("7000"));3637//设置上下左右四个方向的距离,可以将表格撑大38table.setCellMargins(20, 20, 20, 20);3940//表格41List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();4243XWPFTableCell cell = tableCells.get(0);44XWPFParagraph newPara =newXWPFParagraph(cell.getCTTc().addNewP(), cell);45XWPFRun run =newPara.createRun();46/**内容居中显示 **/47newPara.setAlignment(ParagraphAlignment.CENTER);48//run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000红色*/49//run.setUnderline(UnderlinePatterns.THICK);50run.setText("第一 数据");5152tableCells.get(1).setText("第一 数据");53tableCells.get(2).setText("第一 据");54tableCells.get(3).setText("第 据");5556tableCells = table.getRow(1).getTableCells();57tableCells.get(0).setText("第数据");58tableCells.get(1).setText("第一 数据");59tableCells.get(2).setText("第一 据");60tableCells.get(3).setText("第 据");6162//设置字体对齐方式63p1.setAlignment(ParagraphAlignment.CENTER);64p1.setVerticalAlignment(TextAlignment.TOP);6566//第一页要使用p1所定义的属性67XWPFRun r1 =p1.createRun();6869//设置字体是否加粗70r1.setBold(true);71r1.setFontSize(20);7273//设置使用何种字体74r1.setFontFamily("Courier");7576//设置上下两行之间的间距77r1.setTextPosition(20);78r1.setText("标题");7980FileOutputStream out;81try{82out =newFileOutputStream("c:/test/word2007.docx");83//以下代码可进行文件下载84//response.reset();85//response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");86//response.setHeader("Content-Disposition",87//"attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));88//OutputStream out = response.getOutputStream();89//this.doc.write(out);90//out.flush();9192doc.write(out);93out.close();94}catch(IOException e) {95e.printStackTrace();96}97System.out.println("success");98}99100}

3、生成word如下所示

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航