ExcelReaderFactory,阅读第一页 [英] ExcelReaderFactory, reading first sheet
问题描述
我正在C#中使用 ExcelDataReaderFactory
,以便读取我的Excel文件并将其插入数据库。
现在我指定 sheetname
为我要使用的工作表。
我可以让它每次被选为第一张表单?
这是我如何加载数据。
public IExcelDataReader getExcelReader()
{
// ExcelDataReader与二进制Excel文件配合使用,因此需要一个FileStream
//开始。这是我们如何避免对ACE或Interop的依赖:
FileStream stream = File.Open(_path,FileMode.Open,FileAccess.Read);
//我们返回界面,所以
IExcelDataReader reader = null;
try
{
if(_path.EndsWith(。xls))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
if(_path.EndsWith(。xlsx))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
返回阅读器;
}
catch(异常)
{
throw;
}
}
public IEnumerable< string> getWorksheetNames()
{
var reader = this.getExcelReader();
var workbook = reader.AsDataSet();
var sheets =从工作簿中的DataTable工作表。表选择sheet.TableName;
退货单;
}
public IEnumerable< DataRow> getData(string sheet,bool firstRowIsColumnNames = false)
{
var reader = this.getExcelReader();
reader.IsFirstRowAsColumnNames = firstRowIsColumnNames;
var workSheet = reader.AsDataSet()。Tables [sheet];
var rows =从DataRow a在workSheet.Rows中选择一个;
返回行;
}
的getData( 四月); //这里我想要它成为第一张表,而不必选择。
欣赏任何建议。
我不知道那个图书馆。但是,我认为您正在将其转换为 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();
}
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
for the sheet that I want to use.
Can I make it to be chosen as the first sheet every time?
Here is how I load the data.
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.
Appreciate any advice.
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();
}
这篇关于ExcelReaderFactory,阅读第一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!