欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

利用Java Apache POI 生成Word文档示例代码

程序员文章站 2024-02-21 09:36:46
最近公司做的项目需要实现导出word文档的功能,网上关于poi生成word文档的例子很少,找了半天才在官网里找到个demo,有了demo一切就好办了。 /* ==...

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

/* ==================================================================== 
  licensed to the apache software foundation (asf) under one or more 
  contributor license agreements. see the notice file distributed with 
  this work for additional information regarding copyright ownership. 
  the asf licenses this file to you under the apache license, version 2.0 
  (the "license"); you may not use this file except in compliance with 
  the license. you may obtain a copy of the license at 
 
    http://www.apache.org/licenses/license-2.0 
 
  unless required by applicable law or agreed to in writing, software 
  distributed under the license is distributed on an "as is" basis, 
  without warranties or conditions of any kind, either express or implied. 
  see the license for the specific language governing permissions and 
  limitations under the license. 
==================================================================== */ 
package org.apache.poi.xwpf.usermodel; 
 
import java.io.fileoutputstream; 
 
/** 
 * a simple wordprocessingml document created by poi xwpf api 
 * 
 * @author yegor kozlov 
 */ 
public class simpledocument { 
 
  public static void main(string[] args) throws exception { 
    xwpfdocument doc = new xwpfdocument(); 
 
    xwpfparagraph p1 = doc.createparagraph(); 
    p1.setalignment(paragraphalignment.center); 
    p1.setborderbottom(borders.double); 
    p1.setbordertop(borders.double); 
 
    p1.setborderright(borders.double); 
    p1.setborderleft(borders.double); 
    p1.setborderbetween(borders.single); 
 
    p1.setverticalalignment(textalignment.top); 
 
    xwpfrun r1 = p1.createrun(); 
    r1.setbold(true); 
    r1.settext("the quick brown fox"); 
    r1.setbold(true); 
    r1.setfontfamily("courier"); 
    r1.setunderline(underlinepatterns.dot_dot_dash); 
    r1.settextposition(100); 
 
    xwpfparagraph p2 = doc.createparagraph(); 
    p2.setalignment(paragraphalignment.right); 
 
    //borders 
    p2.setborderbottom(borders.double); 
    p2.setbordertop(borders.double); 
    p2.setborderright(borders.double); 
    p2.setborderleft(borders.double); 
    p2.setborderbetween(borders.single); 
 
    xwpfrun r2 = p2.createrun(); 
    r2.settext("jumped over the lazy dog"); 
    r2.setstrike(true); 
    r2.setfontsize(20); 
 
    xwpfrun r3 = p2.createrun(); 
    r3.settext("and went away"); 
    r3.setstrike(true); 
    r3.setfontsize(20); 
    r3.setsubscript(verticalalign.superscript); 
 
 
    xwpfparagraph p3 = doc.createparagraph(); 
    p3.setwordwrap(true); 
    p3.setpagebreak(true); 
         
    //p3.setalignment(paragraphalignment.distribute); 
    p3.setalignment(paragraphalignment.both); 
    p3.setspacinglinerule(linespacingrule.exact); 
 
    p3.setindentationfirstline(600); 
     
 
    xwpfrun r4 = p3.createrun(); 
    r4.settextposition(20); 
    r4.settext("to be, or not to be: that is the question: " 
        + "whether 'tis nobler in the mind to suffer " 
        + "the slings and arrows of outrageous fortune, " 
        + "or to take arms against a sea of troubles, " 
        + "and by opposing end them? to die: to sleep; "); 
    r4.addbreak(breaktype.page); 
    r4.settext("no more; and by a sleep to say we end " 
        + "the heart-ache and the thousand natural shocks " 
        + "that flesh is heir to, 'tis a consummation " 
        + "devoutly to be wish'd. to die, to sleep; " 
        + "to sleep: perchance to dream: ay, there's the rub; " 
        + "......."); 
    r4.setitalic(true); 
//this would imply that this break shall be treated as a simple line break, and break the line after that word: 
 
    xwpfrun r5 = p3.createrun(); 
    r5.settextposition(-10); 
    r5.settext("for in that sleep of death what dreams may come"); 
    r5.addcarriagereturn(); 
    r5.settext("when we have shuffled off this mortal coil," 
        + "must give us pause: there's the respect" 
        + "that makes calamity of so long life;"); 
    r5.addbreak(); 
    r5.settext("for who would bear the whips and scorns of time," 
        + "the oppressor's wrong, the proud man's contumely,"); 
     
    r5.addbreak(breakclear.all); 
    r5.settext("the pangs of despised love, the law's delay," 
        + "the insolence of office and the spurns" + "......."); 
 
    fileoutputstream out = new fileoutputstream("simple.docx"); 
    doc.write(out); 
    out.close(); 
 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。