代码参考了http://stackoverflow.com/questions/24907541/row-span-with-xwpftable,代码如下:
import java.io.FileOutputStream; import java.math.BigInteger; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc; public class POI_表格合并_S3_Test { public static void main(String[] args) throws Exception { POI_表格合并_S3_Test t=new POI_表格合并_S3_Test(); XWPFDocument document = new XWPFDocument(); t.megerTableCell(document); t.saveDocument(document, "f:/saveFile/temp/sys_"+ System.currentTimeMillis() + ".docx"); } public void megerTableCell(XWPFDocument document) { XWPFTable table1 = document.createTable(6, 8); setTableWidth(table1, "8000"); fillTable(table1); mergeCellsVertically(table1, 1, 1,4); mergeCellsVertically(table1, 4, 2, 4); mergeCellsHorizontal(table1,0,3,5); mergeCellsHorizontal(table1,2,2,3); mergeCellsHorizontal(table1,2,6,7); } public void fillTable(XWPFTable table) { for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) { XWPFTableRow row = table.getRow(rowIndex); row.setHeight(380); for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) { XWPFTableCell cell = row.getCell(colIndex); if(rowIndex%2==0){ setCellText(cell, " cell " + rowIndex + colIndex + " ", "D4DBED", 1000); }else{ setCellText(cell, " cell " + rowIndex + colIndex + " ", "AEDE72", 1000); } } } } public void setCellText(XWPFTableCell cell,String text, String bgcolor, int width) { CTTc cttc = cell.getCTTc(); CTTcPr cellPr = cttc.addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(width)); //cell.setColor(bgcolor); CTTcPr ctPr = cttc.addNewTcPr(); CTShd ctshd = ctPr.addNewShd(); ctshd.setFill(bgcolor); ctPr.addNewVAlign().setVal(STVerticalJc.CENTER); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cell.setText(text); } /** * @Description: 跨列合并 */ public void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) { for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) { XWPFTableCell cell = table.getRow(row).getCell(cellIndex); if ( cellIndex == fromCell ) { // The first merged cell is set with RESTART merge value cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE); } } } /** * @Description: 跨行合并 * @see http://stackoverflow.com/questions/24907541/row-span-with-xwpftable */ public void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) { XWPFTableCell cell = table.getRow(rowIndex).getCell(col); if ( rowIndex == fromRow ) { // The first merged cell is set with RESTART merge value cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART); } else { // Cells which join (merge) the first one, are set with CONTINUE cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE); } } } public void setTableWidth(XWPFTable table,String width){ CTTbl ttbl = table.getCTTbl(); CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr(); CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW(); CTJc cTJc=tblPr.addNewJc(); cTJc.setVal(STJc.Enum.forString("center")); tblWidth.setW(new BigInteger(width)); tblWidth.setType(STTblWidth.DXA); } public void saveDocument(XWPFDocument document, String savePath) throws Exception { FileOutputStream fos = new FileOutputStream(savePath); document.write(fos); fos.close(); } }
结果如下:
全文完。
相关推荐
java poi 填充word(合并单元格,添加图片,设置字号)(csdn)————程序
POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法
java运用poi操作word,替换word中的占位符填充数据,并将多个word合并成一个word,附件是java代码和相关jar包,以及测试模板,需要注意是,只适用2007以上的word
easyuiPoi 通过模板导出Excel、支持纵向合并单元格(合并重复内容)。压缩包包含公共工具类、示例模板、调用截图。
poi写word工具类,包括添加标题,插入表格,合并单元格,插入图片,自动生成目录;代码见 https://blog.csdn.net/zheng_chang_wei/article/details/89448055
word-export ...1.SpringBoot + poi-tl 根据word模板动态生成word(文本、动态行表格、循环列表下的动态行表格、合并单元格) 2.SpringBoot + easypoi 根据word模板动态生成word(文本、表格、图片)
POI ,xdocreport框架是一个半成品,往往使用默认的解析结果无法满足业务需求,花了很长时间和心血研究了其源码,修复了以下问题 问题1: xdocreport能解析word转html,但解析出来的是基于word的整套html,往往我们的...
通过三种不同的方式(freemarker+jfreechart、xDoc、POI),根据模板创建Word文件。可以动态修改Word中表格(table)、图表(chart)等数据。导入工程后,可直接运行xxxDemo文件,查看文档生成效果。详见压缩包中的...
这是一个关于poi导出Excel的详细的例子。里面涉及到poi操作Excel的详尽引用,包括设置行的高度、列的宽度、合并单元格、设置单元格的样式等等,一个例子足以掌握poi导出Excel的操作。
不使用书签读取word中表格中的信息,对于合并和拆分单元格的情况也适用,但是要准确的定位。
C#操作word文档。包括创建、保存、文字、图片、表格、页眉、页脚等。其中表格还包括边框,合并单元格,插入图片。其他操作必须先创建word。代码块都有,具体应用需要修改。
20.3 应用POI组件导出到Word 772 第21章 JSP操作Excel 775 21.1 应用JXL组件操作Excel 776 21.2 应用POI组件操作Excel 807 第22章 报表与打印 829 22.1 Web打印 830 22.2 利用Word打印报表 833 22.3 利用Excel打印...
20.3 应用POI组件导出到Word 772 第21章 JSP操作Excel 775 21.1 应用JXL组件操作Excel 776 21.2 应用POI组件操作Excel 807 第22章 报表与打印 829 22.1 Web打印 830 22.2 利用Word打印报表 833 22.3 利用Excel打印...