Using ExcelDataReader to read Excel data starting from a particular cell

后端 未结 8 1866
感情败类
感情败类 2020-12-02 22:46

I am using ExcelDataReader to read data from my Excel workbook in C#.
But structure of my Excel sheet is such that data to be read can start from any particular cell and

8条回答
  •  盖世英雄少女心
    2020-12-02 23:21

    public static DataTable ConvertExcelToDataTable(string filePath, bool isXlsx = false)
    {
        System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
        //open file and returns as Stream
            using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
            {
                    using (var reader = ExcelReaderFactory.CreateReader(stream))
                    {
    
                        var conf = new ExcelDataSetConfiguration
                        {
                            ConfigureDataTable = _ => new ExcelDataTableConfiguration
                            {
                                UseHeaderRow = true
                            }
                        };
    
                        var dataSet = reader.AsDataSet(conf);
    
                        // Now you can get data from each sheet by its index or its "name"
                        var dataTable = dataSet.Tables[0];
    
                        Console.WriteLine("Total no of rows  " + dataTable.Rows.Count);
                        Console.WriteLine("Total no of Columns  " + dataTable.Columns.Count);
    
                        return dataTable;
    
                    }
    
            }
       
    }
    

提交回复
热议问题