码迷,mamicode.com
首页 > Windows程序 > 详细

C# 使用NPOI 操作Excel

时间:2019-03-20 19:16:09      阅读:518      评论:0      收藏:0      [点我收藏+]

标签:ogre   eth   val   exce   工作   creat   result   ext   last   

首先 Nuget 引入NPOI

1、读取Excel

/// <summary>
        /// 读取Excel数据
        /// </summary>
        public static void ReadExcel()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Excel|*.xlsx;*.xls";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                IWorkbook workbook = null;
                //打开选择的Excel文件
                using (FileStream file = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read))
                {
                    //根据现有的Excel文档创建工作簿
                    workbook = WorkbookFactory.Create(ofd.FileName);
                }
                //获得工作表
                ISheet sheet0 = workbook.GetSheetAt(0);
                //获得所有行的集合
                System.Collections.IEnumerator rows = sheet0.GetRowEnumerator();
                var cellCount = sheet0.GetRow(0).LastCellNum;//第一行列数
                //从第一行开始读取
                for (int i = sheet0.FirstRowNum ; i <= sheet0.LastRowNum; i++)
                {
                    IRow row = sheet0.GetRow(i);
                    var cell0 = row.GetCell(0).ToString();//第 i 行第1列
                    var cell1 = row.GetCell(1).ToString();//第 i 行第2列
                    
                    //todo something:
                }
            }
        }

 

2、导出到Excel

/// <summary>
        /// Datable导出成Excel
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="file">导出路径(包括文件名与扩展名)</param>
        public static void TableToExcel(DataTable dt, string file)
        {
            IWorkbook workbook;
            string fileExt = Path.GetExtension(file).ToLower();
            if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; }
            if (workbook == null) { return; }
            ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(dt.TableName);

            //表头  
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据  
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                    cell.SetCellValue(dt.Rows[i][j].ToString());
                }
            }

            //转为字节数组  
            MemoryStream stream = new MemoryStream();
            workbook.Write(stream);
            var buf = stream.ToArray();

            //保存为Excel文件  
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
            }
        }

 

C# 使用NPOI 操作Excel

标签:ogre   eth   val   exce   工作   creat   result   ext   last   

原文地址:https://www.cnblogs.com/mybk/p/10566713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!