读书人

NPOI 复制表怎么操作

发布时间: 2013-07-04 11:45:32 作者: rapoo

NPOI 复制表如何操作
复制整张表 请教这个该如何实现。
例如复制A1:A10, 黏贴在B1:B10
谢谢
[解决办法]
NPOI里面貌似没有Range(A1:A10)的操作。
自己手动对应吧。
[解决办法]
可以试下用Aspose.Cells这个。
[解决办法]
ISheet sheet = workBook.GetSheetAt(0);
var lastRowNum = sheet.LastRowNum;
for (int i = rowStart; i < lastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row!=null)
{
var value= row.GetCell(A).StringCellValue;
row.CreateCell(B).SetCellValue(value);
[解决办法]
参考:

private void MyInsertRow(ISheet sheet, int 插入行, int 插入行总数, IRow 源格式行)
{
#region 批量移动行
sheet.ShiftRows(插入行, //--开始行
sheet.LastRowNum, //--结束行


插入行总数, //--移动大小(行数)--往下移动
true, //是否复制行高
true //是否移动批注
);
#endregion

#region 对批量移动后空出的空行插,创建相应的行,并以插入行的上一行为格式源(即:插入行-1的那一行)
for (int i = 插入行; i < 插入行 + 插入行总数 - 1; i++)
{
IRow targetRow = null;

ICell sourceCell = null;
ICell targetCell = null;

targetRow = sheet.CreateRow(i + 1);
targetRow.Height = 源格式行.Height;

for (int m = 源格式行.FirstCellNum; m < 源格式行.LastCellNum; m++)
{


sourceCell = 源格式行.GetCell(m);
if (sourceCell == null)
continue;
targetCell = targetRow.CreateCell(m);

//targetCell.Encoding = sourceCell.Encoding;
targetCell.CellStyle = sourceCell.CellStyle;
targetCell.SetCellType(sourceCell.CellType);

}
}

IRow firstTargetRow = sheet.GetRow(插入行);
ICell firstSourceCell = null;
ICell firstTargetCell = null;
firstTargetRow.Height = 源格式行.Height;
for (int m = 源格式行.FirstCellNum; m < 源格式行.LastCellNum; m++)
{
firstSourceCell = 源格式行.GetCell(m);
if (firstSourceCell == null)
continue;
firstTargetCell = firstTargetRow.CreateCell(m);

firstTargetCell.CellStyle = firstSourceCell.CellStyle;
firstTargetCell.SetCellType(firstSourceCell.CellType);


}
#endregion
}

读书人网 >C#

热点推荐