码迷,mamicode.com
首页 > 其他好文 > 详细

POI动态添加或删除行

时间:2015-06-18 18:45:43      阅读:7680      评论:0      收藏:0      [点我收藏+]

标签:

 

/**
*
* @param table 对应表格
* @param add 增加或删除行数 if add>0 增加行 add<0 删除行
* @param fromRow 添加开始行位置(fromRow-1是模版行)
*/


private void addOrRemoveRow(XWPFTable table, int add,int fromRow)

{
XWPFTableRow row = table.getRow(fromRow-1);
if(add>0)
{
while(add>0)
{
copyPro(row,table.insertNewTableRow(fromRow));
add--;
}
}
else
{
while(add<0)
{
table.removeRow(fromRow-1);
add++;
}
}
}

private void copyPro(XWPFTableRow sourceRow,XWPFTableRow targetRow)
{
//复制行属性
targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
List<XWPFTableCell> cellList = sourceRow.getTableCells();
if(null==cellList)
{
return ;
}
//添加列、复制列以及列中段落属性
XWPFTableCell targetCell = null;
for(XWPFTableCell sourceCell:cellList)
{
targetCell = targetRow.addNewTableCell();
//列属性
targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
//段落属性
targetCell.getParagraphs().get(0).getCTP().setPPr(sourceCell.getParagraphs().get(0).getCTP().getPPr());
}
}

POI动态添加或删除行

标签:

原文地址:http://www.cnblogs.com/hzw-hym/p/4586311.html

(2)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!