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

直接看代码:

    /// <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,"");
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_39541254/article/details/107918098