Poi generates word files, and poi generates word files.

Source: Internet
Author: User

Poi generates word files, and poi generates word files.

I. Introduction
For poi, You can process word, excel, and ppt. Word currently has two file formats: doc suffix and docx suffix. Versions earlier than 2007 are suffixed with doc. This format of poi uses HWPF for processing. HWPF can also provide limited support for file processing in the old word6 and word95 formats. 2007 (including) is followed by the docx suffix, and poi uses XWPF for processing. HWPF and XWPF have similar features, but currently they do not share interfaces.
HWPF and XWPF can be described as "moderate functionality ". For some examples, the management of Text Extraction provides strong support. For other LEs, the support is limited or incomplete and requires in-depth research on low-level code. Error detection has been removed. All files with incorrect format may be created.
HWPF is included in the poi-scratchpad-XXX.jar package, while XWPF is included in the poi-ooxml-XXX.jar package. We can add these packages to classpath as needed. HWPF and XWPF Web site: http://poi.apache.org/document/index.html.

Ii. Instances

1. dependencies are as follows:

<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. The sample code is as follows:

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 * Create a Word document 21 */22 public class WordCreate {23/** 24 * 2007word document create 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 // you can set the distance between the top, bottom, and left directions. setCellMargins (20, 20, 20, 20); 39 40 // table 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/** center content display **/47 newPara. setAlignment (ParagraphAlignment. CENTER); 48 // run. getCTR (). addNewRPr (). addNewColor (). setVal ("FF0000");/** FF0000 red */49 // run. setUnderline (UnderlinePatterns. THICK); 50 run. setText ("First Data"); 51 52 tableCells. get (1 ). setText ("First Data"); 53 tableCells. get (2 ). setText ("First Data"); 54 tableCells. get (3 ). setText ("data"); 55 56 tableCells = table. getRow (1 ). getTableCells (); 57 tableCells. get (0 ). setText ("data"); 58 tableCells. get (1 ). setText ("First Data"); 59 tableCells. get (2 ). setText ("First Data"); 60 tableCells. get (3 ). setText ("data"); 61 62 // sets the font alignment 63 p1.setAlignment (ParagraphAlignment. CENTER); 64 p1.setVerticalAlignment (TextAlignment. TOP); 65 66 // The first page should use the property 67 XWPFRun r1 = p1.createRun (); 68 69 // set whether the font is bold 70 r1.setBold (true ); 71 r1.setFontSize (20); 72 73 // set the font 74 r1.setFontFamily ("Courier"); 75 76 // set the spacing between the upper and lower rows to 77 r1.setTextPosition (20 ); 78 r1.setText ("title"); 79 80 FileOutputStream out; 81 try {82 out = new FileOutputStream ("c:/test/word2007.docx "); 83 // the following code downloads the file 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. Generate the word as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.