隐藏

C# NPOI库使用(一)——xls、xlsx文件读取

发布:2023/3/25 0:28:09作者:管理员 来源:本站 浏览次数:709

一、准备Excel文件

首先我们要使用的Excel文件名test.xls或test.xlsx,如:

包含的Sheet页:

Sheet页中的内容:

二、xls读取
·核心代码:

//根据指定路径path获取文件流对象
var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
//获取工作簿对象
var workbook = new HSSFWorkbook(fs);
//获取sheet页对象
var sheet = workbook.GetSheet("sheet1");
//获取指定索引index行对象
var row = sheet.GetRow(index);
//获取指定索引index单元格对象
var cell = row.GetCell(index);

完整Demo:

private static void Test1()
{
   var path = @"C:\Users\ashwx\Desktop\test.xls";
    if (File.Exists(path))
   {
       using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
       {
              var workbook = new HSSFWorkbook(fs);
              var sheet = workbook.GetSheet("sheet1");
              if (sheet != null)
              {
                  var row = sheet.GetRow(0);
                  if (row != null)
                  {
                      var cell = row.GetCell(0);
                      if (cell != null)
                      {
                         Console.WriteLine("{0} {1} {2}",cell.ToString(),
                         cell.NumericCellValue, cell.StringCellValue);
                      }
                      else
                      {
                          Console.WriteLine("cell null");
                      }
                  }
                  else
                  {
                      Console.WriteLine("row null");
                  }
             }
             else
             {
                  Console.WriteLine("sheet null");
             }
         }
    }
    else
    {
        Console.WriteLine("file null");
    }
}

   
三、xlsx读取
·核心代码:

//根据指定路径path获取文件流对象
var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
//获取工作簿对象
var workbook = new XSSFWorkbook (fs);
//获取sheet页对象
var sheet = workbook.GetSheet("sheet1");
//获取指定索引index行对象
var row = sheet.GetRow(index);
//获取指定索引index单元格对象
 var cell = row.GetCell(index)

  
完整Demo:

private static void Test2()
{
    var path = @"C:\Users\ashwx\Desktop\1.xlsx";
    if (File.Exists(path))
    {
        using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            var workbook = new XSSFWorkbook(fs);
            var sheet = workbook.GetSheet("sheet1");
            if (sheet != null)
            {
                var row = sheet.GetRow(0);
                if (row != null)
                {
                    var cell = row.GetCell(0);
                    if (cell != null)
                    {
                        Console.WriteLine("{0} {1} {2}", cell.ToString(),
                        cell.NumericCellValue, cell.StringCellValue);
                    }
                    else
                    {
                        Console.WriteLine("cell null");
                    }
                }
                else
                {
                    Console.WriteLine("row null");
                }
            }
            else
            {
                Console.WriteLine("sheet null");
            }
        }
    }
    else
    {
        Console.WriteLine("file null");
    }
}

 

四、遇到的特殊问题

    如果sheet页被隐藏或不存在时,GetSheet()返回值是什么?

    隐藏时,GetSheet可以正常获取非null对象;

    不存在时,GetSheet返回值为null;