ExcelDataReader - 数据 Reader 方法部分的用途是什么?
ExcelDataReader - What is the Data Reader Methods Section Used For?
论坛。
引用:https://github.com/ExcelDataReader/ExcelDataReader
C# code :
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
我的代码:
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
Debug.WriteLine("OnDragDrop");
if (validData)
{
try
{
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2. DataSet - The result of each spreadsheet will be created in the result.Tables
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//3. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
dataGridView1.DataSource = result.Tables[0];
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
createEmployees(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
我的问题是,'Data Reader methods' 部分的用途是什么?
我最初的想法是,它用于在将数据读入 DataSet 之前对其进行转换。通过逐步执行代码,我看到数据集结果已在第 3/4 步设置,因此没有发生数据转换或处理。鉴于此,我不确定 excelReader.Read() 代码的用途。
如果您不想使用 DataSet
而是阅读个人 rows/columns,则可以使用此选项。
Read
将 reader 的光标设置到下一行。如果结果中没有更多行,它 returns false
.
因此当 Read
returns true
时,您可以读取当前行的各个列:
while(excelReader.Read())
{
int i = excelReader.GetInt32(0); // 0 is the column index in your result set
string text = excelReader.IsDbNull(1) ? string.Empty : excelReader.GetString(1);
// etc...
}
您需要知道结果集中的列顺序。
查看 documentation of IDataReader
接口及其 Get*
方法以获取更多信息。
论坛。
引用:https://github.com/ExcelDataReader/ExcelDataReader
C# code :
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
我的代码:
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
Debug.WriteLine("OnDragDrop");
if (validData)
{
try
{
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//2. DataSet - The result of each spreadsheet will be created in the result.Tables
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//3. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
dataGridView1.DataSource = result.Tables[0];
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
createEmployees(result);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
我的问题是,'Data Reader methods' 部分的用途是什么?
我最初的想法是,它用于在将数据读入 DataSet 之前对其进行转换。通过逐步执行代码,我看到数据集结果已在第 3/4 步设置,因此没有发生数据转换或处理。鉴于此,我不确定 excelReader.Read() 代码的用途。
如果您不想使用 DataSet
而是阅读个人 rows/columns,则可以使用此选项。
Read
将 reader 的光标设置到下一行。如果结果中没有更多行,它 returns false
.
因此当 Read
returns true
时,您可以读取当前行的各个列:
while(excelReader.Read())
{
int i = excelReader.GetInt32(0); // 0 is the column index in your result set
string text = excelReader.IsDbNull(1) ? string.Empty : excelReader.GetString(1);
// etc...
}
您需要知道结果集中的列顺序。
查看 documentation of IDataReader
接口及其 Get*
方法以获取更多信息。