-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

1

1 Answer 1

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
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I have already installed this. Still it's not working. I'm a beginner in C# programming . I kinda stuck again XD
Read the instructions for the library.
Currently reading it. i hope i could find the key.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.