// 判断合并单元格重载
// 调用时要在输出变量前加 out
public bool isMergeCell(ISheet sheet,int rowNum,int colNum,out int rowSpan,out int colSpan)
{
bool result = false
rowSpan = 0
colSpan = 0
if ((rowNum <1) || (colNum <1)) return result
int rowIndex = rowNum - 1
int colIndex = colNum - 1
int regionsCount = sheet.NumMergedRegions
rowSpan = 1
colSpan = 1
for (int i = 0i <regionsCounti++ )
{
CellRangeAddress range = sheet.GetMergedRegion(i)
sheet.IsMergedRegion(range)
if (range.FirstRow == rowIndex &&range.FirstColumn == colIndex)
{
rowSpan = range.LastRow - range.FirstRow + 1
colSpan = range.LastColumn - range.FirstColumn + 1
break
}
}
try
{
result = sheet.GetRow(rowIndex).GetCell(colIndex).IsMergedCell
}
catch
{
}
return result
}
其中 rowSpan 和 colSpan 变量调用时要用 out 行跨度变量a out 列跨度变量b 格式返回给变量存储,
int spanR=0,spanC=0
bool result=isMergeCell(sheet,5,4,out spanR,out spanC)
此时spanR和spanC返回后的结果就是行列跨度,核心原理就是用了range.LastRow和range.LastColumn读区域表格的最后行列位置
如:
NPOI可以控制样式啊,好好看看官方示例的代码。ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1")
//set the width of columns
sheet1.SetColumnWidth(0,50 * 256)
sheet1.SetColumnWidth(1, 100 * 256)
sheet1.SetColumnWidth(2, 150 * 256)
//set the width of height
sheet1.CreateRow(0).Height = 100*20
sheet1.CreateRow(1).Height = 200*20
sheet1.CreateRow(2).Height = 300*20
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)