zoukankan      html  css  js  c++  java
  • poi导出word

    最近做了个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       
    105         FileOutputStream out = new FileOutputStream("C:\User\Desktop\test.docx");
    106         
    107         doc.write(out);
    108         System.out.println(1);
    109         out.close();
    110 
    111     }
    112 }
  • 相关阅读:
    给Apache增加SSI支持(shtml的奥秘)
    Raphael实现商品来源去向图
    有趣的居中方式
    oc-基本语法
    APMServ 配置记录
    解决Mac Chrome打开HTTPS证书错误问题
    JavaScript生成GUID的算法
    Backbone模型
    利用apply和arguments复用方法
    软件复用的几种方式
  • 原文地址:https://www.cnblogs.com/huzi007/p/4566113.html
Copyright © 2011-2022 走看看