最近公司做的项目需要实现导出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
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容- Java 动态线程池对性能究竟有哪些影响呢?(Java动态线程池对性能的影响)
- 在 Java 中,Guava 究竟有哪些作用呢?(java中guava的作用是什么)
- 软考高项证书能个税抵扣吗?软考高项证书个税啥时候填报?
- 在 JavaScript 中,offsetWidth 的作用究竟是什么?(javascript中offsetwidth作用是什么)
- 软考高项证书个税抵扣有用吗?软考高项证书怎么抵扣个税?
- Java 多线程究竟能否实现线程池?(Java多线程能实现线程池吗)
- Java 序列化中 getByte 方法的角色究竟是什么?(getbyte方法在java序列化中的角色)
- 软考高项证书个税怎么填报?软考高项证书个税填报方法
- Java log4j2 究竟可以进行哪些定制化操作?(java log4j2能做哪些定制化)
- 如何在 Java 中实现文件导入数据库?(Java怎么实现文件导入数据库)
猜你喜欢
AI推送时光机利用Java Apache POI 生成Word文档示例代码
后端开发2023-05-31
Java实现导出Word文档的示例代码
后端开发2023-02-09
Java实现合并word文档的示例代码
后端开发2022-11-13
Java实现PDF转为Word文档的示例代码
后端开发2023-01-28
利用java生成二维码工具类示例代码
后端开发2023-05-31
java 生成文字图片的示例代码
后端开发2023-05-31
C++利用GPAC实现生成MP4文件的示例代码
后端开发2023-02-06
Java如何实现长图文生成的示例代码
后端开发2023-05-31
java使用dom4j生成与解析xml文档的方法示例
后端开发2023-05-31
Java利用File类创建文件的示例代码
后端开发2024-04-02
咦!没有更多了?去看看其它编程学习网 内容吧