其他分享
首页 > 其他分享> > POI XWPFDocument 实现word导出功能

POI XWPFDocument 实现word导出功能

作者:互联网

想系统学习的同志,可以参考 POI官方

1、引用依赖

  <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency> <!-- 操作File好用 可选 -->
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
    </dependencies>

 

2、编写代码

import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.List;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;

public class ExportDocTest {

  public static void main(String[] args) throws Exception {

       XWPFDocument doc = new XWPFDocument();

       XWPFParagraph title = doc.createParagraph();//设置活动标题
       title.setAlignment(ParagraphAlignment.CENTER);
       XWPFRun r1 = title.createRun();
       r1.setBold(true);
       r1.setFontFamily("宋体");
       r1.setText("20元优惠劵活动");//活动名称
       r1.setFontSize(22);

       XWPFParagraph actTheme = doc.createParagraph();//设置活动主题
       actTheme.setAlignment(ParagraphAlignment.LEFT);
       XWPFRun runText1=actTheme.createRun();
       runText1.setText("1、活动主题:20劵优惠劵活动");
       runText1.setFontSize(13);

       XWPFParagraph actType = doc.createParagraph();//设置活动类型
       XWPFRun runText2=actType.createRun();
       runText2.setText("2、活动类型:系统发劵类型");
       runText2.setFontSize(13);

       XWPFParagraph actDate = doc.createParagraph();//设置活动日期
       XWPFRun actDaterun=actDate.createRun();
       actDaterun.setText("3、活动日期:2015-06-08至2015-06-10");
       actDaterun.setFontSize(13);

       XWPFParagraph actText = doc.createParagraph();//设置活动内容
       XWPFRun runText3=actText.createRun();
       runText3.setFontSize(13);
       runText3.setText("4、活动内容:哈哈哈士大夫士大夫立刻绝对是方路即可大水井坊路可绝对是弗兰克家第三方立刻几点睡了罚款绝对是路客服绝对是路客服绝对是路客服几点睡了罚款家第三方立刻几点睡了罚款记录可定时 ");

       XWPFParagraph actRemark = doc.createParagraph();//设置活动备注
       XWPFRun runText4=actRemark.createRun();
       runText4.setText("5、活动备注: ");
       runText4.setFontSize(13);
       runText4.setBold(true);
       XWPFRun runText5=actRemark.createRun();
       runText5.setText("我是活动备注哦........................ ");
       runText5.setFontSize(12);

       XWPFParagraph actRule = doc.createParagraph();//设置活动备注
       XWPFRun rule=actRule.createRun();
       rule.setText("6、活动规则: ");
       rule.setFontSize(13);
       rule.setBold(true);

       XWPFTable table=actRule.getDocument().createTable(2,2);//创建表格
       table.setWidth(500);
       table.setCellMargins(20, 20, 20, 20);
       System.out.println(table.getWidth());

       //表格属性
       CTTblPr tablePr = table.getCTTbl().addNewTblPr();
       //表格宽度
       CTTblWidth width = tablePr.addNewTblW();
       width.setW(BigInteger.valueOf(8000));

       List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
       tableCells.get(0).setText("第一行第一列的数据:规则类型名称");
       tableCells.get(1).setText("第一行第二列的数据:规则描述");

       List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
       tableCellsq.get(0).setText("第二行第一列的数据:A发劵规则");
       tableCellsq.get(1).setText("第二行第二列的数据:A发劵规则针对5星级用户");

       XWPFParagraph text9 = doc.createParagraph();
       XWPFRun runText9=text9.createRun();
       runText9.addBreak(BreakType.TEXT_WRAPPING);//换行
       runText9.setBold(true);
       runText9.setText("7、请现在你喜欢运动?");
       runText9.setFontSize(13);

       XWPFParagraph text10 = doc.createParagraph();
       XWPFRun runText10=text10.createRun();
       runText10.setFontSize(12);
       runText10.setText("□a.排球       ");
       runText10.setText("☑b.羽毛球");


       XWPFParagraph text3 = doc.createParagraph();
       XWPFRun runText7=text3.createRun();
       runText7.setText("负责人:zhangsan");
       runText7.setFontSize(13);

       XWPFParagraph text8 = doc.createParagraph();
       XWPFRun runText8=text8.createRun();
       runText8.setText("负责人电话:12345678921");
       runText8.setFontSize(13);


       FileOutputStream out = new FileOutputStream("E:\\test.docx");

       doc.write(out);
       System.out.println(1);
       out.close();

    }
}

 

标签:createRun,word,doc,setText,setFontSize,XWPFDocument,POI,XWPFParagraph,XWPFRun
来源: https://www.cnblogs.com/M87-A/p/15312584.html