类似谷德设计网的网站,网站制作 苏州,微软网站设计,微信开发者工具介绍及其优点文章目录 使用POI生成word文档的table表格1. 引入maven依赖2. 生成table的两种方式介绍2.1 生成一行一列的table2.2 生成固定行列的table2.3 table合并列2.4 创建多个table存在的问题 使用POI生成word文档的table表格
1. 引入maven依赖 dependencygroupIdorg.… 文章目录 使用POI生成word文档的table表格1. 引入maven依赖2. 生成table的两种方式介绍2.1 生成一行一列的table2.2 生成固定行列的table2.3 table合并列2.4 创建多个table存在的问题 使用POI生成word文档的table表格
1. 引入maven依赖 dependencygroupIdorg.apache.poi/groupIdartifactIdpoi/artifactIdversion4.1.2/version/dependencydependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion4.1.2/version/dependencydependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml-schemas/artifactIdversion4.1.2/version/dependency2. 生成table的两种方式介绍
2.1 生成一行一列的table
//生成一行一列的table XWPFTable table document.createTable(); //添加列 table.getRow(0).addNewTableCell(); //添加行添加的新行默认就是总共的列数 table.createRow();
测试DemoCreateTableDemo1.java
package com.poi.word.demo;import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;import java.io.FileOutputStream;public class CreateTableDemo1 {public static void main(String[] args) throws Exception {XWPFDocument document new XWPFDocument();//默认创建一行一列tableXWPFTable table document.createTable();table.setWidth(100%);XWPFTableRow first_row table.getRow(0);XWPFTableCell first_Row_first_Cell first_row.getCell(0);first_Row_first_Cell.setText(我是第一行第一列);//第一行添加一列first_row.addNewTableCell().setText(我是第一行第二列);//创建第二行XWPFTableRow snd_row table.createRow();snd_row.getCell(0).setText(第二行第一列);snd_row.getCell(1).setText(第二行第二列);//创建第三行XWPFTableRow trd_row table.createRow();XWPFParagraph trd_row_first_paragraph trd_row.getCell(0).getParagraphs().get(0);XWPFRun trdRowFirstCellRun trd_row_first_paragraph.createRun();trdRowFirstCellRun.setFontSize(14);trdRowFirstCellRun.setBold(true);trdRowFirstCellRun.setText(第三行第一列);trd_row.getCell(1).setText(第三行第二列);//创建第四行XWPFTableRow row4 table.createRow();row4.getCell(0).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);row4.getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);row4.getCell(0).setText(第四行);FileOutputStream out new FileOutputStream(D:\\poiword\\create_table1.docx);document.write(out);out.close();document.close();}
}生成结果
2.2 生成固定行列的table
//生成3行5列的table XWPFTable table2 document.createTable(3, 5);
测试Demo
package com.poi.word.demo;import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;import java.io.FileOutputStream;public class CreateTableDemo2 {public static void main(String[] args) throws Exception {XWPFDocument document new XWPFDocument();XWPFTable table2 document.createTable(3, 5);table2.setWidth(100%);for(int i0; i3; i){XWPFTableRow t2tRow table2.getRow(i);for(int j0; j5; j){if(i1){XWPFRun t2Row2Run t2tRow.getCell(j).getParagraphs().get(0).createRun();t2Row2Run.setFontSize(10);t2Row2Run.setBold(true);t2Row2Run.setText(第(i1)行第(j1)列);}else{t2tRow.getCell(j).setText(第(i1)行第(j1)列);}}}FileOutputStream out new FileOutputStream(D:\\poiword\\create_table2.docx);document.write(out);out.close();document.close();}
}
生成结果
2.3 table合并列
row4.getCell(0).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
row4.getCell(1).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);2.4 创建多个table存在的问题 创建的两个table输出时候合并成了一个table而且第一个table的宽度也变成了第二个table前两列的宽度。
解决方法
添加空段落 XWPFParagraph paragraph1 document.createParagraph();添加分页会让两个table在不同的页面 document.createParagraph().setPageBreak(true); document.createParagraph().createRun().addBreak(BreakType.PAGE);//推荐方式
添加空段落的解决方法Demo:
package com.poi.word.demo;import org.apache.poi.xwpf.usermodel.*;import java.io.FileOutputStream;public class GenWordTableDemo2 {public static void main(String[] args) throws Exception {XWPFDocument document new XWPFDocument();//默认创建一行一列tableXWPFTable table document.createTable();table.setWidth(100%);XWPFTableRow first_row table.getRow(0);XWPFTableCell first_Row_first_Cell first_row.getCell(0);first_Row_first_Cell.setText(我是第一行第一列);//第一行添加一列first_row.addNewTableCell().setText(我是第一行第二列);//创建第二行XWPFTableRow snd_row table.createRow();snd_row.getCell(0).setText(第二行第一列);snd_row.getCell(1).setText(第二行第二列);//创建第三行XWPFTableRow trd_row table.createRow();XWPFParagraph trd_row_first_paragraph trd_row.getCell(0).getParagraphs().get(0);XWPFRun trdRowFirstCellRun trd_row_first_paragraph.createRun();trdRowFirstCellRun.setFontSize(14);trdRowFirstCellRun.setBold(true);trdRowFirstCellRun.setText(第三行第一列);trd_row.getCell(1).setText(第三行第二列);XWPFParagraph paragraph1 document.createParagraph();//分页的两种方式//document.createParagraph().setPageBreak(true);//document.createParagraph().createRun().addBreak(BreakType.PAGE);//推荐方式/*** 第2个table*/XWPFTable table2 document.createTable(3, 5);table2.setWidth(100%);XWPFTableRow t2FirstRow table2.getRow(0);for(int i0; i3; i){XWPFTableRow t2tRow table2.getRow(i);for(int j0; j5; j){if(i1){XWPFRun t2Row2Run t2tRow.getCell(j).getParagraphs().get(0).createRun();t2Row2Run.setFontSize(10);t2Row2Run.setBold(true);t2Row2Run.setText(第(i1)行第(j1)列);}else{t2tRow.getCell(j).setText(第(i1)行第(j1)列);}}}FileOutputStream out new FileOutputStream(D:\\poiword\\gen_word2.docx);document.write(out);out.close();document.close();}
}
效果