简体   繁体   中英

importing data from excel file to c#

I have an excel file: City Cafe Phone Number New York Trio Cafe 78654 New York Central Cafe 32344 Washington House Cafe 23222 Washington Central Cafe 11111 LA Wood Cafe 45434 Texas Central Cafe 16564

ComboBox1 contains cities. ComboBox2 contains cafes. Button1 writes to datagridview. My problem is that: For Example: I am choosing Washington in the ComboBox1.Then I am choosing Central Cafe in the Combobox2 and clicking Button1. I see that: New York Central Cafe 32344 Washington Central Cafe 11111 Texas Central Cafe 16564 in the datagridview. But I chose Washington in the Combobox1.So I want to see only Washington Central Cafe 11111 in the datagridview.

How can I do this??

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace uy
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
    DataTable dt = new DataTable();
    private void Form1_Load(object sender, EventArgs e)
    {
        OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");

        baglan.Open();
        string sql = "SELECT  * FROM [Sheet1$A1:A1000]";
        OleDbCommand komut = new OleDbCommand(sql, baglan);
        OleDbDataReader dr = null;
        dr = komut.ExecuteReader();



        while (dr.Read())
        {
            if (!comboBox1.Items.Contains(dr[0].ToString()))
            {
                comboBox1.Items.Add(dr[0].ToString());
            }


        }
        baglan.Close();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox2.Items.Clear();

        OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
        baglan.Open();
        string sql_b = "SELECT * FROM [Sheet1$B1:B1000]";
        OleDbCommand komut_b = new OleDbCommand(sql_b, baglan);
        OleDbDataReader dr_b = null;
        dr_b = komut_b.ExecuteReader();

        string sql_a = "SELECT * FROM [Sheet1$A1:A1000]";
        OleDbCommand komut_a = new OleDbCommand(sql_a, baglan);
        OleDbDataReader dr_a = null;
        dr_a = komut_a.ExecuteReader();
        while (dr_a.Read() && dr_b.Read())
        {
            if (dr_a[0].ToString() == comboBox1.SelectedItem.ToString())
            {
                if (!comboBox2.Items.Contains(dr_b[0].ToString()))
                {
                    comboBox2.Items.Add(dr_b[0].ToString());
                }
            }
        }
        baglan.Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM[Sheet1$]  where Cafe like '%" + comboBox2.SelectedItem + "%'", baglan);
        dt.Clear();
        baglan.Open();

        da.Fill(dt);
        dataGridView1.DataSource = dt;

        baglan.Close();
    }
}
}

Change your code like,

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.Items.Clear();
    OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
    baglan.Open();
    string sql_b = "SELECT * FROM [Sheet1$] where [city]=@city";        
    OleDbCommand komut_b = new OleDbCommand(sql_b, baglan);
    komut_b.Parameters.AddWithValue("@city", comboBox1.Text.ToString());
    OleDbDataReader dr_b = null;
    dr_b = komut_b.ExecuteReader();        
    while (dr_b.Read())
    {           
      comboBox2.Items.Add(dr_b[0].ToString());                
    }
    baglan.Close();
}

private void button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
 OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
OleDbCommand comm = new OleDbCommand();
OleDbDataAdapter dAdpter = new OleDbDataAdapter(comm);
comm.Connection = baglan;
comm.CommandText = "SELECT * FROM[Sheet1$]  where Cafe like @cafe and city =@city";   
comm.Parameters.AddWithValue("@cafe", "%" + comboBox2.Text.ToString() + "%"); 
comm.Parameters.AddWithValue("@city",combobox1.Text.ToString());
dAdpter.Fill(dt);
 datagridview.DataSource=dt;
}

I reorganized like that but now when the program run, I can choose city and cafe but When I clicked the button, datagridview is empty.

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox2.Items.Clear();

        OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
        baglan.Open();

        string sql_a = "SELECT * FROM [Sheet1$] where [City]=@City";
        OleDbCommand komut_a = new OleDbCommand(sql_a, baglan);
        komut_a.Parameters.AddWithValue("@City", comboBox1.SelectedItem.ToString());
        OleDbDataReader dr_a = null;
        dr_a = komut_a.ExecuteReader();
        string sql_b = "SELECT * FROM [Sheet1$B1:B1000]";
        OleDbCommand komut_b = new OleDbCommand(sql_b, baglan);
        OleDbDataReader dr_b = null;
        dr_b = komut_b.ExecuteReader();

        while (dr_a.Read() && dr_b.Read())
        {
            if (dr_a[0].ToString() == comboBox1.SelectedItem.ToString())
            {
                if (!comboBox2.Items.Contains(dr_b[0].ToString()))
                {
                    comboBox2.Items.Add(dr_b[0].ToString());
                }
            }
        }
        baglan.Close();


    }

    private void button1_Click(object sender, EventArgs e)
    {


        DataTable dt = new DataTable();
        OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\\users\\toshiba\\desktop\\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
       OleDbCommand comm = new OleDbCommand();
 OleDbDataAdapter dAdpter = new OleDbDataAdapter(comm);
comm.Connection = baglan;
        comm.CommandText = "SELECT * FROM[Sheet1$]  where Cafe like '%@Dormitory%'";
        comm.Parameters.AddWithValue("@Cafe",    comboBox2.SelectedItem.ToString());

        dAdpter.Fill(dt);
        dataGridView1.DataSource = dt;
        baglan.Close();
    }

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