- 引入poi的依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-full</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>4.0.0</version>
</dependency>
生成word文档(带简单表格)
//创建一个word 文档
XWPFDocument xwpfDocument = new XWPFDocument();
//新建一个段落
XWPFParagraph paragraph = xwpfDocument.createParagraph();
//创建一个具有相同属性的文本区域 -- 盒子
XWPFRun paragraphRun = paragraph.createRun();
paragraphRun.setText("您好,我正在学习如何生成word文档");
//在文档中创建一个表格
XWPFTable table = xwpfDocument.createTable();
//设置表格跟word文档一样宽
table.setWidth("100%");
//设置表格样式
// 设置表格样式
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTJc cellAlignment = CTJc.Factory.newInstance();
cellAlignment.setVal(STJc.CENTER); // 设置水平居中
tblPr.setJc(cellAlignment);
//获取表格第一行
XWPFTableRow onerow = table.getRow(0);
//获取第一个单元格
XWPFTableCell cell = onerow.getCell(0);
//设置单元格的内容水平居中
//cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
//设置单元格的内容
cell.setText("我是第一行,第一列");
//在上面表格的基础上添加一列
onerow.addNewTableCell().setText("我是第一行,第二列");
//创建表格的第二行 (会在第一行的基础上进行生成)
XWPFTableRow row = table.createRow();
XWPFTableCell cell0 = row.getCell(0);
XWPFTableCell cell1 = row.getCell(1);
cell0.setText("我是第二行,第一列");
cell1.setText("我是第二行,第二列");
/**
* 创建一篇文章
* 第一章:文章开头
* 第二章:文章内容
* 第三章:文章开头
*/
//新建文章开头
XWPFParagraph paragraphHeader = xwpfDocument.createParagraph();
//设置文章标题居中
paragraphHeader.setAlignment(ParagraphAlignment.CENTER);
XWPFRun runHeader = paragraphHeader.createRun();
//标题加粗
runHeader.setBold(true);
runHeader.setFontSize(30);
runHeader.setText("文章开头");
//新建文章内容
XWPFParagraph paragraphContent = xwpfDocument.createParagraph();
//设置开始空两个格
paragraphContent.setIndentationFirstLine(400);
XWPFRun runContent = paragraphContent.createRun();
//模拟文档内容
StringBuffer content = new StringBuffer();
for (int i = 0; i < 50 ; i++) {
content.append("文章内容");
}
runContent.setText(content.toString());
runContent.setFontFamily("楷体");
//新建文章内容
XWPFParagraph paragraphFooter = xwpfDocument.createParagraph();
paragraphFooter.setIndentationFirstLine(400);
XWPFRun runFooter = paragraphFooter.createRun();
//模拟文章结尾
StringBuffer foot = new StringBuffer();
for (int i = 0; i < 20 ; i++) {
foot.append("文章结尾");
}
runFooter.setText(foot.toString());
runFooter.setFontFamily("华文琥珀");
//创建一个文件对象
File file = new File("doctest.docx");
//创建一个文件输出流
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
xwpfDocument.write(fileOutputStream);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("生成完成");
生成word文档(复杂表格样式)
//创建一个word文档
XWPFDocument xwpfDocument = new XWPFDocument();
//表格的行和列的大小
int rowNums = 5;
int colNums = 7;
XWPFTable table = xwpfDocument.createTable(rowNums, colNums);
//设置表格样式
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString tblStyle = tblPr.addNewTblStyle();
tblStyle.setVal("StyleTable");
List<XWPFTableRow> rows = table.getRows();
int currentRow = 0;
int currentCol = 0;
for (XWPFTableRow row : rows) {
//<tr></tr>
CTTrPr ctTrPr = row.getCtRow().addNewTrPr();
CTHeight ctHeight = ctTrPr.addNewTrHeight();
ctHeight.setVal(BigInteger.valueOf(300));
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
CTTcPr ctTcPr = cell.getCTTc().addNewTcPr();
CTVerticalJc ctVerticalJc = ctTcPr.addNewVAlign();
//设置单元格居中
ctVerticalJc.setVal(STVerticalJc.CENTER);
CTShd ctShd = ctTcPr.addNewShd();
ctShd.setColor("auto");
ctShd.setVal(STShd.CLEAR);
if (currentRow == 0) {
ctShd.setFill("708090");
} else {
ctShd.setFill("90EE90");
}
XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);
XWPFRun run = xwpfParagraph.createRun();
if (currentRow == 0) {
run.setFontSize(18);
run.setFontFamily("楷体");
}
if (currentRow == 0) {
run.setText("header=" + (currentCol + 1));
} else {
run.setText("body=" + (currentCol + 1));
}
xwpfParagraph.setAlignment(ParagraphAlignment.CENTER);
currentCol++;
}
currentCol = 0;
currentRow++;
}
File file = new File("table.docx");
//创建一个文件输出流
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
xwpfDocument.write(fileOutputStream);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("生成完成");