最近做了个poi导出word的功能
下面是代码:
一个可以参考的例子:
package com.lzb.crm.web; import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.List; import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.BreakClear;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.LineSpacingRule;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; /**
*
* @author 全力以赴001
*/
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("活动主题:20劵优惠劵活动");
runText1.setFontSize(15); XWPFParagraph actType = doc.createParagraph();//设置活动类型
XWPFRun runText2=actType.createRun();
runText2.setText("活动类型:系统发劵类型");
runText2.setFontSize(15); XWPFParagraph actDate = doc.createParagraph();//设置活动日期
XWPFRun actDaterun=actDate.createRun();
actDaterun.setText("活动日期:2015-06-08至2015-06-10");
actDaterun.setFontSize(15); XWPFParagraph actText = doc.createParagraph();//设置活动内容
XWPFRun runText3=actText.createRun();
runText3.setText("活动内容:哈哈哈士大夫士大夫立刻绝对是方路即可大水井坊路可绝对是弗兰克家第三方立刻几点睡了罚款绝对是路客服绝对是路客服绝对是路客服几点睡了罚款家第三方立刻几点睡了罚款记录可定时 ");
runText3.setFontSize(15); XWPFParagraph actRemark = doc.createParagraph();//设置活动备注
XWPFRun runText4=actRemark.createRun();
runText4.setText("活动备注: ");
runText4.setFontSize(15);
runText4.setBold(true);
XWPFRun runText5=actRemark.createRun();
runText5.setText("我是活动备注哦........................ ");
runText5.setFontSize(15); XWPFParagraph actRule = doc.createParagraph();//设置活动备注
XWPFRun rule=actRule.createRun();
rule.setText("活动规则: ");
rule.setFontSize(15);
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 text3 = doc.createParagraph();
XWPFRun runText7=text3.createRun();
runText7.setText("负责人:zhangsan");
runText7.setFontSize(15); XWPFParagraph text8 = doc.createParagraph();
XWPFRun runText8=text8.createRun();
runText8.setText("负责人电话:12345678921");
runText8.setFontSize(15); FileOutputStream out = new FileOutputStream("C:\\User\\Desktop\\test.docx"); doc.write(out);
System.out.println(1);
out.close(); }
}