第5章-ISheetインターフェイスの一般的な属性とメソッド

コードを直接見てください:

/// <summary>
    /// ISheet 常用的属性/方法
    /// </summary>
    public  class ISheetDemo
    {
    
    
        IWorkbook workbook = null;
        NPOIHelper npoi = new NPOIHelper();
        public ISheetDemo()
        {
    
    
            string filePath = "D:\\NPOITest.xlsx";
            workbook = npoi.GetWorkbookByPath(filePath);
        }
        /// <summary>
        /// isheet 对象常规使用的属性
        /// </summary>
        public void ProPertyDemo()
        {
    
    
            Console.WriteLine("ISheet的属性使用");
            //获取第一个sheet
            ISheet sheet = workbook.GetSheetAt(0);

            //sheet页单元格默认的宽度--没有效果
            int columnWidth = sheet.DefaultColumnWidth;
            Console.WriteLine("获得第一个sheet页的单元格默认宽度是:"+columnWidth);
            /*
             sheet页行默认的高度
             excel中是15磅的得到的结果是300
             */
            int rowHeight = sheet.DefaultRowHeight;
            Console.WriteLine("默认行高:"+rowHeight);
            /*
             设置默认行高为400,
             excle中的行高为20磅
             */
            sheet.DefaultRowHeight = 400;
            //DefaultRowHeightInPoints:默认行高(磅)
            float defaultRowHeightInPoints = sheet.DefaultRowHeightInPoints;
            Console.WriteLine("DefaultRowHeightInPoints:" + defaultRowHeightInPoints);
            //获取出现第一个数据的行的下标(从0开始)
            int firstRowNum = sheet.FirstRowNum;
            Console.WriteLine("FirstRowNum=" + firstRowNum);
            //获取最后一个数据所在行的下标(从0开始)
            int lastRowNum= sheet.LastRowNum;
            Console.WriteLine("LastRowNum="+ lastRowNum);
            //获取sheet页名称
            string sheetName = sheet.SheetName;
            Console.WriteLine("Sheet页名称:"+sheetName);
            //获取sheet对应的workbook对象
            IWorkbook sheetWorkbook = sheet.Workbook;
            

            //创建新的excel查看修改的效果
            npoi.CreateNewExcel(workbook,null);
        }

        /// <summary>
        /// isheet 对象常见的方法使用
        /// </summary>
        public void MehthodDemo()
        {
    
    
            Console.WriteLine("*********ISheet 方法的使用*********");
            //获取第一个sheet页
            ISheet sheet = workbook.GetSheetAt(0);
            //
            /*
             复制行,将第一行的数据,copy到第二行,其他行依次向下移动,相当于插入一行
             注意:被copy的行的下标一定要小于等于lastrownum
             */
            IRow row = sheet.CopyRow(0, 1);
            //复制sheet页,默认样式页copy过去
            ISheet copySheet1 = sheet.CopySheet("copySheet1");
            //复制sheet页,并指定样式是否也copy走----没有效果
            ISheet copySheet2 = sheet.CopySheet("copySheet2",false);
            /*
             将要给sheet页的内容复制给另外一个sheet页
             */
            sheet.CopyTo(workbook, "sheet333", false, false);
            //创建一行
            Console.WriteLine("创建一行前,最后一行的下标:"+sheet.LastRowNum);
            sheet.CreateRow(10);
            Console.WriteLine("创建一行后,最后一行的下标:" + sheet.LastRowNum);
            /*
             获取一行
             注意:如果该行没有一个单元格有数据则,返回null
             */
            IRow row2 = sheet.GetRow(10);
            //不清楚有什么样的效果
            //sheetGroupColumn(2, 3);

            /*
             删除行,只删除行内容,后面的行不上移
             */
            IRow row4 = sheet.GetRow(1);
            sheet.RemoveRow(row4);

            npoi.CreateNewExcel(workbook,null);
        }

    }

おすすめ

転載: blog.csdn.net/qq_39541254/article/details/107907268