直接上代码:
import java.io.FileOutputStream; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; import org.apache.poi.xwpf.usermodel.BreakType; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; 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.CTBorder; 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.CTTblBorders; 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.STBorder; 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_表格边框相关_S2_Test { public static void main(String[] args) throws Exception { POI_表格边框相关_S2_Test t=new POI_表格边框相关_S2_Test(); XWPFDocument document = new XWPFDocument(); t.createSimpleTableWithBdColor(document); t.addNewPage(document, BreakType.TEXT_WRAPPING); t.createSimpleTableNormal(document); t.addNewPage(document, BreakType.TEXT_WRAPPING); t.createSimpleTableWithNotBd(document); t.saveDocument(document, "f:/saveFile/temp/sys_"+ System.currentTimeMillis() + ".docx"); } //表格自定义边框 请忽略这么丑的颜色样式,主要说明可以自定义样式 public void createSimpleTableWithBdColor(XWPFDocument doc) throws Exception { List<String> columnList = new ArrayList<String>(); columnList.add("序号"); columnList.add("姓名信息|姓甚|名谁"); columnList.add("名刺信息|籍贯|营生"); XWPFTable table = doc.createTable(2,5); CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders(); CTBorder hBorder=borders.addNewInsideH(); hBorder.setVal(STBorder.Enum.forString("dashed")); hBorder.setSz(new BigInteger("1")); hBorder.setColor("0000FF"); CTBorder vBorder=borders.addNewInsideV(); vBorder.setVal(STBorder.Enum.forString("dotted")); vBorder.setSz(new BigInteger("1")); vBorder.setColor("00FF00"); CTBorder lBorder=borders.addNewLeft(); lBorder.setVal(STBorder.Enum.forString("double")); lBorder.setSz(new BigInteger("1")); lBorder.setColor("3399FF"); CTBorder rBorder=borders.addNewRight(); rBorder.setVal(STBorder.Enum.forString("single")); rBorder.setSz(new BigInteger("1")); rBorder.setColor("F2B11F"); CTBorder tBorder=borders.addNewTop(); tBorder.setVal(STBorder.Enum.forString("thick")); tBorder.setSz(new BigInteger("1")); tBorder.setColor("C3599D"); CTBorder bBorder=borders.addNewBottom(); bBorder.setVal(STBorder.Enum.forString("wave")); bBorder.setSz(new BigInteger("1")); bBorder.setColor("BF6BCC"); 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("8000")); tblWidth.setType(STTblWidth.DXA); XWPFTableRow firstRow=null; XWPFTableRow secondRow=null; XWPFTableCell firstCell=null; XWPFTableCell secondCell=null; for(int i=0;i<2;i++){ firstRow=table.getRow(i); firstRow.setHeight(380); for(int j=0;j<5;j++){ firstCell=firstRow.getCell(j); setCellText(firstCell, "测试", "FFFFC9", 1600); } } firstRow=table.insertNewTableRow(0); secondRow=table.insertNewTableRow(1); firstRow.setHeight(380); secondRow.setHeight(380); for(String str:columnList){ if(str.indexOf("|") == -1){ firstCell=firstRow.addNewTableCell(); secondCell=secondRow.addNewTableCell(); createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART); createVSpanCell(secondCell, "", "CCCCCC", 1600,null); }else{ String[] strArr=str.split("\\|"); firstCell=firstRow.addNewTableCell(); createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART); for(int i=1;i<strArr.length-1;i++){ firstCell=firstRow.addNewTableCell(); createHSpanCell(firstCell, "","CCCCCC",1600,null); } for(int i=1;i<strArr.length;i++){ secondCell=secondRow.addNewTableCell(); setCellText(secondCell, strArr[i], "CCCCCC", 1600); } } } } //表格正常边框 public void createSimpleTableNormal(XWPFDocument doc) throws Exception { List<String> columnList = new ArrayList<String>(); columnList.add("序号"); columnList.add("姓名信息|姓甚|名谁"); columnList.add("名刺信息|籍贯|营生"); XWPFTable table = doc.createTable(2,5); 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("8000")); tblWidth.setType(STTblWidth.DXA); XWPFTableRow firstRow=null; XWPFTableRow secondRow=null; XWPFTableCell firstCell=null; XWPFTableCell secondCell=null; for(int i=0;i<2;i++){ firstRow=table.getRow(i); firstRow.setHeight(380); for(int j=0;j<5;j++){ firstCell=firstRow.getCell(j); setCellText(firstCell, "测试", "FFFFC9", 1600); } } firstRow=table.insertNewTableRow(0); secondRow=table.insertNewTableRow(1); firstRow.setHeight(380); secondRow.setHeight(380); for(String str:columnList){ if(str.indexOf("|") == -1){ firstCell=firstRow.addNewTableCell(); secondCell=secondRow.addNewTableCell(); createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART); createVSpanCell(secondCell, "", "CCCCCC", 1600,null); }else{ String[] strArr=str.split("\\|"); firstCell=firstRow.addNewTableCell(); createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART); for(int i=1;i<strArr.length-1;i++){ firstCell=firstRow.addNewTableCell(); createHSpanCell(firstCell, "","CCCCCC",1600,null); } for(int i=1;i<strArr.length;i++){ secondCell=secondRow.addNewTableCell(); setCellText(secondCell, strArr[i], "CCCCCC", 1600); } } } } //表格无边框 public void createSimpleTableWithNotBd(XWPFDocument doc) throws Exception { List<String> columnList = new ArrayList<String>(); columnList.add("序号"); columnList.add("姓名信息|姓甚|名谁"); columnList.add("名刺信息|籍贯|营生"); XWPFTable table = doc.createTable(2,5); CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders(); CTBorder hBorder=borders.addNewInsideH(); hBorder.setVal(STBorder.Enum.forString("none")); hBorder.setSz(new BigInteger("1")); hBorder.setColor("0000FF"); CTBorder vBorder=borders.addNewInsideV(); vBorder.setVal(STBorder.Enum.forString("none")); vBorder.setSz(new BigInteger("1")); vBorder.setColor("00FF00"); CTBorder lBorder=borders.addNewLeft(); lBorder.setVal(STBorder.Enum.forString("none")); lBorder.setSz(new BigInteger("1")); lBorder.setColor("3399FF"); CTBorder rBorder=borders.addNewRight(); rBorder.setVal(STBorder.Enum.forString("none")); rBorder.setSz(new BigInteger("1")); rBorder.setColor("F2B11F"); CTBorder tBorder=borders.addNewTop(); tBorder.setVal(STBorder.Enum.forString("none")); tBorder.setSz(new BigInteger("1")); tBorder.setColor("C3599D"); CTBorder bBorder=borders.addNewBottom(); bBorder.setVal(STBorder.Enum.forString("none")); bBorder.setSz(new BigInteger("1")); bBorder.setColor("F7E415"); 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("8000")); tblWidth.setType(STTblWidth.DXA); XWPFTableRow firstRow=null; XWPFTableRow secondRow=null; XWPFTableCell firstCell=null; XWPFTableCell secondCell=null; for(int i=0;i<2;i++){ firstRow=table.getRow(i); firstRow.setHeight(380); for(int j=0;j<5;j++){ firstCell=firstRow.getCell(j); setCellText(firstCell, "测试", "FFFFC9", 1600); } } firstRow=table.insertNewTableRow(0); secondRow=table.insertNewTableRow(1); firstRow.setHeight(380); secondRow.setHeight(380); for(String str:columnList){ if(str.indexOf("|") == -1){ firstCell=firstRow.addNewTableCell(); secondCell=secondRow.addNewTableCell(); createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART); createVSpanCell(secondCell, "", "CCCCCC", 1600,null); }else{ String[] strArr=str.split("\\|"); firstCell=firstRow.addNewTableCell(); createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART); for(int i=1;i<strArr.length-1;i++){ firstCell=firstRow.addNewTableCell(); createHSpanCell(firstCell, "","CCCCCC",1600,null); } for(int i=1;i<strArr.length;i++){ secondCell=secondRow.addNewTableCell(); setCellText(secondCell, strArr[i], "CCCCCC", 1600); } } } } 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); } public void createHSpanCell(XWPFTableCell cell,String value, String bgcolor, int width,STMerge.Enum stMerge){ CTTc cttc = cell.getCTTc(); CTTcPr cellPr = cttc.addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(width)); cell.setColor(bgcolor); cellPr.addNewHMerge().setVal(stMerge); cellPr.addNewVAlign().setVal(STVerticalJc.CENTER); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cttc.getPList().get(0).addNewR().addNewT().setStringValue(value); } public void createVSpanCell(XWPFTableCell cell,String value, String bgcolor, int width,STMerge.Enum stMerge){ CTTc cttc = cell.getCTTc(); CTTcPr cellPr = cttc.addNewTcPr(); cellPr.addNewTcW().setW(BigInteger.valueOf(width)); cell.setColor(bgcolor); cellPr.addNewVMerge().setVal(stMerge); cellPr.addNewVAlign().setVal(STVerticalJc.CENTER); cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER); cttc.getPList().get(0).addNewR().addNewT().setStringValue(value); } public void addNewPage(XWPFDocument document,BreakType breakType){ XWPFParagraph xp = document.createParagraph(); xp.createRun().addBreak(breakType); } public void saveDocument(XWPFDocument document,String savePath) throws Exception{ FileOutputStream fos = new FileOutputStream(savePath); document.write(fos); fos.close(); } }
结果如下:
有关单元格合并的内容,可以参考我的另一篇文章:http://53873039oycg.iteye.com/blog/2152009
全文完。
相关推荐
poi将word里面的内容转换成html、包括word里面字体的大小、表格的处理、还有图片、段落等等...
poi.word poi.word poi操作word表格
POI导出Word包含页眉 页脚的设置,标题的设置以及显示显示设置,包括POI操作Word完整的jar包
java使用poi操作.doc word模板替换,循环插入表格
java Apache poi word模板 word表格模板 word报表.zip
POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法
资源描述了poi对于word的操作,poi合并word文档,poi设置word页码,内含maven依赖文档和完整代码,是一个完整工程,打开即可用
值得注意的是 POI 在读取 word 文件的时候不会读取 word 文件中的图片信息 还有就是对于 2007 版的 word docx 如果 word 文件中有表格 所有表格中的数据都会在读取出来的字符串的最后 ">这是一个POI读取word 2003 和...
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
POI 替换word2007文本框的值POI,包含word表中的文本框的值,定义好word模板即可替换word中的值。
POI报表Word导出
POI解析word2007文本及图片(已测试).doc
使用poi将Word2003和Word2007内容(包括图片)完整转换成html。内附核心代码,成功事例,及完整Jar包。不用再东查西找拼凑Jar包了!
Apache poi 根据word模板生成word报表 替换 循环列表 图片,代码调试过了,修改相应的图片位置,word模板路径即可运行
POI word2007依赖jar包 poi-ooxml-3.9-20121203.jar poi-ooxml-schemas-3.9-20121203.jar poi-scratchpad-3.9-20121203.jar ....
下载后,运行 可以 Test1中的main方法,注意里面的word模版地址要修改下。 test1.java主要是针对word的书签操作 MSWordTool.java主要是针对word的列表书签的操作,相对来说比较复杂,具体看个人需求。
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701
附件包含一个web项目,是一个简单的解析excel和word的例子,项目中包含jar poi解析excel、word2007,2010等版本
NULL 博文链接:https://53873039oycg.iteye.com/blog/2187388
apach poi 读取word 文档 jar 包。 博文链接:https://wxinpeng.iteye.com/blog/231881