NPOI

读写Excel文件:

 1 using NPOI.SS.UserModel;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.IO;
 8 using NPOI.XSSF.UserModel;
 9 using NPOI.HSSF.UserModel;
10 namespace ExcelNPOI01
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             #region 读取文件
17 
18 
19             //通过流的方式获取文件
20             using (FileStream fsRead = new FileStream("123.xlsx", FileMode.Open, FileAccess.Read))
21             {
22                 //创建工作表
23                 IWorkbook workBook = WorkbookFactory.Create(fsRead);
24                 //获取页
25                 ISheet sheet = workBook.GetSheetAt(0);
26                 //获取行--最后一行
27                 for (int i = 0; i <= sheet.LastRowNum; i++)
28                 {
29                     IRow row = sheet.GetRow(i);
30                     //遍历所有的单元格
31                     StringBuilder sb = new StringBuilder();
32                     for (int j = 0; j < row.LastCellNum; j++)
33                     {
34                         //获取单元格
35                         ICell cell = row.GetCell(j);
36                         //判断单元格的数据类型
37                         if (cell.CellType == CellType.Numeric)
38                         {
39                             sb.Append(cell.NumericCellValue);//单元格的数据
40                         }
41                         if (cell.CellType == CellType.String)
42                         {
43                             sb.Append(cell.StringCellValue);
44                         }
45                     }
46                     Console.WriteLine(sb.ToString());
47                 }
48                 Console.ReadKey();
49             } 
50             #endregion
51 
52             //写入文件
53             List<Person> list = new List<Person>();
54             list.Add(new Person() { Name = "卡卡西", Age = 1, Gender = "" });
55             list.Add(new Person() { Name = "地主", Age = 2, Gender = "" });
56             list.Add(new Person() { Name = "独角兽", Age = 40, Gender = "" });
57             list.Add(new Person() { Name = "凤姐", Age = 4, Gender = "" });
58             //
59             using (FileStream fsWrite = new FileStream("person.xlsx", FileMode.Create, FileAccess.Write))
60             {
61                 //创建工作表
62                 XSSFWorkbook work = new XSSFWorkbook();//2007
63                 //HSSFWorkbook work = new HSSFWorkbook();//2003 改一下 person.xls
64                 //创建页
65                 ISheet sheet = work.CreateSheet("");
66                 //创建行
67                 for (int i = 0; i < list.Count; i++)
68                 {
69                     IRow row = sheet.CreateRow(i);
70                     ICell cell = row.CreateCell(0, CellType.String);
71                     cell.SetCellValue(list[i].Name);//姓名
72 
73                     ICell cell1 = row.CreateCell(1, CellType.Numeric);
74                     cell1.SetCellValue(list[i].Age);//年龄
75 
76                     ICell cell2 = row.CreateCell(2, CellType.String);
77                     cell2.SetCellValue(list[i].Gender);//性别
78                 }
79                 work.Write(fsWrite);
80             }
81             Console.WriteLine("OK");
82         }
83     }
84     class Person
85     {
86         public string Name { get; set; }
87         public int Age { get; set; }
88         public string Gender { get; set; }
89 
90     }
91 
92 
93 }