第七章——ICell的常用属性与方法

9 篇文章 8 订阅

直接看代码:

    /// <summary>
    /// NPOI ICell 对象的常用属性与方法 demo
    /// </summary>
    public class NPOIICellDemo
    {
        IWorkbook workbook = null;
        NPOIHelper npoiHelp = new NPOIHelper();
        public NPOIICellDemo()
        {
            string filePath = "D:\\NPOITest.xlsx";
            workbook = npoiHelp.GetWorkbookByPath(filePath);
        }

        /// <summary>
        /// ICell 常用的属性
        /// </summary>
        public void PropertyDemo()
        {
            Console.WriteLine("***********ICell的常用属性**********");
            ISheet sheet = workbook.GetSheetAt(0);
            IRow row = sheet.GetRow(0);
            ICell cell= row.GetCell(2);
            //获取/设置单元格样式对象
            ICellStyle cellStyle = cell.CellStyle;
            //获取单元格类型
            CellType cellType = cell.CellType;
            //获取单元格所在行的行对象
            IRow rows = cell.Row;
            //获取单元格所在行下标
            int rowindex = cell.RowIndex;
            //获取单元格所在sheet页的对象
            ISheet sheets = cell.Sheet;
            //获取单元格的值(string格式),不是字符串格式的单元格,获取会报错
            string cellValue = cell.StringCellValue;
            Console.WriteLine("CellValue="+ cellValue);


            npoiHelp.CreateNewExcel(workbook,null);
        }

        /// <summary>
        /// ICell 常用的方法
        /// </summary>
        public void MethodDemo()
        {
            Console.WriteLine("***********ICell的常用方法**********");
            ISheet sheet = workbook.GetSheetAt(0);
            IRow row = sheet.GetRow(0);
            ICell cell = row.GetCell(2);

            //copy单元格到指定的位置(只能在同行)
            ICell copyCell = cell.CopyCellTo(6);
            //设置单元格的值
            cell.SetCellValue(true);//bool
            cell.SetCellValue("test");//strintg
            cell.SetCellValue(DateTime.Now);//Datetime
            cell.SetCellValue(2.22);//double


            npoiHelp.CreateNewExcel(workbook,"");
        }
    }
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值