IRow 对象的常用属性及方法
看代码:
public class NPOIIRowDemo
{
IWorkbook workbook = null;
NPOIHelper npoiHelp = new NPOIHelper();
public NPOIIRowDemo()
{
string filePath = "D:\\NPOITest.xlsx";
workbook = npoiHelp.GetWorkbookByPath(filePath);
}
/// <summary>
/// 常用的属性
/// </summary>
public void ProPertyDemo()
{
Console.WriteLine("********IRow 的属性********");
ISheet sheet = workbook.GetSheetAt(0);
IRow row = sheet.GetRow(0);//获取第一行
//获取行第一个有数据的单元格对应的下标(从0开始)
short firstCellNum= row.FirstCellNum;
Console.WriteLine("FirstCellNum="+ firstCellNum);
//获取行最后一个有数据的单元格对应的下标(从1开始)
short lastCellNum= row.LastCellNum;
Console.WriteLine("LastCellNum="+ lastCellNum);
//读取/设置行对应的高度——25磅=5000
short height= row.Height;
Console.WriteLine("Height="+ height);
//读取/设置行对应的高度(磅)
float heightInPoints= row.HeightInPoints;
Console.WriteLine("HeightInPoints="+ heightInPoints);
row.HeightInPoints = 50;//设置行高为50磅
//读取/设置行是否隐藏
//bool? rowHidden = row.Hidden;
//Console.WriteLine("Hidden="+rowHidden);
//row.Hidden = false;//取消行隐藏
//读取/设置行的下标
int rowNum= row.RowNum;
Console.WriteLine("RowNum="+rowNum);
//rowNum = 5;//这样的赋值好像并没有任何意义
//获取/设置行对应的单元格的样式对象
ICellStyle cellStyle = row.RowStyle;
//获取行所在的sheet页的对象
ISheet sheets = row.Sheet;
//ZeroHeight——行高度是否为0(等同于行隐藏)
bool zeroHeight = row.ZeroHeight;
Console.WriteLine("ZeroHeight="+ zeroHeight);
row.ZeroHeight = true;//设置高度为0就相对于设置的行隐藏
npoiHelp.CreateNewExcel(workbook,"");
}
/// <summary>
/// 常用的方法
/// </summary>
public void MethodDemo()
{
Console.WriteLine("********IRow 的方法********");
ISheet sheet = workbook.GetSheetAt(0);
IRow row = sheet.GetRow(0);//获取第一行
//copy一个单元格的内容到另外一个单元格
row.CopyCell(10, 1);//copy第三个单元格的内容到第二个单元格
//copy该行到指定的行,其余行会向下移动(相当于插入一行)
row.CopyRowTo(1);
//创建一个单元格
ICell cell = row.CreateCell(5);
//创建一个单元格,同时声明单元格的类型---并不认为这个重载方法有什么特殊的
ICell cell2 = row.CreateCell(6, CellType.Numeric);
ICell cell3 = row.CreateCell(7, CellType.String);
ICell cell4 = row.CreateCell(8, CellType.Formula);//默认值:0
ICell cell5 = row.CreateCell(9, CellType.Blank);
ICell cell6 = row.CreateCell(10, CellType.Boolean);//默认值:False
ICell cell7 = row.CreateCell(11, CellType.Error);
//ICell cell8 = row.CreateCell(12, CellType.Unknown);//使用这个类型会报错
//获取指定位置的单元格
ICell cell9 = row.GetCell(2);//如果单元格内容为空,返回null对象
//移除一个单元格
//row.MoveCell(cell9,3);//报错
//删除单元格——只删除内容不删除位置
row.RemoveCell(cell9);
npoiHelp.CreateNewExcel(workbook, "");
}
}