ExcelReaderFactory, reading first sheet

前端 未结 2 1128
死守一世寂寞
死守一世寂寞 2021-01-20 21:00

I am using the ExcelDataReaderFactory in C#, in order to read my Excel files and inserting them to a database.
Right now I am specifying sheetname

相关标签:
2条回答
  • 2021-01-20 21:51

    Update for v3

    There have been some breaking changes in the upgrade to v3, so here's the original code and accepted answer updated to work with v3.

        public IExcelDataReader getExcelReader()
        {
            return ExcelReaderFactory.CreateReader(System.IO.File.OpenRead(_path));
        }
    
        public IEnumerable<string> getWorksheetNames()
        {
            var reader = this.getExcelReader();
            var workbook = reader.AsDataSet();
            var sheets = from DataTable sheet in workbook.Tables.Cast<DataTable>() select sheet.TableName;
            return sheets;
        }
    
        public IEnumerable<DataRow> getData(string sheet, bool firstRowIsColumnNames = false)
        {
            var reader = this.getExcelReader();
            reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = firstRowIsColumnNames
                }
            });
            var workSheet = reader.AsDataSet().Tables[sheet];
            var rows = from DataRow a in workSheet.Rows select a;
            return rows;
        }
    
        public IEnumerable<DataRow> GetFirstSheetData(bool firstRowIsColumnNames = false)
        {
            var reader = this.getExcelReader();
            reader.AsDataSet(new ExcelDataSetConfiguration()
            {
                ConfigureDataTable = (_) => new ExcelDataTableConfiguration()
                {
                    UseHeaderRow = firstRowIsColumnNames
                }
            });
            return getData(getWorksheetNames().First());
        }
    

    I would say that getExcelReader has been simplified to the point of redundancy.

    0 讨论(0)
  • 2021-01-20 21:57

    I don't know that library. But I think you are converting it to a DataSet anyway. Then the first sheet/table is:

    DataTable firstWorkSheet = reader.AsDataSet().Tables[0];
    

    Since the indexer of DataTableCollection has an overload for the index not only for the name.

    So the whole method is:

    public IEnumerable<DataRow> GetFirstSheetData(bool firstRowIsColumnNames = false)
    {
        var reader = this.getExcelReader();
        reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
        return reader.AsDataSet().Tables[0].AsEnumerable();
    }
    
    0 讨论(0)
提交回复
热议问题