C# read excel file via ExcelDataReader

FredGrit發表於2024-08-20
Install-Package ExcelDataReader

using ExcelDataReader;
using System.Text;
using System.IO;

namespace ConsoleApp50
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ReadExcel("aaa.xls");
            Console.ReadLine();
        }

        static void ReadExcel(string excelPath)
        {
            System.Text.Encoding encoding = System.Text.Encoding.UTF8 ;
            if (!File.Exists(excelPath))
            {
                return;
            }
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            using (var fs = File.Open(excelPath, FileMode.Open,FileAccess.Read))
            {
                using (var excelReader = ExcelReaderFactory.CreateReader(fs))
                {
                    while (excelReader.Read())
                    {
                        Console.WriteLine($"{excelReader.GetString(0)},{excelReader.GetString(4)},{excelReader.GetString(7)}");
                    }
                    while (excelReader.NextResult()) ;
                }
            }
        }
    }
}

相關文章