简体   繁体   中英

How to import many excel selected files in datagridview in C#

I have "import button" in my WFA to select which file I want to import it, my code loads the files which I selected,but it import only the last one. Here is load button I can load files in datagridview but I can not import them I import just the last one. How I can do to import all selected files, I'm really tired of this assigenment please help.

enter code here

   private void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "XML Files, Text Files, Excel Files|*.xlsx; *.xls; *.xml; *.txt; ";
            openFileDialog1.Multiselect = true;

            DataTable table = new DataTable();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (String file in openFileDialog1.FileNames)
                {
                    //tb_path is textbox
                    tb_path.Text = file;
                    // excelFilePath_com = tb_path.Text;
                    string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");

                    OleDbConnection con = new OleDbConnection(constr);
                    con.Open();

                    DataTable dt1 = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    table.Merge(dt1);
                    drop_down_sheet.DataSource = dt1;
                    //dro_down_sheet is combobox to choose which sheet to import 
                    drop_down_sheet.DisplayMember = "TABLE_NAME";
                    drop_down_sheet.ValueMember = "TABLE_NAME";
                }
            }
            dataGridView1.DataSource = table;
        }
        catch (Exception e1)
        {
            MessageBox.Show(e1.Message + e1.StackTrace);
        }
    }

And here is my import button:

enter code here

private void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
            OleDbConnection con = new OleDbConnection(constr);
            OleDbDataAdapter sda = new OleDbDataAdapter("Select * From[" + drop_down_sheet.SelectedValue + "]", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            foreach (DataRow row in dt.Rows)
            {
                dataGridView1.DataSource = dt;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message,
         "Important Note",
         MessageBoxButtons.OK,
         MessageBoxIcon.Error,
         MessageBoxDefaultButton.Button1);

        }
    }

Thank you:

Hope that i didnt misunderstood your question... You can only bind 1 source to 1 combobox(I think) Incase you want to bind 1 table per each combobox item, its must be a different thing.

 private void Button1_Click(object sender, EventArgs e)
 {
    try
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "XML Files, Text Files, Excel Files|*.xlsx; *.xls; *.xml; *.txt; ";
        openFileDialog1.Multiselect = true;

        DataTable table = new DataTable();

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            foreach (String file in openFileDialog1.FileNames)
            {
                //tb_path is textbox
                tb_path.Text = file;
                // excelFilePath_com = tb_path.Text;
                string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");

                OleDbConnection con = new OleDbConnection(constr);
                con.Open();

                DataTable dt1 = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                table.Merge(dt1);
            }
        }
            drop_down_sheet.DataSource = table;
            //dro_down_sheet is combobox to choose which sheet to import 
            drop_down_sheet.DisplayMember = "TABLE_NAME";
            drop_down_sheet.ValueMember = "TABLE_NAME";
        dataGridView1.DataSource = table;
    }
    catch (Exception e1)
    {
        MessageBox.Show(e1.Message + e1.StackTrace);
    }
}

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