最近做了个poi导出word的功能

下面是代码:

一个可以参考的例子:

  1. package com.lzb.crm.web;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.math.BigInteger;
  5. import java.util.List;
  6.  
  7. import org.apache.poi.xwpf.usermodel.Borders;
  8. import org.apache.poi.xwpf.usermodel.BreakClear;
  9. import org.apache.poi.xwpf.usermodel.BreakType;
  10. import org.apache.poi.xwpf.usermodel.LineSpacingRule;
  11. import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
  12. import org.apache.poi.xwpf.usermodel.VerticalAlign;
  13. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  14. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  15. import org.apache.poi.xwpf.usermodel.XWPFRun;
  16. import org.apache.poi.xwpf.usermodel.XWPFTable;
  17. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  18. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
  19. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
  20.  
  21. /**
  22. *
  23. * @author 全力以赴001
  24. */
  25. public class ExportDocTest {
  26.  
  27. public static void main(String[] args) throws Exception {
  28.  
  29. XWPFDocument doc = new XWPFDocument();
  30.  
  31. XWPFParagraph title = doc.createParagraph();//设置活动标题
  32. title.setAlignment(ParagraphAlignment.CENTER);
  33. XWPFRun r1 = title.createRun();
  34. r1.setBold(true);
  35. r1.setFontFamily("宋体");
  36. r1.setText("20元优惠劵活动");//活动名称
  37. r1.setFontSize(22);
  38.  
  39. XWPFParagraph actTheme = doc.createParagraph();//设置活动主题
  40. actTheme.setAlignment(ParagraphAlignment.LEFT);
  41. XWPFRun runText1=actTheme.createRun();
  42. runText1.setText("活动主题:20劵优惠劵活动");
  43. runText1.setFontSize(15);
  44.  
  45. XWPFParagraph actType = doc.createParagraph();//设置活动类型
  46. XWPFRun runText2=actType.createRun();
  47. runText2.setText("活动类型:系统发劵类型");
  48. runText2.setFontSize(15);
  49.  
  50. XWPFParagraph actDate = doc.createParagraph();//设置活动日期
  51. XWPFRun actDaterun=actDate.createRun();
  52. actDaterun.setText("活动日期:2015-06-08至2015-06-10");
  53. actDaterun.setFontSize(15);
  54.  
  55. XWPFParagraph actText = doc.createParagraph();//设置活动内容
  56. XWPFRun runText3=actText.createRun();
  57. runText3.setText("活动内容:哈哈哈士大夫士大夫立刻绝对是方路即可大水井坊路可绝对是弗兰克家第三方立刻几点睡了罚款绝对是路客服绝对是路客服绝对是路客服几点睡了罚款家第三方立刻几点睡了罚款记录可定时 ");
  58. runText3.setFontSize(15);
  59.  
  60. XWPFParagraph actRemark = doc.createParagraph();//设置活动备注
  61. XWPFRun runText4=actRemark.createRun();
  62. runText4.setText("活动备注: ");
  63. runText4.setFontSize(15);
  64. runText4.setBold(true);
  65. XWPFRun runText5=actRemark.createRun();
  66. runText5.setText("我是活动备注哦........................ ");
  67. runText5.setFontSize(15);
  68.  
  69. XWPFParagraph actRule = doc.createParagraph();//设置活动备注
  70. XWPFRun rule=actRule.createRun();
  71. rule.setText("活动规则: ");
  72. rule.setFontSize(15);
  73. rule.setBold(true);
  74.  
  75. XWPFTable table=actRule.getDocument().createTable(2,2);//创建表格
  76. table.setWidth(500);
  77. table.setCellMargins(20, 20, 20, 20);
  78. System.out.println(table.getWidth());
  79.  
  80. //表格属性
  81. CTTblPr tablePr = table.getCTTbl().addNewTblPr();
  82. //表格宽度
  83. CTTblWidth width = tablePr.addNewTblW();
  84. width.setW(BigInteger.valueOf(8000));
  85.  
  86. List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
  87. tableCells.get(0).setText("第一行第一列的数据:规则类型名称");
  88. tableCells.get(1).setText("第一行第二列的数据:规则描述");
  89.  
  90. List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
  91. tableCellsq.get(0).setText("第二行第一列的数据:A发劵规则");
  92. tableCellsq.get(1).setText("第二行第二列的数据:A发劵规则针对5星级用户");
  93.  
  94. XWPFParagraph text3 = doc.createParagraph();
  95. XWPFRun runText7=text3.createRun();
  96. runText7.setText("负责人:zhangsan");
  97. runText7.setFontSize(15);
  98.  
  99. XWPFParagraph text8 = doc.createParagraph();
  100. XWPFRun runText8=text8.createRun();
  101. runText8.setText("负责人电话:12345678921");
  102. runText8.setFontSize(15);
  103.  
  104. FileOutputStream out = new FileOutputStream("C:\\User\\Desktop\\test.docx");
  105.  
  106. doc.write(out);
  107. System.out.println(1);
  108. out.close();
  109.  
  110. }
  111. }

