1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 简单]poi word设置表格边框样式

简单]poi word设置表格边框样式

时间:2023-06-14 08:32:08

相关推荐

简单]poi word设置表格边框样式

直接上代码:

Java代码

importjava.io.FileOutputStream;importjava.math.BigInteger;importjava.util.ArrayList;importjava.util.List;importorg.apache.poi.xwpf.usermodel.BreakType;importorg.apache.poi.xwpf.usermodel.XWPFDocument;importorg.apache.poi.xwpf.usermodel.XWPFParagraph;importorg.apache.poi.xwpf.usermodel.XWPFTable;importorg.apache.poi.xwpf.usermodel.XWPFTableCell;importorg.apache.poi.xwpf.usermodel.XWPFTableRow;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTBorder;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTJc;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTShd;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTTbl;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTTblBorders;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTTblPr;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTTblWidth;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTTc;importorg.openxmlformats.schemas.wordprocessingml.x.main.CTTcPr;importorg.openxmlformats.schemas.wordprocessingml.x.main.STBorder;importorg.openxmlformats.schemas.wordprocessingml.x.main.STJc;importorg.openxmlformats.schemas.wordprocessingml.x.main.STMerge;importorg.openxmlformats.schemas.wordprocessingml.x.main.STTblWidth;importorg.openxmlformats.schemas.wordprocessingml.x.main.STVerticalJc;publicclassPOI_表格边框相关_S2_Test{publicstaticvoidmain(String[]args)throwsException{POI_表格边框相关_S2_Testt=newPOI_表格边框相关_S2_Test();XWPFDocumentdocument=newXWPFDocument();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");}//表格自定义边框请忽略这么丑的颜色样式,主要说明可以自定义样式publicvoidcreateSimpleTableWithBdColor(XWPFDocumentdoc)throwsException{List<String>columnList=newArrayList<String>();columnList.add("序号");columnList.add("姓名信息|姓甚|名谁");columnList.add("名刺信息|籍贯|营生");XWPFTabletable=doc.createTable(2,5);CTTblBordersborders=table.getCTTbl().getTblPr().addNewTblBorders();CTBorderhBorder=borders.addNewInsideH();hBorder.setVal(STBorder.Enum.forString("dashed"));hBorder.setSz(newBigInteger("1"));hBorder.setColor("0000FF");CTBordervBorder=borders.addNewInsideV();vBorder.setVal(STBorder.Enum.forString("dotted"));vBorder.setSz(newBigInteger("1"));vBorder.setColor("00FF00");CTBorderlBorder=borders.addNewLeft();lBorder.setVal(STBorder.Enum.forString("double"));lBorder.setSz(newBigInteger("1"));lBorder.setColor("3399FF");CTBorderrBorder=borders.addNewRight();rBorder.setVal(STBorder.Enum.forString("single"));rBorder.setSz(newBigInteger("1"));rBorder.setColor("F2B11F");CTBordertBorder=borders.addNewTop();tBorder.setVal(STBorder.Enum.forString("thick"));tBorder.setSz(newBigInteger("1"));tBorder.setColor("C3599D");CTBorderbBorder=borders.addNewBottom();bBorder.setVal(STBorder.Enum.forString("wave"));bBorder.setSz(newBigInteger("1"));bBorder.setColor("BF6BCC");CTTblttbl=table.getCTTbl();CTTblPrtblPr=ttbl.getTblPr()==null?ttbl.addNewTblPr():ttbl.getTblPr();CTTblWidthtblWidth=tblPr.isSetTblW()?tblPr.getTblW():tblPr.addNewTblW();CTJccTJc=tblPr.addNewJc();cTJc.setVal(STJc.Enum.forString("center"));tblWidth.setW(newBigInteger("8000"));tblWidth.setType(STTblWidth.DXA);XWPFTableRowfirstRow=null;XWPFTableRowsecondRow=null;XWPFTableCellfirstCell=null;XWPFTableCellsecondCell=null;for(inti=0;i<2;i++){firstRow=table.getRow(i);firstRow.setHeight(380);for(intj=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(Stringstr: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(inti=1;i<strArr.length-1;i++){firstCell=firstRow.addNewTableCell();createHSpanCell(firstCell,"","CCCCCC",1600,null);}for(inti=1;i<strArr.length;i++){secondCell=secondRow.addNewTableCell();setCellText(secondCell,strArr[i],"CCCCCC",1600);}}}}//表格正常边框publicvoidcreateSimpleTableNormal(XWPFDocumentdoc)throwsException{List<String>columnList=newArrayList<String>();columnList.add("序号");columnList.add("姓名信息|姓甚|名谁");columnList.add("名刺信息|籍贯|营生");XWPFTabletable=doc.createTable(2,5);CTTblttbl=table.getCTTbl();CTTblPrtblPr=ttbl.getTblPr()==null?ttbl.addNewTblPr():ttbl.getTblPr();CTTblWidthtblWidth=tblPr.isSetTblW()?tblPr.getTblW():tblPr.addNewTblW();CTJccTJc=tblPr.addNewJc();cTJc.setVal(STJc.Enum.forString("center"));tblWidth.setW(newBigInteger("8000"));tblWidth.setType(STTblWidth.DXA);XWPFTableRowfirstRow=null;XWPFTableRowsecondRow=null;XWPFTableCellfirstCell=null;XWPFTableCellsecondCell=null;for(inti=0;i<2;i++){firstRow=table.getRow(i);firstRow.setHeight(380);for(intj=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(Stringstr: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(inti=1;i<strArr.length-1;i++){firstCell=firstRow.addNewTableCell();createHSpanCell(firstCell,"","CCCCCC",1600,null);}for(inti=1;i<strArr.length;i++){secondCell=secondRow.addNewTableCell();setCellText(secondCell,strArr[i],"CCCCCC",1600);}}}}//表格无边框publicvoidcreateSimpleTableWithNotBd(XWPFDocumentdoc)throwsException{List<String>columnList=newArrayList<String>();columnList.add("序号");columnList.add("姓名信息|姓甚|名谁");columnList.add("名刺信息|籍贯|营生");XWPFTabletable=doc.createTable(2,5);CTTblBordersborders=table.getCTTbl().getTblPr().addNewTblBorders();CTBorderhBorder=borders.addNewInsideH();hBorder.setVal(STBorder.Enum.forString("none"));hBorder.setSz(newBigInteger("1"));hBorder.setColor("0000FF");CTBordervBorder=borders.addNewInsideV();vBorder.setVal(STBorder.Enum.forString("none"));vBorder.setSz(newBigInteger("1"));vBorder.setColor("00FF00");CTBorderlBorder=borders.addNewLeft();lBorder.setVal(STBorder.Enum.forString("none"));lBorder.setSz(newBigInteger("1"));lBorder.setColor("3399FF");CTBorderrBorder=borders.addNewRight();rBorder.setVal(STBorder.Enum.forString("none"));rBorder.setSz(newBigInteger("1"));rBorder.setColor("F2B11F");CTBordertBorder=borders.addNewTop();tBorder.setVal(STBorder.Enum.forString("none"));tBorder.setSz(newBigInteger("1"));tBorder.setColor("C3599D");CTBorderbBorder=borders.addNewBottom();bBorder.setVal(STBorder.Enum.forString("none"));bBorder.setSz(newBigInteger("1"));bBorder.setColor("F7E415");CTTblttbl=table.getCTTbl();CTTblPrtblPr=ttbl.getTblPr()==null?ttbl.addNewTblPr():ttbl.getTblPr();CTTblWidthtblWidth=tblPr.isSetTblW()?tblPr.getTblW():tblPr.addNewTblW();CTJccTJc=tblPr.addNewJc();cTJc.setVal(STJc.Enum.forString("center"));tblWidth.setW(newBigInteger("8000"));tblWidth.setType(STTblWidth.DXA);XWPFTableRowfirstRow=null;XWPFTableRowsecondRow=null;XWPFTableCellfirstCell=null;XWPFTableCellsecondCell=null;for(inti=0;i<2;i++){firstRow=table.getRow(i);firstRow.setHeight(380);for(intj=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(Stringstr: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(inti=1;i<strArr.length-1;i++){firstCell=firstRow.addNewTableCell();createHSpanCell(firstCell,"","CCCCCC",1600,null);}for(inti=1;i<strArr.length;i++){secondCell=secondRow.addNewTableCell();setCellText(secondCell,strArr[i],"CCCCCC",1600);}}}}publicvoidsetCellText(XWPFTableCellcell,Stringtext,Stringbgcolor,intwidth){CTTccttc=cell.getCTTc();CTTcPrcellPr=cttc.addNewTcPr();cellPr.addNewTcW().setW(BigInteger.valueOf(width));//cell.setColor(bgcolor);CTTcPrctPr=cttc.addNewTcPr();CTShdctshd=ctPr.addNewShd();ctshd.setFill(bgcolor);ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);cell.setText(text);}publicvoidcreateHSpanCell(XWPFTableCellcell,Stringvalue,Stringbgcolor,intwidth,STMerge.EnumstMerge){CTTccttc=cell.getCTTc();CTTcPrcellPr=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);}publicvoidcreateVSpanCell(XWPFTableCellcell,Stringvalue,Stringbgcolor,intwidth,STMerge.EnumstMerge){CTTccttc=cell.getCTTc();CTTcPrcellPr=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);}publicvoidaddNewPage(XWPFDocumentdocument,BreakTypebreakType){XWPFParagraphxp=document.createParagraph();xp.createRun().addBreak(breakType);}publicvoidsaveDocument(XWPFDocumentdocument,StringsavePath)throwsException{FileOutputStreamfos=newFileOutputStream(savePath);document.write(fos);fos.close();}}

结果如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。