繁体   English   中英

获取Excel工作表名称并将其添加到comboBox

[英]Getting Excel sheetname and add them to comboBox

我正在尝试向我的应用程序中添加功能,以便用户可以从comboBox中选择工作表。 但是我遇到了一些麻烦,我需要一些帮助! 我已经能够读取默认工作表名称,因此以前能够读取excel文件。 但是现在我可以将工作表名称放入我的comboBox中,但是现在似乎无法读取excel文件了吗? 请帮我

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);
            }
        }

现在

    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);
                }
            }

我可以知道怎么了吗? 我收到Cannot find table -1的错误,但我只能在Excel中看到工作Cannot find table -1而不是内容

首先,在表单中使用DataSet属性:

private DataSet ExcelDateSet { get; set; }

并添加一种从中读取表的方法,如下所示:

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];
}

然后像这样在btnOpen_Click设置其值:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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