poi导出word的更多相关文章

  1. java工具类POI导出word

    1.新建一个word,里面填写内容,如: 2.导出wordjava类 /** * POI导出word测试 * @throws Exception */ @RequestMapping(value=&q ...

  2. poi导出word表格详解 超详细了

    转:非常感谢原作者 poi导出word表格详解 2018年07月20日 10:41:33 Z丶royAl 阅读数:36138   一.效果如下 二.js代码 function export_word( ...

  3. poi导出word时设置兼容性

    接上一篇poi导出word http://www.cnblogs.com/xiufengd/p/4708680.html. public static void setAuto(XWPFDocumen ...

  4. 使用POI导出Word(含表格)的实现方式及操作Word的工具类

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  5. Spring MVC中使用POI导出Word

    内容绝大部分来源于网络 准备工作 准备[XwpfTUtil]工具类(来源于网络) 准备word模版 下载[XwpfTUtil]工具类 import org.apache.poi.xwpf.usermo ...

  6. POI导出Word插入复选框

    POI功能比较强大,但是有些不常用功能比如插入特殊符号,不知道API怎么调用 Word里要插入复选框,首先想到的是POI有没有提供现成的API,搜了一番,貌似都说不直接支持 http://stacko ...

  7. poi导出word模板项目实例(一个文件)

    在页面上填写值,然后导出到word模板中,并把页面上的值带到模板中,也就是导出word文档,提前有word 的模板形式, 1.jsp 页面   <table class="formTa ...

  8. poi导出word表格跨行

    DataCommon.java package com.ksource.pwlp.model.statistic; public class DataCommon { private Long id; ...

  9. poi导出word文档,doc和docx

    maven <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency> <gro ...

随机推荐

  1. [转] Linux TCP/IP网络小课堂:net-tools与iproute2大比较

    PS:netstat选项是-planet,方便记忆 http://os.51cto.com/art/201409/450886.htm 如今许多系统管理员仍结合使用ifconfig.route.arp ...

  2. checkbox 删除

    先创建del.php文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  3. htm5 user-scalable 的意思

    <meta name="viewport" content="width=device-width,user-scalable=yes,minimum-scale= ...

  4. HTML5 ArrayBuffer:类型化数组 (二)

    类型化数组是JavaScript操作二进制数据的一个接口. 这要从WebGL项目的诞生说起,所谓WebGL,就是指浏览器与显卡之间的通信接口,为了满足JavaScript与显卡之间大量的.实时的数据交 ...

  5. UILabel,UITextField,UIButton三大基础控件总结

    (一)UILabel空件 属性: 1.背景颜色 label.backgroundColor = [UIColor ***]; 2. 显示文字: label.text = @"******&q ...

  6. Web学习资源及手册查询整理

    入门了解html.css.js.jQuery:http://www.w3school.com.cn/, bootstrap.nodejs.php.jQuery入门:http://www.runoob. ...

  7. linux网络编程常用头文件

    sys/types.h:数据类型定义 sys/socket.h:提供socket函数及数据结构 netinet/in.h:定义数据结构sockaddr_in arpa/inet.h:提供IP地址转换函 ...

  8. Log4j实现对Java日志的配置全攻略

    1. 配置文件 Log4J配置文件的基本格式如下: #配置根Logger log4j.rootLogger = [ level ] , appenderName1 , appenderName2 , ...

  9. Adb shell 常用命令

    1. 查看IP adb shell netcfg 2. 查看挂载设备 adb devices 3. 将本地端口转发至手机端口 adb forward tcp: tcp: // PC上所有6100端口通 ...

  10. Android学习----打印日志Log

    Log.v(tag,msg);所有内容 Log.d(tag,msg);debug Log.i(tag,msg);一般信息 Log.w(tag,msg);警告信息 Log.e(tag,msg);错误信息 ...