简体   繁体   English

将数据从Excel文件导入C#

[英]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 我有一个Excel文件:City Cafe电话号码纽约Trio Cafe 78654纽约Central Cafe 32344 Washington House Cafe 23222 Washington Central Cafe 11111 LA Wood Cafe 45434 Texas Central Cafe 16564

ComboBox1 contains cities. ComboBox1包含城市。 ComboBox2 contains cafes. ComboBox2包含咖啡馆。 Button1 writes to datagridview. Button1写入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. 我的问题是:例如:我在ComboBox1中选择Washington,然后在Combobox2中选择Central Cafe,然后单击Button1。 I see that: New York Central Cafe 32344 Washington Central Cafe 11111 Texas Central Cafe 16564 in the datagridview. 我看到了:datagridview中的New York Central Cafe 32344 Washington Central Cafe 11111 Texas Central Cafe 16564。 But I chose Washington in the Combobox1.So I want to see only Washington Central Cafe 11111 in the datagridview. 但是我在Combobox1中选择了Washington,所以我只想在datagridview中看到Washington Central Cafe 11111。

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. 我像这样重组,但是现在程序运行时,我可以选择城市和咖啡馆,但是当我单击按钮时,datagridview为空。

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

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

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