java导出word文档_poi生成word文件

1 packagecom.test.word;2

3 importjava.io.FileOutputStream;4 importjava.io.IOException;5 importjava.math.BigInteger;6 importjava.util.List;7

8 importorg.apache.poi.xwpf.usermodel.ParagraphAlignment;9 importorg.apache.poi.xwpf.usermodel.TextAlignment;10 importorg.apache.poi.xwpf.usermodel.XWPFDocument;11 importorg.apache.poi.xwpf.usermodel.XWPFParagraph;12 importorg.apache.poi.xwpf.usermodel.XWPFRun;13 importorg.apache.poi.xwpf.usermodel.XWPFTable;14 importorg.apache.poi.xwpf.usermodel.XWPFTableCell;15 importorg.junit.Test;16 importorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;17 importorg.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;18

19 /**

20 * 创建word文档21 */

22 public classWordCreate {23 /**

24 * 2007word文档创建25 */

26 @Test27 public voidcreateWord2007() {28 XWPFDocument doc = newXWPFDocument();29 XWPFParagraph p1 =doc.createParagraph();30

31 XWPFTable table = doc.createTable(11, 4);32 //CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();

33 CTTblPr tblPr =table.getCTTbl().getTblPr();34 tblPr.getTblW().setType(STTblWidth.DXA);35 tblPr.getTblW().setW(new BigInteger("7000"));36

37 //设置上下左右四个方向的距离,可以将表格撑大

38 table.setCellMargins(20, 20, 20, 20);39

40 //表格

41 List tableCells = table.getRow(0).getTableCells();42

43 XWPFTableCell cell = tableCells.get(0);44 XWPFParagraph newPara = newXWPFParagraph(cell.getCTTc().addNewP(), cell);45 XWPFRun run =newPara.createRun();46 /**内容居中显示 **/

47 newPara.setAlignment(ParagraphAlignment.CENTER);48 //run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000红色*/49 //run.setUnderline(UnderlinePatterns.THICK);

50 run.setText("第一 数据");51

52 tableCells.get(1).setText("第一 数据");53 tableCells.get(2).setText("第一 据");54 tableCells.get(3).setText("第 据");55

56 tableCells = table.getRow(1).getTableCells();57 tableCells.get(0).setText("第数据");58 tableCells.get(1).setText("第一 数据");59 tableCells.get(2).setText("第一 据");60 tableCells.get(3).setText("第 据");61

62 //设置字体对齐方式

63 p1.setAlignment(ParagraphAlignment.CENTER);64 p1.setVerticalAlignment(TextAlignment.TOP);65

66 //第一页要使用p1所定义的属性

67 XWPFRun r1 =p1.createRun();68

69 //设置字体是否加粗

70 r1.setBold(true);71 r1.setFontSize(20);72

73 //设置使用何种字体

74 r1.setFontFamily("Courier");75

76 //设置上下两行之间的间距

77 r1.setTextPosition(20);78 r1.setText("标题");79

80 FileOutputStream out;81 try{82 out = new FileOutputStream("c:/test/word2007.docx");83 //以下代码可进行文件下载84 //response.reset();85 //response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");86 //response.setHeader("Content-Disposition",87 //"attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));88 //OutputStream out = response.getOutputStream();89 //this.doc.write(out);90 //out.flush();

91

92 doc.write(out);93 out.close();94 } catch(IOException e) {95 e.printStackTrace();96 }97 System.out.println("success");98 }99

100 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值