麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

poi生成word文件

2019-11-14 15:39:39
字體:
供稿:網(wǎng)友

一、簡介
  對于poi來說,poi可以完成對WordExcelPPT的處理。word目前有兩種文件格式,一種是doc后綴、另一種是docx后綴的。2007之前的版本都是doc后綴的,這種格式poi使用HWPF進行處理。HWPF也能有限多地對舊的word6和word95格式的文件處理提供支持。2007(包括)之后都是docx后綴的,poi使用XWPF進行處理。HWPF 和 XWPF的特性有些相似,但是目前兩者沒有共用接口。
  HWPF和XWPF可以被描述為“適度功能”。對于一些例子來說,管理文本的提取,提供強有力的支持。對于其他萊斯,支持是有限或不完整的,需要深入研究低級別的代碼。錯誤檢測已經(jīng)被移除,所有可能會創(chuàng)建格式錯誤的文件。
  HWPF 包含在poi-scratchpad-XXX.jar包中,而XWPF 包含在 poi-ooxml-XXX.jar包中。我們可以根據(jù)我們的需要來將這些包添加都classpath里面。HWPF與XWPF網(wǎng)址為: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、示例代碼如下:

  1 package com.test.word;  2   3 import java.io.FileOutputStream;  4 import java.io.IOException;  5 import java.math.BigInteger;  6 import java.util.List;  7   8 import org.apache.poi.xwpf.usermodel.ParagraphAlignment;  9 import org.apache.poi.xwpf.usermodel.TextAlignment; 10 import org.apache.poi.xwpf.usermodel.XWPFDocument; 11 import org.apache.poi.xwpf.usermodel.XWPFParagraph; 12 import org.apache.poi.xwpf.usermodel.XWPFRun; 13 import org.apache.poi.xwpf.usermodel.XWPFTable; 14 import org.apache.poi.xwpf.usermodel.XWPFTableCell; 15 import org.junit.Test; 16 import org.openxmlformats.schemas.wordPRocessingml.x2006.main.CTTblPr; 17 import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; 18  19 /** 20  * 創(chuàng)建word文檔 21  */ 22 public class WordCreate {  23     /** 24      * 2007word文檔創(chuàng)建 25      */ 26     @Test 27     public void createWord2007() { 28         XWPFDocument doc = new XWPFDocument(); 29         XWPFParagraph p1 = doc.createParagraph(); 30  31         XWPFTable table = doc.createTable(11, 4); 32         // CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders(); 33         CTTblPr tblPr = table.getCTTbl().getTblPr(); 34         tblPr.getTblW().setType(STTblWidth.DXA); 35         tblPr.getTblW().setW(new BigInteger("7000")); 36  37         // 設(shè)置上下左右四個方向的距離,可以將表格撐大 38         table.setCellMargins(20, 20, 20, 20); 39  40         // 表格 41         List<XWPFTableCell> tableCells = table.getRow(0).getTableCells(); 42  43         XWPFTableCell cell = tableCells.get(0); 44         XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(), cell); 45         XWPFRun run = newPara.createRun(); 46         /** 內(nèi)容居中顯示 **/ 47         newPara.setAlignment(ParagraphAlignment.CENTER); 48         // run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000紅色*/ 49         // run.setUnderline(UnderlinePatterns.THICK); 50         run.setText("第一 數(shù)據(jù)"); 51  52         tableCells.get(1).setText("第一 數(shù)據(jù)"); 53         tableCells.get(2).setText("第一 據(jù)"); 54         tableCells.get(3).setText("第 據(jù)"); 55  56         tableCells = table.getRow(1).getTableCells(); 57         tableCells.get(0).setText("第數(shù)據(jù)"); 58         tableCells.get(1).setText("第一 數(shù)據(jù)"); 59         tableCells.get(2).setText("第一 據(jù)"); 60         tableCells.get(3).setText("第 據(jù)"); 61  62         // 設(shè)置字體對齊方式 63         p1.setAlignment(ParagraphAlignment.CENTER); 64         p1.setVerticalAlignment(TextAlignment.TOP); 65  66         // 第一頁要使用p1所定義的屬性 67         XWPFRun r1 = p1.createRun(); 68  69         // 設(shè)置字體是否加粗 70         r1.setBold(true); 71         r1.setFontSize(20); 72  73         // 設(shè)置使用何種字體 74         r1.setFontFamily("Courier"); 75  76         // 設(shè)置上下兩行之間的間距 77         r1.setTextPosition(20); 78         r1.setText("標(biāo)題"); 79  80         FileOutputStream out; 81         try { 82             out = new FileOutputStream("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(); 91  92             doc.write(out); 93             out.close(); 94         } catch (IOException e) { 95             e.printStackTrace(); 96         } 97         System.out.println("success"); 98     } 99 100 }

  3、生成word如下所示


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 亚洲成人入口 | 日本黄色一级视频 | 久久人人爽人人爽人人片av高清 | 免费观看视频在线 | 亚洲国产精品一区二区精品 | 最新中文字幕日本 | 大片毛片 | 国产精品久久久久久久四虎电影 | 欧美a视频在线观看 | 毛片免费一区二区三区 | 欧洲精品久久 | 国产一级毛片国产 | 西川av在线一区二区三区 | 欧美一级电影网站 | 国产一级二级在线播放 | 天天看逼 | 91成人一区二区三区 | 精品国产乱码久久久久久久久 | 操碰97| 久久精品视频日本 | 国产激情视频在线 | 日本欧美一区二区三区在线观看 | 久久久国产视频 | 国产91丝袜在线播放0 | fc2国产成人免费视频 | 国产色爱综合网 | 欧美性受xxxx白人性爽 | 日本精品视频一区二区三区四区 | 国产二区三区视频 | 日本中文高清 | 羞羞漫画无遮挡观看 | 一级性生活免费视频 | 一区国产精品 | 久久久久国 | 一级视频在线播放 | 免费试看av | 一级毛片在线免费观看 | 国产亚洲欧美日韩在线观看不卡 | 手机视频在线播放 | 成人不卡一区二区 | 久久综合精品视频 |