zoukankan      html  css  js  c++  java
  • C#读取Excel表格的数据

    1、创建工程后,需要下载 EPPlus.dll 添加到工程中,这里有一个下载地址:https://download.csdn.net/download/myunity/10784634

    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<string> list = GetExcelColumnValue(@"D:UniuAndroid5.6.2plan配置表excel	_equip.xlsx", 1, 3);
    13 
    14             for (int i = 0; i < list.Count; ++i)
    15             {
    16                 Console.WriteLine(list[i]);
    17             }
    18 
    19             Console.ReadKey();
    20         }
    21 
    22         /// <summary>
    23         /// 读取Excel表格列数据
    24         /// </summary>
    25         /// <param name="path">Excel表格所在的路径</param>
    26         /// <param name="sheetIndex">需要读取的Sheet页码序号</param>
    27         /// <param name="columnIndex">需要读取的列序号</param>
    28         /// <returns></returns>
    29         static List<string> GetExcelColumnValue(string path, int sheetIndex, int columnIndex)
    30         {
    31             List<string> list = new List<string>();
    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 }

    读取结果部分截图:

  • 相关阅读:
    Linux私房菜——防火墙部分笔记
    ubuntu
    序列求和
    圆的面积
    Fibonacci数列
    JavaScript中定义数组
    C语言中的EOF
    jQuery获取的值去掉px
    jQuery中单引号和双引号的使用
    jQuery报错:Uncaught ReferenceError: $ is not defined
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/9958663.html
Copyright © 2011-2022 走看看