利用Java Apache POI 生成Word文档

来源:互联网 发布:js臀推 滑进去了 编辑:程序博客网 时间:2024/05/13 06:49

最近公司做的项目需要实现导出Word文档的功能,网上关于POI生成Word文档的例子很少,找了半天才在官网里找到个Demo,有了Demo一切就好办了。

[java] view plaincopy
  1. /* ==================================================================== 
  2.    Licensed to the Apache Software Foundation (ASF) under one or more 
  3.    contributor license agreements.  See the NOTICE file distributed with 
  4.    this work for additional information regarding copyright ownership. 
  5.    The ASF licenses this file to You under the Apache License, Version 2.0 
  6.    (the "License"); you may not use this file except in compliance with 
  7.    the License.  You may obtain a copy of the License at 
  8.  
  9.        http://www.apache.org/licenses/LICENSE-2.0 
  10.  
  11.    Unless required by applicable law or agreed to in writing, software 
  12.    distributed under the License is distributed on an "AS IS" BASIS, 
  13.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14.    See the License for the specific language governing permissions and 
  15.    limitations under the License. 
  16. ==================================================================== */  
  17. package org.apache.poi.xwpf.usermodel;  
  18.   
  19. import java.io.FileOutputStream;  
  20.   
  21. /** 
  22.  * A simple WOrdprocessingML document created by POI XWPF API 
  23.  * 
  24.  * @author Yegor Kozlov 
  25.  */  
  26. public class SimpleDocument {  
  27.   
  28.     public static void main(String[] args) throws Exception {  
  29.         XWPFDocument doc = new XWPFDocument();  
  30.   
  31.         XWPFParagraph p1 = doc.createParagraph();  
  32.         p1.setAlignment(ParagraphAlignment.CENTER);  
  33.         p1.setBorderBottom(Borders.DOUBLE);  
  34.         p1.setBorderTop(Borders.DOUBLE);  
  35.   
  36.         p1.setBorderRight(Borders.DOUBLE);  
  37.         p1.setBorderLeft(Borders.DOUBLE);  
  38.         p1.setBorderBetween(Borders.SINGLE);  
  39.   
  40.         p1.setVerticalAlignment(TextAlignment.TOP);  
  41.   
  42.         XWPFRun r1 = p1.createRun();  
  43.         r1.setBold(true);  
  44.         r1.setText("The quick brown fox");  
  45.         r1.setBold(true);  
  46.         r1.setFontFamily("Courier");  
  47.         r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);  
  48.         r1.setTextPosition(100);  
  49.   
  50.         XWPFParagraph p2 = doc.createParagraph();  
  51.         p2.setAlignment(ParagraphAlignment.RIGHT);  
  52.   
  53.         //BORDERS  
  54.         p2.setBorderBottom(Borders.DOUBLE);  
  55.         p2.setBorderTop(Borders.DOUBLE);  
  56.         p2.setBorderRight(Borders.DOUBLE);  
  57.         p2.setBorderLeft(Borders.DOUBLE);  
  58.         p2.setBorderBetween(Borders.SINGLE);  
  59.   
  60.         XWPFRun r2 = p2.createRun();  
  61.         r2.setText("jumped over the lazy dog");  
  62.         r2.setStrike(true);  
  63.         r2.setFontSize(20);  
  64.   
  65.         XWPFRun r3 = p2.createRun();  
  66.         r3.setText("and went away");  
  67.         r3.setStrike(true);  
  68.         r3.setFontSize(20);  
  69.         r3.setSubscript(VerticalAlign.SUPERSCRIPT);  
  70.   
  71.   
  72.         XWPFParagraph p3 = doc.createParagraph();  
  73.         p3.setWordWrap(true);  
  74.         p3.setPageBreak(true);  
  75.                   
  76.         //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);  
  77.         p3.setAlignment(ParagraphAlignment.BOTH);  
  78.         p3.setSpacingLineRule(LineSpacingRule.EXACT);  
  79.   
  80.         p3.setIndentationFirstLine(600);  
  81.           
  82.   
  83.         XWPFRun r4 = p3.createRun();  
  84.         r4.setTextPosition(20);  
  85.         r4.setText("To be, or not to be: that is the question: "  
  86.                 + "Whether 'tis nobler in the mind to suffer "  
  87.                 + "The slings and arrows of outrageous fortune, "  
  88.                 + "Or to take arms against a sea of troubles, "  
  89.                 + "And by opposing end them? To die: to sleep; ");  
  90.         r4.addBreak(BreakType.PAGE);  
  91.         r4.setText("No more; and by a sleep to say we end "  
  92.                 + "The heart-ache and the thousand natural shocks "  
  93.                 + "That flesh is heir to, 'tis a consummation "  
  94.                 + "Devoutly to be wish'd. To die, to sleep; "  
  95.                 + "To sleep: perchance to dream: ay, there's the rub; "  
  96.                 + ".......");  
  97.         r4.setItalic(true);  
  98. //This would imply that this break shall be treated as a simple line break, and break the line after that word:  
  99.   
  100.         XWPFRun r5 = p3.createRun();  
  101.         r5.setTextPosition(-10);  
  102.         r5.setText("For in that sleep of death what dreams may come");  
  103.         r5.addCarriageReturn();  
  104.         r5.setText("When we have shuffled off this mortal coil,"  
  105.                 + "Must give us pause: there's the respect"  
  106.                 + "That makes calamity of so long life;");  
  107.         r5.addBreak();  
  108.         r5.setText("For who would bear the whips and scorns of time,"  
  109.                 + "The oppressor's wrong, the proud man's contumely,");  
  110.           
  111.         r5.addBreak(BreakClear.ALL);  
  112.         r5.setText("The pangs of despised love, the law's delay,"  
  113.                 + "The insolence of office and the spurns" + ".......");  
  114.   
  115.         FileOutputStream out = new FileOutputStream("simple.docx");  
  116.         doc.write(out);  
  117.         out.close();  
  118.   
  119.     }  
  120. }  
0 0
原创粉丝点击