POI word2007表格按模版样式填充行数据

 
 

 主要实现了按照模版行的样式填充数据,针对的是动态数据,静态数据可以直接替换变量实现,先说下缺点:1)暂未实现特殊样式填充(如列合并(跨行合并)),只能用于普通样式(如段落间距 缩进 字体 对齐)2)数据行插到模版行下面,没有实现指定位置插入
        直接上代码:

        

Java代码  
  1. import java.io.FileInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5. import java.util.Iterator;  
  6. import java.util.List;  
  7.   
  8. import org.apache.poi.xwpf.usermodel.XWPFDocument;  
  9. import org.apache.poi.xwpf.usermodel.XWPFParagraph;  
  10. import org.apache.poi.xwpf.usermodel.XWPFRun;  
  11. import org.apache.poi.xwpf.usermodel.XWPFTable;  
  12. import org.apache.poi.xwpf.usermodel.XWPFTableCell;  
  13. import org.apache.poi.xwpf.usermodel.XWPFTableRow;  
  14. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;  
  15. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd;  
  16. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;  
  17. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;  
  18. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;  
  19. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;  
  20. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;  
  21. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;  
  22.   
  23. public class POI_表格_按模版样式填充数据_S3_Test {  
  24.     public static void main(String[] args) throws Exception {  
  25.         POI_表格_按模版样式填充数据_S3_Test t=new POI_表格_按模版样式填充数据_S3_Test();  
  26.         t.insertDataToTable("f:/saveFile/temp/sys_s3_template.docx",3,false);  
  27.     }  
  28.       
  29.     public void insertDataToTable(String filePath,int tableSize,boolean isDelTmpRow) throws Exception {  
  30.         InputStream is = new FileInputStream(filePath);  
  31.         XWPFDocument doc = new XWPFDocument(is);  
  32.         List<List<String>> resultList = generateTestData(4);  
  33.         insertValueToTable(doc, resultList,tableSize,isDelTmpRow);  
  34.         saveDocument(doc, "f:/saveFile/temp/sys_"+System.currentTimeMillis()+".docx");  
  35.     }  
  36.   
  37.     /** 
  38.      * @Description: 按模版行样式填充数据,暂未实现特殊样式填充(如列合并),只能用于普通样式(如段落间距 缩进 字体 对齐) 
  39.      * @param resultList 填充数据 
  40.      * @param tableRowSize 模版表格行数 取第一个行数相等列数相等的表格填充 
  41.      * @param isDelTmpRow 是否删除模版行 
  42.      */  
  43.     //TODO 数据行插到模版行下面,没有实现指定位置插入  
  44.     public void insertValueToTable(XWPFDocument doc,  
  45.             List<List<String>> resultList,int tableRowSize,boolean isDelTmpRow) throws Exception {  
  46.         Iterator<XWPFTable> iterator = doc.getTablesIterator();  
  47.         XWPFTable table = null;  
  48.         List<XWPFTableRow> rows=null;  
  49.         List<XWPFTableCell> cells=null;  
  50.         List<XWPFTableCell> tmpCells=null;//模版列  
  51.         XWPFTableRow tmpRow=null;//匹配用  
  52.         XWPFTableCell tmpCell=null;//匹配用  
  53.         boolean flag=false;//是否找到表格  
  54.         while (iterator.hasNext()) {  
  55.             table = iterator.next();  
  56.             rows = table.getRows();  
  57.             if(rows.size()==tableRowSize){  
  58.                 tmpRow=rows.get(tableRowSize-1);  
  59.                 cells =tmpRow.getTableCells();  
  60.                 if(cells.size()==resultList.get(0).size()){  
  61.                     flag=true;  
  62.                     break;  
  63.                 }  
  64.             }  
  65.         }  
  66.         if(!flag){  
  67.             return;  
  68.         }  
  69.         tmpCells=tmpRow.getTableCells();  
  70.         for(int i=0,len=resultList.size();i<len;i++){  
  71.             XWPFTableRow row=table.createRow();  
  72.             row.setHeight(tmpRow.getHeight());  
  73.             List<String> list=resultList.get(i);  
  74.             cells=row.getTableCells();  
  75.             //插入的行会填充与表格第一行相同的列数  
  76.             for(int k=0,klen=cells.size();k<klen;k++){  
  77.                 tmpCell=tmpCells.get(k);  
  78.                 XWPFTableCell cell=cells.get(k);  
  79.                 setCellText(tmpCell, cell, list.get(k));  
  80.             }  
  81.             //继续写剩余的列  
  82.             for(int j=cells.size(),jlen=list.size();j<jlen;j++){  
  83.                 tmpCell=tmpCells.get(j);  
  84.                 XWPFTableCell cell=row.addNewTableCell();  
  85.                 setCellText(tmpCell, cell, list.get(j));  
  86.             }  
  87.         }  
  88.         //删除模版行  
  89.         if(isDelTmpRow){  
  90.             table.removeRow(tableRowSize-1);  
  91.         }  
  92.     }  
  93.       
  94.     public  void setCellText(XWPFTableCell tmpCell,XWPFTableCell cell,String text) throws Exception{  
  95.         CTTc cttc2 = tmpCell.getCTTc();  
  96.         CTTcPr ctPr2=cttc2.getTcPr();  
  97.           
  98.         CTTc cttc = cell.getCTTc();  
  99.         CTTcPr ctPr = cttc.addNewTcPr();  
  100.         cell.setColor(tmpCell.getColor());  
  101.         cell.setVerticalAlignment(tmpCell.getVerticalAlignment());  
  102.         if(ctPr2.getTcW()!=null){  
  103.             ctPr.addNewTcW().setW(ctPr2.getTcW().getW());  
  104.         }  
  105.         if(ctPr2.getVAlign()!=null){  
  106.             ctPr.addNewVAlign().setVal(ctPr2.getVAlign().getVal());  
  107.         }  
  108.         if(cttc2.getPList().size()>0){  
  109.             CTP ctp=cttc2.getPList().get(0);  
  110.             if(ctp.getPPr()!=null){  
  111.                 if(ctp.getPPr().getJc()!=null){  
  112.                     cttc.getPList().get(0).addNewPPr().addNewJc().setVal(ctp.getPPr().getJc().getVal());  
  113.                 }  
  114.             }  
  115.         }  
  116.           
  117.         if(ctPr2.getTcBorders()!=null){  
  118.             ctPr.setTcBorders(ctPr2.getTcBorders());  
  119.         }  
  120.           
  121.         XWPFParagraph tmpP=tmpCell.getParagraphs().get(0);  
  122.         XWPFParagraph cellP=cell.getParagraphs().get(0);  
  123.         XWPFRun tmpR =null;  
  124.         if(tmpP.getRuns()!=null&&tmpP.getRuns().size()>0){  
  125.             tmpR=tmpP.getRuns().get(0);  
  126.         }  
  127.         XWPFRun cellR = cellP.createRun();  
  128.         cellR.setText(text);  
  129.         //复制字体信息  
  130.         if(tmpR!=null){  
  131.             cellR.setBold(tmpR.isBold());  
  132.             cellR.setItalic(tmpR.isItalic());  
  133.             cellR.setStrike(tmpR.isStrike());  
  134.             cellR.setUnderline(tmpR.getUnderline());  
  135.             cellR.setColor(tmpR.getColor());  
  136.             cellR.setTextPosition(tmpR.getTextPosition());  
  137.             if(tmpR.getFontSize()!=-1){  
  138.                 cellR.setFontSize(tmpR.getFontSize());  
  139.             }  
  140.             if(tmpR.getFontFamily()!=null){  
  141.                 cellR.setFontFamily(tmpR.getFontFamily());  
  142.             }  
  143.             if(tmpR.getCTR()!=null){  
  144.                 if(tmpR.getCTR().isSetRPr()){  
  145.                     CTRPr tmpRPr =tmpR.getCTR().getRPr();  
  146.                     if(tmpRPr.isSetRFonts()){  
  147.                         CTFonts tmpFonts=tmpRPr.getRFonts();  
  148.                         CTRPr cellRPr=cellR.getCTR().isSetRPr() ? cellR.getCTR().getRPr() : cellR.getCTR().addNewRPr();  
  149.                         CTFonts cellFonts = cellRPr.isSetRFonts() ? cellRPr.getRFonts() : cellRPr.addNewRFonts();  
  150.                         cellFonts.setAscii(tmpFonts.getAscii());  
  151.                         cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme());  
  152.                         cellFonts.setCs(tmpFonts.getCs());  
  153.                         cellFonts.setCstheme(tmpFonts.getCstheme());  
  154.                         cellFonts.setEastAsia(tmpFonts.getEastAsia());  
  155.                         cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme());  
  156.                         cellFonts.setHAnsi(tmpFonts.getHAnsi());  
  157.                         cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme());  
  158.                     }  
  159.                 }  
  160.             }  
  161.         }  
  162.         //复制段落信息  
  163.         cellP.setAlignment(tmpP.getAlignment());  
  164.         cellP.setVerticalAlignment(tmpP.getVerticalAlignment());  
  165.         cellP.setBorderBetween(tmpP.getBorderBetween());  
  166.         cellP.setBorderBottom(tmpP.getBorderBottom());  
  167.         cellP.setBorderLeft(tmpP.getBorderLeft());  
  168.         cellP.setBorderRight(tmpP.getBorderRight());  
  169.         cellP.setBorderTop(tmpP.getBorderTop());  
  170.         cellP.setPageBreak(tmpP.isPageBreak());  
  171.         if(tmpP.getCTP()!=null){  
  172.             if(tmpP.getCTP().getPPr()!=null){  
  173.                 CTPPr tmpPPr = tmpP.getCTP().getPPr();  
  174.                 CTPPr cellPPr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();  
  175.                 //复制段落间距信息  
  176.                 CTSpacing tmpSpacing =tmpPPr.getSpacing();  
  177.                 if(tmpSpacing!=null){  
  178.                     CTSpacing cellSpacing= cellPPr.getSpacing()!=null?cellPPr.getSpacing():cellPPr.addNewSpacing();  
  179.                     if(tmpSpacing.getAfter()!=null){  
  180.                         cellSpacing.setAfter(tmpSpacing.getAfter());  
  181.                     }  
  182.                     if(tmpSpacing.getAfterAutospacing()!=null){  
  183.                         cellSpacing.setAfterAutospacing(tmpSpacing.getAfterAutospacing());  
  184.                     }  
  185.                     if(tmpSpacing.getAfterLines()!=null){  
  186.                         cellSpacing.setAfterLines(tmpSpacing.getAfterLines());  
  187.                     }  
  188.                     if(tmpSpacing.getBefore()!=null){  
  189.                         cellSpacing.setBefore(tmpSpacing.getBefore());  
  190.                     }  
  191.                     if(tmpSpacing.getBeforeAutospacing()!=null){  
  192.                         cellSpacing.setBeforeAutospacing(tmpSpacing.getBeforeAutospacing());  
  193.                     }  
  194.                     if(tmpSpacing.getBeforeLines()!=null){  
  195.                         cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines());  
  196.                     }  
  197.                     if(tmpSpacing.getLine()!=null){  
  198.                         cellSpacing.setLine(tmpSpacing.getLine());  
  199.                     }  
  200.                     if(tmpSpacing.getLineRule()!=null){  
  201.                         cellSpacing.setLineRule(tmpSpacing.getLineRule());  
  202.                     }  
  203.                 }  
  204.                 //复制段落缩进信息  
  205.                 CTInd tmpInd=tmpPPr.getInd();  
  206.                 if(tmpInd!=null){  
  207.                     CTInd cellInd=cellPPr.getInd()!=null?cellPPr.getInd():cellPPr.addNewInd();  
  208.                     if(tmpInd.getFirstLine()!=null){  
  209.                         cellInd.setFirstLine(tmpInd.getFirstLine());  
  210.                     }  
  211.                     if(tmpInd.getFirstLineChars()!=null){  
  212.                         cellInd.setFirstLineChars(tmpInd.getFirstLineChars());  
  213.                     }  
  214.                     if(tmpInd.getHanging()!=null){  
  215.                         cellInd.setHanging(tmpInd.getHanging());  
  216.                     }  
  217.                     if(tmpInd.getHangingChars()!=null){  
  218.                         cellInd.setHangingChars(tmpInd.getHangingChars());  
  219.                     }  
  220.                     if(tmpInd.getLeft()!=null){  
  221.                         cellInd.setLeft(tmpInd.getLeft());  
  222.                     }  
  223.                     if(tmpInd.getLeftChars()!=null){  
  224.                         cellInd.setLeftChars(tmpInd.getLeftChars());  
  225.                     }  
  226.                     if(tmpInd.getRight()!=null){  
  227.                         cellInd.setRight(tmpInd.getRight());  
  228.                     }  
  229.                     if(tmpInd.getRightChars()!=null){  
  230.                         cellInd.setRightChars(tmpInd.getRightChars());  
  231.                     }  
  232.                 }  
  233.             }  
  234.         }  
  235.     }  
  236.   
  237.     public void saveDocument(XWPFDocument document,String savePath) throws Exception{  
  238.         FileOutputStream fos = new FileOutputStream(savePath);  
  239.         document.write(fos);  
  240.         fos.close();  
  241.     }  
  242.     //生成测试数据  
  243.     public List<List<String>> generateTestData(int num) {  
  244.         List<List<String>> resultList = new ArrayList<List<String>>();  
  245.         for (int i = 1; i <= num; i++) {  
  246.             List<String> list = new ArrayList<String>();  
  247.             list.add("" + i);  
  248.             list.add("测试_" + i);  
  249.             list.add("测试2_" + i);  
  250.             list.add("测试3_" + i);  
  251.             list.add("测试4_" + i);  
  252.             resultList.add(list);  
  253.         }  
  254.         return resultList;  
  255.     }  
  256. }  

   结果如下:
      普通表格不删除模版列:
      
POI word2007表格按模版样式填充行数据_第1张图片
 
      普通表格删除模版列:
      
POI word2007表格按模版样式填充行数据_第2张图片
 
      带样式表格不删除模版列:
      

 
      带样式表格删除模版列:
       
POI word2007表格按模版样式填充行数据_第3张图片
 
      带合并单元格表格不删除模版列:
      
POI word2007表格按模版样式填充行数据_第4张图片
 
      带合并单元格表格删除模版列:
      
POI word2007表格按模版样式填充行数据_第5张图片
 

你可能感兴趣的:(POI word2007表格按模版样式填充行数据)