I'm using C# to read an Excel file using ExcelDataReader
and ExcelDataReader.DataSet
. I'm not too familiar with parsing DataSets so I was wondering how to save the values from each row in a particular column in the Excel. I need the code to pull all the values from the first column of the spreadsheet I load.
public void ExcelFileReader(string path)
{
var stream = File.Open(path, FileMode.Open, FileAccess.Read);
var reader = ExcelReaderFactory.CreateReader(stream);
var rowCount = reader.RowCount;
result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (DataTableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = true
}
});
var tables = result.Tables.Cast<DataTable>();
var dataTable = result.Tables[0].Columns[0].ToString();
foreach(DataTable table in tables)
{
dataGridView1.DataSource = table;
}
}
The line: var dataTable = result.Tables[0].Columns[0].ToString();
will get me the name of the header for the column I want but how do I parse through the rows of column 1 and save all the values?
CodePudding user response:
You have to iterate throught the rows of the table and then get the column 0 value.
foreach (DataRow row in dataset.Tables[0].Rows)
{
Console.WriteLine(row[0]);
}
This will write all the values from the first column (index 0).