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

 直接上代码:

       

Java代码 

 

  1. import java.io.FileOutputStream;  
  2. import java.math.BigInteger;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.poi.xwpf.usermodel.BreakType;  
  7. import org.apache.poi.xwpf.usermodel.XWPFDocument;  
  8. import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
  9. import org.apache.poi.xwpf.usermodel.XWPFTable;  
  10. import org.apache.poi.xwpf.usermodel.XWPFTableCell;  
  11. import org.apache.poi.xwpf.usermodel.XWPFTableRow;  
  12. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;  
  13. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;  
  14. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;  
  15. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;  
  16. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;  
  17. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;  
  18. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;  
  19. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;  
  20. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;  
  21. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;  
  22. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;  
  23. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;  
  24. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;  
  25. import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;  
  26.   
  27. public class POI_表格边框相关_S2_Test {  
  28.     public static void main(String[] args) throws Exception {  
  29.         POI_表格边框相关_S2_Test t=new POI_表格边框相关_S2_Test();  
  30.         XWPFDocument document = new XWPFDocument();  
  31.         t.createSimpleTableWithBdColor(document);  
  32.         t.addNewPage(document, BreakType.TEXT_WRAPPING);  
  33.         t.createSimpleTableNormal(document);  
  34.         t.addNewPage(document, BreakType.TEXT_WRAPPING);  
  35.         t.createSimpleTableWithNotBd(document);  
  36.         t.saveDocument(document, "f:/saveFile/temp/sys_"+ System.currentTimeMillis() + ".docx");  
  37.     }  
  38.       
  39.     //表格自定义边框 请忽略这么丑的颜色样式,主要说明可以自定义样式  
  40.     public  void createSimpleTableWithBdColor(XWPFDocument doc) throws Exception {  
  41.         List<String> columnList = new ArrayList<String>();  
  42.         columnList.add("序号");  
  43.         columnList.add("姓名信息|姓甚|名谁");  
  44.         columnList.add("名刺信息|籍贯|营生");  
  45.         XWPFTable table = doc.createTable(2,5);  
  46.           
  47.         CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();  
  48.         CTBorder hBorder=borders.addNewInsideH();  
  49.         hBorder.setVal(STBorder.Enum.forString("dashed"));  
  50.         hBorder.setSz(new BigInteger("1"));  
  51.         hBorder.setColor("0000FF");  
  52.           
  53.         CTBorder vBorder=borders.addNewInsideV();  
  54.         vBorder.setVal(STBorder.Enum.forString("dotted"));  
  55.         vBorder.setSz(new BigInteger("1"));  
  56.         vBorder.setColor("00FF00");  
  57.           
  58.         CTBorder lBorder=borders.addNewLeft();  
  59.         lBorder.setVal(STBorder.Enum.forString("double"));  
  60.         lBorder.setSz(new BigInteger("1"));  
  61.         lBorder.setColor("3399FF");  
  62.           
  63.         CTBorder rBorder=borders.addNewRight();  
  64.         rBorder.setVal(STBorder.Enum.forString("single"));  
  65.         rBorder.setSz(new BigInteger("1"));  
  66.         rBorder.setColor("F2B11F");  
  67.           
  68.         CTBorder tBorder=borders.addNewTop();  
  69.         tBorder.setVal(STBorder.Enum.forString("thick"));  
  70.         tBorder.setSz(new BigInteger("1"));  
  71.         tBorder.setColor("C3599D");  
  72.           
  73.         CTBorder bBorder=borders.addNewBottom();  
  74.         bBorder.setVal(STBorder.Enum.forString("wave"));  
  75.         bBorder.setSz(new BigInteger("1"));  
  76.         bBorder.setColor("BF6BCC");  
  77.           
  78.         CTTbl ttbl = table.getCTTbl();  
  79.         CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();  
  80.         CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();  
  81.         CTJc cTJc=tblPr.addNewJc();  
  82.         cTJc.setVal(STJc.Enum.forString("center"));  
  83.         tblWidth.setW(new BigInteger("8000"));  
  84.         tblWidth.setType(STTblWidth.DXA);  
  85.           
  86.         XWPFTableRow firstRow=null;  
  87.         XWPFTableRow secondRow=null;  
  88.         XWPFTableCell firstCell=null;  
  89.         XWPFTableCell secondCell=null;  
  90.           
  91.         for(int i=0;i<2;i++){  
  92.             firstRow=table.getRow(i);  
  93.             firstRow.setHeight(380);  
  94.             for(int j=0;j<5;j++){  
  95.                 firstCell=firstRow.getCell(j);  
  96.                 setCellText(firstCell, "测试", "FFFFC9", 1600);  
  97.             }  
  98.         }  
  99.           
  100.         firstRow=table.insertNewTableRow(0);  
  101.         secondRow=table.insertNewTableRow(1);  
  102.         firstRow.setHeight(380);  
  103.         secondRow.setHeight(380);  
  104.         for(String str:columnList){  
  105.             if(str.indexOf("|") == -1){  
  106.                 firstCell=firstRow.addNewTableCell();  
  107.                 secondCell=secondRow.addNewTableCell();  
  108.                 createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART);  
  109.                 createVSpanCell(secondCell, "", "CCCCCC", 1600,null);  
  110.             }else{  
  111.                 String[] strArr=str.split("\\|");  
  112.                 firstCell=firstRow.addNewTableCell();  
  113.                 createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART);  
  114.                 for(int i=1;i<strArr.length-1;i++){  
  115.                     firstCell=firstRow.addNewTableCell();  
  116.                     createHSpanCell(firstCell, "","CCCCCC",1600,null);  
  117.                 }  
  118.                 for(int i=1;i<strArr.length;i++){  
  119.                     secondCell=secondRow.addNewTableCell();  
  120.                     setCellText(secondCell, strArr[i], "CCCCCC", 1600);  
  121.                 }  
  122.             }  
  123.         }  
  124.     }  
  125.       
  126.     //表格正常边框  
  127.     public  void createSimpleTableNormal(XWPFDocument doc) throws Exception {  
  128.         List<String> columnList = new ArrayList<String>();  
  129.         columnList.add("序号");  
  130.         columnList.add("姓名信息|姓甚|名谁");  
  131.         columnList.add("名刺信息|籍贯|营生");  
  132.         XWPFTable table = doc.createTable(2,5);  
  133.           
  134.         CTTbl ttbl = table.getCTTbl();  
  135.         CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();  
  136.         CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();  
  137.         CTJc cTJc=tblPr.addNewJc();  
  138.         cTJc.setVal(STJc.Enum.forString("center"));  
  139.         tblWidth.setW(new BigInteger("8000"));  
  140.         tblWidth.setType(STTblWidth.DXA);  
  141.           
  142.         XWPFTableRow firstRow=null;  
  143.         XWPFTableRow secondRow=null;  
  144.         XWPFTableCell firstCell=null;  
  145.         XWPFTableCell secondCell=null;  
  146.           
  147.         for(int i=0;i<2;i++){  
  148.             firstRow=table.getRow(i);  
  149.             firstRow.setHeight(380);  
  150.             for(int j=0;j<5;j++){  
  151.                 firstCell=firstRow.getCell(j);  
  152.                 setCellText(firstCell, "测试", "FFFFC9", 1600);  
  153.             }  
  154.         }  
  155.           
  156.         firstRow=table.insertNewTableRow(0);  
  157.         secondRow=table.insertNewTableRow(1);  
  158.         firstRow.setHeight(380);  
  159.         secondRow.setHeight(380);  
  160.         for(String str:columnList){  
  161.             if(str.indexOf("|") == -1){  
  162.                 firstCell=firstRow.addNewTableCell();  
  163.                 secondCell=secondRow.addNewTableCell();  
  164.                 createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART);  
  165.                 createVSpanCell(secondCell, "", "CCCCCC", 1600,null);  
  166.             }else{  
  167.                 String[] strArr=str.split("\\|");  
  168.                 firstCell=firstRow.addNewTableCell();  
  169.                 createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART);  
  170.                 for(int i=1;i<strArr.length-1;i++){  
  171.                     firstCell=firstRow.addNewTableCell();  
  172.                     createHSpanCell(firstCell, "","CCCCCC",1600,null);  
  173.                 }  
  174.                 for(int i=1;i<strArr.length;i++){  
  175.                     secondCell=secondRow.addNewTableCell();  
  176.                     setCellText(secondCell, strArr[i], "CCCCCC", 1600);  
  177.                 }  
  178.             }  
  179.         }  
  180.     }  
  181.       
  182.     //表格无边框  
  183.     public  void createSimpleTableWithNotBd(XWPFDocument doc) throws Exception {  
  184.         List<String> columnList = new ArrayList<String>();  
  185.         columnList.add("序号");  
  186.         columnList.add("姓名信息|姓甚|名谁");  
  187.         columnList.add("名刺信息|籍贯|营生");  
  188.         XWPFTable table = doc.createTable(2,5);  
  189.           
  190.         CTTblBorders borders=table.getCTTbl().getTblPr().addNewTblBorders();  
  191.         CTBorder hBorder=borders.addNewInsideH();  
  192.         hBorder.setVal(STBorder.Enum.forString("none"));  
  193.         hBorder.setSz(new BigInteger("1"));  
  194.         hBorder.setColor("0000FF");  
  195.           
  196.         CTBorder vBorder=borders.addNewInsideV();  
  197.         vBorder.setVal(STBorder.Enum.forString("none"));  
  198.         vBorder.setSz(new BigInteger("1"));  
  199.         vBorder.setColor("00FF00");  
  200.           
  201.         CTBorder lBorder=borders.addNewLeft();  
  202.         lBorder.setVal(STBorder.Enum.forString("none"));  
  203.         lBorder.setSz(new BigInteger("1"));  
  204.         lBorder.setColor("3399FF");  
  205.           
  206.         CTBorder rBorder=borders.addNewRight();  
  207.         rBorder.setVal(STBorder.Enum.forString("none"));  
  208.         rBorder.setSz(new BigInteger("1"));  
  209.         rBorder.setColor("F2B11F");  
  210.           
  211.         CTBorder tBorder=borders.addNewTop();  
  212.         tBorder.setVal(STBorder.Enum.forString("none"));  
  213.         tBorder.setSz(new BigInteger("1"));  
  214.         tBorder.setColor("C3599D");  
  215.           
  216.         CTBorder bBorder=borders.addNewBottom();  
  217.         bBorder.setVal(STBorder.Enum.forString("none"));  
  218.         bBorder.setSz(new BigInteger("1"));  
  219.         bBorder.setColor("F7E415");  
  220.           
  221.         CTTbl ttbl = table.getCTTbl();  
  222.         CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();  
  223.         CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();  
  224.         CTJc cTJc=tblPr.addNewJc();  
  225.         cTJc.setVal(STJc.Enum.forString("center"));  
  226.         tblWidth.setW(new BigInteger("8000"));  
  227.         tblWidth.setType(STTblWidth.DXA);  
  228.           
  229.         XWPFTableRow firstRow=null;  
  230.         XWPFTableRow secondRow=null;  
  231.         XWPFTableCell firstCell=null;  
  232.         XWPFTableCell secondCell=null;  
  233.           
  234.         for(int i=0;i<2;i++){  
  235.             firstRow=table.getRow(i);  
  236.             firstRow.setHeight(380);  
  237.             for(int j=0;j<5;j++){  
  238.                 firstCell=firstRow.getCell(j);  
  239.                 setCellText(firstCell, "测试", "FFFFC9", 1600);  
  240.             }  
  241.         }  
  242.           
  243.         firstRow=table.insertNewTableRow(0);  
  244.         secondRow=table.insertNewTableRow(1);  
  245.         firstRow.setHeight(380);  
  246.         secondRow.setHeight(380);  
  247.         for(String str:columnList){  
  248.             if(str.indexOf("|") == -1){  
  249.                 firstCell=firstRow.addNewTableCell();  
  250.                 secondCell=secondRow.addNewTableCell();  
  251.                 createVSpanCell(firstCell, str,"CCCCCC",1600,STMerge.RESTART);  
  252.                 createVSpanCell(secondCell, "", "CCCCCC", 1600,null);  
  253.             }else{  
  254.                 String[] strArr=str.split("\\|");  
  255.                 firstCell=firstRow.addNewTableCell();  
  256.                 createHSpanCell(firstCell, strArr[0],"CCCCCC",1600,STMerge.RESTART);  
  257.                 for(int i=1;i<strArr.length-1;i++){  
  258.                     firstCell=firstRow.addNewTableCell();  
  259.                     createHSpanCell(firstCell, "","CCCCCC",1600,null);  
  260.                 }  
  261.                 for(int i=1;i<strArr.length;i++){  
  262.                     secondCell=secondRow.addNewTableCell();  
  263.                     setCellText(secondCell, strArr[i], "CCCCCC", 1600);  
  264.                 }  
  265.             }  
  266.         }  
  267.     }  
  268.       
  269.     public  void setCellText(XWPFTableCell cell,String text, String bgcolor, int width) {  
  270.         CTTc cttc = cell.getCTTc();  
  271.         CTTcPr cellPr = cttc.addNewTcPr();  
  272.         cellPr.addNewTcW().setW(BigInteger.valueOf(width));  
  273.         //cell.setColor(bgcolor);  
  274.         CTTcPr ctPr = cttc.addNewTcPr();  
  275.         CTShd ctshd = ctPr.addNewShd();  
  276.         ctshd.setFill(bgcolor);  
  277.         ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);  
  278.         cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);  
  279.         cell.setText(text);  
  280.     }  
  281.     public void createHSpanCell(XWPFTableCell cell,String value, String bgcolor, int width,STMerge.Enum stMerge){  
  282.         CTTc cttc = cell.getCTTc();  
  283.         CTTcPr cellPr = cttc.addNewTcPr();  
  284.         cellPr.addNewTcW().setW(BigInteger.valueOf(width));  
  285.         cell.setColor(bgcolor);  
  286.         cellPr.addNewHMerge().setVal(stMerge);  
  287.         cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);  
  288.         cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);  
  289.         cttc.getPList().get(0).addNewR().addNewT().setStringValue(value);  
  290.     }  
  291.       
  292.     public void createVSpanCell(XWPFTableCell cell,String value, String bgcolor, int width,STMerge.Enum stMerge){  
  293.         CTTc cttc = cell.getCTTc();  
  294.         CTTcPr cellPr = cttc.addNewTcPr();  
  295.         cellPr.addNewTcW().setW(BigInteger.valueOf(width));  
  296.         cell.setColor(bgcolor);  
  297.         cellPr.addNewVMerge().setVal(stMerge);  
  298.         cellPr.addNewVAlign().setVal(STVerticalJc.CENTER);  
  299.         cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);  
  300.         cttc.getPList().get(0).addNewR().addNewT().setStringValue(value);  
  301.     }  
  302.       
  303.     public void addNewPage(XWPFDocument document,BreakType breakType){  
  304.         XWPFParagraph xp = document.createParagraph();  
  305.         xp.createRun().addBreak(breakType);  
  306.     }  
  307.       
  308.     public void saveDocument(XWPFDocument document,String savePath) throws Exception{  
  309.         FileOutputStream fos = new FileOutputStream(savePath);  
  310.         document.write(fos);  
  311.         fos.close();  
  312.     }  
  313.       
  314. }  

    结果如下:

    

猜你喜欢

转载自blog.csdn.net/weixin_41565034/article/details/86361214