NPOI学习笔记

  NPOI最简单的创建一个Excel,并且在指定单元格内填写一些数据

 HSSFWorkbook workbook = new HSSFWorkbook(file);
 ISheet sheet = workbook.CreateSheet("许嵩");
 sheet.CreateRow(1).CreateCell(1).SetCellValue("我是牛逼村的");
 sheet.GetRow(1).CreateCell(2).SetCellValue("蜀云泉真帅啊");
 FileStream file = new FileStream("蜀云泉.xls",FileMode.Create);
 workbook.Write(file);
 file.Close();

  

  不过这个没意思,大部分人在使用导出Excel的时候,不会傻傻的自己去创建一个新的Excel,使用模板才是最方便的方法。

  模板的使用方法:

  

  我在bin下面新建了两个文件夹,一个是模板,一个是结果。模板里面存放了一个excel模板,那做的是花里胡哨的,代码如下:

            string fileDir = Environment.CurrentDirectory + "\模板\蜀云泉真帅.xls";
            FileStream Dir = new FileStream(fileDir, FileMode.Open, FileAccess.Read);     
            HSSFWorkbook workbook = new HSSFWorkbook(Dir);
            ISheet sheet = workbook.GetSheet("Sheet1");

            sheet.GetRow(3).GetCell(1).SetCellValue("");
            sheet.GetRow(4).GetCell(1).SetCellValue("");
            sheet.GetRow(5).GetCell(1).SetCellValue("");
            sheet.GetRow(6).GetCell(1).SetCellValue("");
            sheet.GetRow(7).GetCell(1).SetCellValue("我");
            sheet.GetRow(8).GetCell(1).SetCellValue("真");
            sheet.GetRow(9).GetCell(1).SetCellValue("是");
            sheet.GetRow(3).GetCell(2).SetCellValue("帅");
            
            sheet.ForceFormulaRecalculation = true;  //强制计算Excel中的公式

            FileStream file = new FileStream(Environment.CurrentDirectory + $"\结果\蜀云泉真帅{DateTime.Now.ToString("D")}.xls", FileMode.Create);
            workbook.Write(file);
            file.Close();

  如此,如此。这里还需要说明的一个事情是,我在测试这个案例的时候,一直报错,说我的对象应用未引用到实例什么的,但是我检查感觉没问题啊,最后发现我必须在模板里面需要填写的单元格随便填上一些东西,比如0或者其它乱七八糟的字符。NPOI才相信我这一列是存在的。。。。

  以上就是NPOI写内容进Excel的一些操作。但是有的时候外面还需要去读取Excel里面的内容。NPOI读取Excel内容代码如下所示:

   //读取excel
        public List<string> ExcelToDataTable(string filePath, bool isColumnName)
        {
            //DataTable dataTable = null;
            List<string> list = new List<string>();
            FileStream fs = null;
            IWorkbook workbook = null;
            ISheet sheet = null;
            ICell cell = null;
            string result=null;
            try
            {
                using (fs = File.OpenRead(filePath))
                {
                    // 2007版本
                    if (filePath.IndexOf(".xlsx") > 0)
                        workbook = new XSSFWorkbook(fs);
                    // 2003版本
                    else if (filePath.IndexOf(".xls") > 0)
                        workbook = new HSSFWorkbook(fs);

                    if (workbook != null)
                    {
                        sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
                        //dataTable = new DataTable();
                        if (sheet != null)
                        {
                            int rowCount = sheet.LastRowNum;//总行数
                            if (rowCount > 0)
                            {
                                IRow firstRow = sheet.GetRow(2);//第3行
                                int cellCount = firstRow.LastCellNum;//列数

                                //构建datatable的列
                                if (isColumnName)
                                {
                                    for (int i = firstRow.FirstCellNum; i < cellCount; i++)
                                    {
                                        cell = firstRow.GetCell(i);
                                        if (cell != null)
                                        {
                                            result = cell.Row.Cells[i].ToString();
                                            list.Add(result);

                                        }
                                    }
                                }
                                else
                                {
                                    //for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
                                    //{
                                    //    column = new DataColumn("column" + (i + 1));
                                    //    dataTable.Columns.Add(column);
                                    //}
                                }
                               
                            }
                        }
                    }
                }
                return list;
            }
            catch (Exception)
            {
                if (fs != null)
                {
                    fs.Close();
                }
                return null;
            }
        }

  基本代码就是这样的。

  有的单元格的文字是有链接的,读取文字和链接使用的方法如下

string name = Row.GetCell(1).Row.Cells[0].ToString();
string a = Row.GetCell(1).Row.Cells[0].Hyperlink.Address;
原文地址:https://www.cnblogs.com/yunquan/p/8952356.html