文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-05-31 14:47

关注

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

 package org.apache.poi.xwpf.usermodel;  import java.io.FileOutputStream;   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();    } } 

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