首页 > 其他 > 详细

POI动态添加或删除行

时间:2015-06-18 18:43:48      阅读:283      评论: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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!