-2

I have tried this code but it doesn't work:

private void Load_Button_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
             System.IO.FileStream fs = File.Open(ofd.FileName, System.IO.FileMode.Open, FileAccess.Read);
             ExcelDataReader.IExcelDataReader reader = ExcelDataReader.ExcelReaderFactory.CreateBinaryReader(fs);
             reader.IsFirstRowAsColumnNames = true;
             result = reader.AsDataSet();
             cboSheet.Items.Clear();

             foreach (DataTable dt in result.Tables)
                 cboSheet.Items.Add(dt.TableName);

             reader.Close();    
         }
     }
}

I can't seem to run this code because the IsFirstRowAsColumnNames and AsDataSet has red underlines.

I found the code here https://www.youtube.com/watch?v=7X3fTnuII7c

DavidG
  • 113,891
  • 12
  • 217
  • 223
Tramyer
  • 13
  • 1
  • 8

1 Answers1

2

From the instructions for the library:

Install the ExcelDataReader.DataSet extension package to use the AsDataSet() method to populate a System.Data.DataSet.

So you need an additional library here.

The IsFirstRowAsColumnNames property doesn't exist at all, perhaps it's a legacy item that has been removed. Instead, it looks like you need to pass in configuration to the AsDataSet method, for example:

ds = reader.AsDataSet(new ExcelDataSetConfiguration()
{
    ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
    {
        UseHeaderRow = true
    }
});
DavidG
  • 113,891
  • 12
  • 217
  • 223