Java使用poi生成word文档的简单实例
生成的效果如下:
用到的poi的简单的知识
新建一个word对象
1 2 | //新建文件 XWPFDocument document = new XWPFDocument(); |
新建段落以及文字样式
1 2 3 4 5 6 7 8 9 10 11 12 | //创建段落 XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); //创建标题 XWPFRun titleFun = paragraph.createRun(); titleFun.setText( "个人信息表" ); titleFun.setBold( true ); titleFun.setFontSize( 25 ); titleFun.setColor( "000000" ); titleFun.setFontFamily( "宋体" ); titleFun.addBreak(); |
titleFun.addBreak(); 换行
实例-生成一个简单word文档
新建一个maven项目
pom.xml 导入5.2.5的poi的依赖包
1 | org.apache.poipoi-ooxml5.2.5org.apache.poipoi5.2.5 |
新建SimpleWord.java文件
文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | package com.wumeng.wordexport; import org.apache.poi.xwpf.usermodel.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @author wumeng 2024/6/27 17:56 */ public class SimpleWord { /** * 新建word * @throws IOException */ static void newWord(String outputPath) throws IOException { //新建文件 XWPFDocument document = new XWPFDocument(); //创建段落 XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); //创建标题 XWPFRun titleFun = paragraph.createRun(); titleFun.setText( "个人信息表" ); titleFun.setBold( true ); titleFun.setFontSize( 25 ); titleFun.setColor( "000000" ); titleFun.setFontFamily( "宋体" ); titleFun.addBreak(); //添加引言 XWPFParagraph paragraph2 = document.createParagraph(); paragraph2.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleFun2 = paragraph2.createRun(); titleFun2.setText( "引言" ); titleFun2.setBold( true ); titleFun2.setFontSize( 20 ); titleFun2.setColor( "000000" ); titleFun2.setFontFamily( "宋体" ); titleFun2.addBreak(); //添加第一段内容 XWPFParagraph paragraph3 = document.createParagraph(); paragraph3.setAlignment(ParagraphAlignment.LEFT); XWPFRun titleFun3 = paragraph3.createRun(); titleFun3.setText( "个人信息表" ); titleFun3.setBold( true ); titleFun3.setFontSize( 14 ); titleFun3.setColor( "000000" ); titleFun3.setFontFamily( "宋体" ); XWPFRun titleFun4 = paragraph3.createRun(); titleFun4.setText( ",(注:以下内容请根据个人情况如实填写)" ); titleFun4.setFontSize( 14 ); titleFun4.setColor( "000000" ); titleFun4.setFontFamily( "宋体" ); titleFun4.addBreak(); //创建表格 XWPFTable table = document.createTable( 1 , 3 ); SimpleWordUtil.setTableWidthAndHAlign(table, "9072" ,STJcTable.Enum.forString( "center" )); List headers = new ArrayList(); headers.add( "姓名" ); headers.add( "性别" ); headers.add( "年龄" ); for ( int i = 0 ; i > lists = new ArrayList>(); List list1 = new ArrayList(); list1.add( "黄xx" ); list1.add( "男" ); list1.add( "18" ); lists.add(list1); List list2 = new ArrayList(); list2.add( "王xx" ); list2.add( "女" ); list2.add( "16" ); lists.add(list2); for ( int i = 0 ; i |
相关工具类SimpleWordUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | package com.wumeng.wordexport; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigInteger; /** * @author wumeng 2024/6/27 18:03 */ public class SimpleWordUtil { /** * 获取表格属性 * @param table * @return */ public static CTTblPr getTableCTTblPr(XWPFTable table) { CTTbl ttbl = table.getCTTbl(); // 表格属性 CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr(); return tblPr; } /** * 设置表格宽度和水平对齐方式 * @param table * @param width * @param enumValue */ public static void setTableWidthAndHAlign(XWPFTable table, String width, STJcTable.Enum enumValue) { CTTblPr tblPr = getTableCTTblPr(table); // 表格宽度 CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); if (enumValue != null ) { CTJcTable ctJcTable = tblPr.addNewJc(); ctJcTable.setVal(enumValue); } // 设置宽度 tblWidth.setW( new BigInteger(width)); tblWidth.setType(STTblWidth.DXA); } /** * 保存文档 * @param document * @param savePath * @throws IOException */ public static void saveDocument(XWPFDocument document, String savePath) throws IOException { OutputStream os = new FileOutputStream(savePath); document.write(os); os.close(); } } |
运行,就可以看到效果了!!
到此这篇关于Java使用poi生成word文档的简单实例的文章就介绍到这了,更多相关Java poi生成word文档内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!