使用NPIO操作Excel

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using NPOI.SS.UserModel;

using NPOI.HSSF.UserModel;

using System.IO;



namespace _02使用NOPI操作excel

{

    class Program

    {

        static void Main(string[] args)

        {

            //1、读取EXCEL到Filestream

            using (FileStream fs = File.OpenRead("1.xls"))

            {

                //2、根据文件流fs创建一个Workbook

                using (Workbook wk = new HSSFWorkbook(fs))

                {

                    //3、循环获取工作表中的个数 wk.NumberOfSheets

                    for (int i = 0; i < wk.NumberOfSheets; i++)

                    {

                        //4、循环获取每个工作表

                        using (Sheet sheet = wk.GetSheetAt(i))

                        {

                           Console.WriteLine("===================={0}===============", sheet.SheetName);

                            //5、循环获取每一行sheet.LastRowNum获取最后一行的索引

                            for (int r = 0; r <= sheet.LastRowNum; r++)

                            {

                               

                                Row row = sheet.GetRow(r);

                                //6、获取行中的单元格

                                for (int c = 0; c < row.LastCellNum; c++)

                                {

                                     Cell cell = row.GetCell(c);

                                     Console.Write(cell.ToString()+"\t");

                                 }

                                 Console.WriteLine();

                            }

                        }

                    }

                }

            }

            Console.ReadKey();

        }

    }

}

 

你可能感兴趣的:(Excel)