C#读取Excel表格的数据
- 格式:pdf
- 大小:44.54 KB
- 文档页数:2
C#读取Excel表格的数据
2、下⾯仅实现读取Excel表格的列数据:
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using OfficeOpenXml;
5
6 namespace ConsoleApplication1
7 {
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 List
13
14 for (int i = 0; i < list.Count; ++i)
15 {
16 Console.WriteLine(list[i]);
17 }
18
19 Console.ReadKey();
20 }
21
22 ///
23 /// 读取Excel表格列数据
24 ///
25 /// Excel表格所在的路径
26 /// 需要读取的Sheet页码序号
27 /// 需要读取的列序号
28 ///
29 static List
30 {
31 List
32
33 FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
34 ExcelPackage excel = new ExcelPackage(fileStream);
35 ExcelWorksheet sheet = excel.Workbook.Worksheets[sheetIndex];
36
37 try
38 {
39 for (int i = 5; i <= sheet.Dimension.End.Row; i++)
40 {
41 var cell = sheet.Cells[i, columnIndex];
42 if (cell != null && cell.Value != null)
43 list.Add(cell.Value.ToString());
44 }
45 }
46 catch
47 {
48 throw;
49 }
50 finally
51 {
52 sheet.Dispose();
53 excel.Dispose();
54 fileStream.Dispose();
55 }
56
57 return list;
58 }
59 }
60 }
读取结果部分截图: