我在 Excel 中有两列,但我想将某些部分导入 C# 程序
I have two colums in Excel but I want to import certain sections to C# program
我的 Excel 是:
region city
iç anadolu ankara
iç anadolu ankara
iç anadolu ankara
iç anadolu ankara
iç anadolu kayseri
marmara istanbul
marmara istanbul
marmara istanbul
marmara bursa
marmara bursa
我想当在combobox1中选择iç anadolu
时,只有ankara
和kayseri
出现在combobox2中,当在combobox1中选择marmara
时,只有ankara
和kayseri
出现istanbul
和 bursa
出现在 combobox2 中。
我该怎么做?
这是我的代码:
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 trfd
{
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_ofy\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
private void Form1_Load(object sender, EventArgs e)
{
baglan.Open();
string sql = "SELECT * FROM [Sheet1$A1:A100]";
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());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
按照下面的代码
public Form1()
{
InitializeComponent();
OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\d.faldu\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\tempExcel; Extended Properties='Excel 12.0 xml;HDR=YES;'");
baglan.Open();
string sql = "SELECT * FROM [Sheet1$A1:A100]";
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\d.faldu\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\tempExcel; Extended Properties='Excel 12.0 xml;HDR=YES;'");
baglan.Open();
string sql = "SELECT * FROM [Sheet1$B1:B100]";
OleDbCommand komut = new OleDbCommand(sql, baglan);
OleDbDataReader dr = null;
dr = komut.ExecuteReader();
string sql_2 = "SELECT * FROM [Sheet1$A1:A100]";
OleDbCommand komut_2 = new OleDbCommand(sql_2, baglan);
OleDbDataReader dr_2 = null;
dr_2 = komut_2.ExecuteReader();
while (dr_2.Read() && dr.Read())
{
if (dr_2[0].ToString() == comboBox1.SelectedItem.ToString())
{
if (!comboBox2.Items.Contains(dr[0].ToString()))
{
comboBox2.Items.Add(dr[0].ToString());
}
}
}
baglan.Close();
}
我的 Excel 是:
region city iç anadolu ankara iç anadolu ankara iç anadolu ankara iç anadolu ankara iç anadolu kayseri marmara istanbul marmara istanbul marmara istanbul marmara bursa marmara bursa
我想当在combobox1中选择iç anadolu
时,只有ankara
和kayseri
出现在combobox2中,当在combobox1中选择marmara
时,只有ankara
和kayseri
出现istanbul
和 bursa
出现在 combobox2 中。
我该怎么做?
这是我的代码:
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 trfd
{
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_ofy\proje-1; Extended Properties='Excel 12.0 xml;HDR=YES;'");
private void Form1_Load(object sender, EventArgs e)
{
baglan.Open();
string sql = "SELECT * FROM [Sheet1$A1:A100]";
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());
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
} }
按照下面的代码
public Form1()
{
InitializeComponent();
OleDbConnection baglan = new OleDbConnection(@"Provider =Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\d.faldu\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\tempExcel; Extended Properties='Excel 12.0 xml;HDR=YES;'");
baglan.Open();
string sql = "SELECT * FROM [Sheet1$A1:A100]";
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\d.faldu\Desktop\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\tempExcel; Extended Properties='Excel 12.0 xml;HDR=YES;'");
baglan.Open();
string sql = "SELECT * FROM [Sheet1$B1:B100]";
OleDbCommand komut = new OleDbCommand(sql, baglan);
OleDbDataReader dr = null;
dr = komut.ExecuteReader();
string sql_2 = "SELECT * FROM [Sheet1$A1:A100]";
OleDbCommand komut_2 = new OleDbCommand(sql_2, baglan);
OleDbDataReader dr_2 = null;
dr_2 = komut_2.ExecuteReader();
while (dr_2.Read() && dr.Read())
{
if (dr_2[0].ToString() == comboBox1.SelectedItem.ToString())
{
if (!comboBox2.Items.Contains(dr[0].ToString()))
{
comboBox2.Items.Add(dr[0].ToString());
}
}
}
baglan.Close();
}