如何将特定的Excel列范围添加到DataGridView通过"添加列"添加的列

时间:2017-11-16 11:32:05

标签: c# excel datagridview

我正在尝试将Excel工作表列范围(A1:A100)导入到我的DataGridView"第1列和第34列;我在DataGridView"添加列"菜单如下图所示: Add Column

以下是我必须导入Excel工作表的当前代码:

private void ribbonButton1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.textBox_path.Text = openFileDialog1.FileName;
            string pathconn = "Provider=Microsoft.ACE.OLEDB.12.0; Data source=" + textBox_path.Text + ";Extended Properties=\"Excel 12.0;HDR= yes;\";";
            OleDbConnection conn = new OleDbConnection(pathconn);
            OleDbDataAdapter MyDataAdapter = new OleDbDataAdapter("Select * from [TB$]", conn);
            DataTable dt = new DataTable();
            MyDataAdapter.Fill(dt);
            dataGridView1.DataSource = dt;

但是每当我在创建自定义列后导入它时,它会导入我自定义创建列右侧的范围,如下图所示: After Import

请有人帮助我。我只想将导入数据放在我提供的列中

感谢米尔

0 个答案:

没有答案