ExcelReaderFactory,读取第一张表

时间:2015-09-11 11:07:14

标签: c# excel

我在C#中使用ExcelDataReaderFactory,以便读取我的Excel文件并将其插入数据库。
现在我正在为我想要使用的工作表指定sheetname。 我可以每次都选择它作为第一张吗?

以下是我加载数据的方法。

public IExcelDataReader getExcelReader()
{
    // ExcelDataReader works with the binary Excel file, so it needs a FileStream
    // to get started. This is how we avoid dependencies on ACE or Interop:
    FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read);

    // We return the interface, so that
    IExcelDataReader reader = null;
    try
    {
        if (_path.EndsWith(".xls"))
        {
            reader = ExcelReaderFactory.CreateBinaryReader(stream);
        }
        if (_path.EndsWith(".xlsx"))
        {
            reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        }
        return reader;
    }
    catch (Exception)
    {
        throw;
    }
}

public IEnumerable<string> getWorksheetNames()
{
    var reader = this.getExcelReader();
    var workbook = reader.AsDataSet();
    var sheets = from DataTable sheet in workbook.Tables select sheet.TableName;
    return sheets;
}

public IEnumerable<DataRow> getData(string sheet, bool firstRowIsColumnNames = false)
{
    var reader = this.getExcelReader();
    reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
    var workSheet = reader.AsDataSet().Tables[sheet];
    var rows = from DataRow a in workSheet.Rows select a;
    return rows;
}

getData("april"); //Here I want it to be the first sheet, and not have to choose.

感谢任何建议。

2 个答案:

答案 0 :(得分:4)

我不知道那个图书馆。但我认为你无论如何都将它转换为DataSet。然后第一张表/表是:

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

由于indexer of DataTableCollection指数的重载不仅仅是名称。

所以整个方法是:

public IEnumerable<DataRow> GetFirstSheetData(bool firstRowIsColumnNames = false)
{
    var reader = this.getExcelReader();
    reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
    return reader.AsDataSet().Tables[0].AsEnumerable();
}

答案 1 :(得分:1)

v3更新

已经有一些breaking changes in the upgrade to v3,因此,这是原始代码和已接受的答案已更新,可以与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());
    }

我想说getExcelReader已简化为冗余点。