简体   繁体   中英

Getting Excel sheetname and add them to comboBox

I am trying to add a feature into my application where users can choose the sheets from the comboBox. But I am hitting some bumps, I need some help! I was able to read the the excel file previously as I already sheet the default sheetname. But now I am able to get the sheetname into my comboBox, but I can't seem to read the excel file now? Please help me

public static DataTable ExcelToDataTable (string fileName)
        {
            using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        UseColumnDataType = true,
                        ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                        {
                            UseHeaderRow = true
                        }
                    });
                    DataTableCollection table = result.Tables;
                    DataTable resultTable = table["Sheet1"];                 
                    return resultTable;
                }
            }
        }

 private void btnOpen_Click(object sender, EventArgs e)
        {

            try
            {
                using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel 2003 Worksheet|*.xls|Excel 2007 Worksheet|*.xlsx", ValidateNames = true, Multiselect = false })
                {
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                     dataGridView1.DataSource = ExcelToDataTable(ofd.FileName);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

And Now

    public static DataSet ExcelToDataTable (string fileName)
            {
                using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
                {
                    using (var reader = ExcelReaderFactory.CreateReader(stream))
                    {
                        var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                        {
                            UseColumnDataType = true,
                            ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                            {
                                UseHeaderRow = true
                            }
                        });
                        return result;
                    }
                }
            }

     private void btnOpen_Click(object sender, EventArgs e)
            {

                try
                {
                    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel 2003 Worksheet|*.xls|Excel 2007 Worksheet|*.xlsx", ValidateNames = true, Multiselect = false })
                    {
                        if (ofd.ShowDialog() == DialogResult.OK)
                        {
                            comboBox1.Items.Clear();
                            foreach (DataTable dt in ExcelToDataTable(ofd.FileName).Tables)
                            {
                                comboBox1.Items.Add(dt.TableName);
                            }
                            DataTableCollection table = ExcelToDataTable(ofd.FileName).Tables;
                            DataTable resultTable = ExcelToDataTable(ofd.FileName).Tables[comboBox1.SelectedIndex];
                            dataGridView1.DataSource = resultTable
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

May I know what went wrong? I am getting an error of Cannot find table -1 but I can see the sheetname in the excel just not the content

At first use a DataSet property in your form:

private DataSet ExcelDateSet { get; set; }

And add a method to read a table from it like this:

private DataTable GetExcelDataTable(string sheetName)
{
    if (string.IsNullOrEmpty(sheetName) || !ExcelDateSet.Tables.Contains(sheetName))
    {
         // Notify user to select a sheet-name from your ComboBox!
         // or you can return first-sheet info as default like this:
         // return ExcelDateSet.Tables[0];
    }

    return ExcelDateSet.Tables[sheetName];
}

then set its value in your btnOpen_Click like this:

ExcelDataSet = ExcelToDataTable(ofd.FileName);
dataGridView1.DataSource = GetExcelDataTable(comboBox1.SelectedText);
dataGridView1.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM