【发布时间】:2019-05-05 04:24:38
【问题描述】:
C# Windows 窗体。
Proyect:导入 2 Excel 并选择包含表格的工作表,然后比较两个文件中的“代码”列,并在第三个 dataGridView 上显示匹配的文件及其信息。
DGV1
Code Date Tipe Price Account
3367 02-feb-18 NEW 25 N/A
8543 04-feb-18 NEW 25 N/A
3367 05-feb-18 RENEW 50 N/A
5542 07-feb-18 NEW 75 N/A
1069 27-jan-18 NEW 25 N/A
DGV2
City Code
Texas 3367
Texas 8543
Texas 5542
Texas 8673
DGV3
Code Date Tipe Price Account
3367 02-feb-18 NEW 25 N/A
3367 05-feb-18 RENEW 50 N/A
8543 04-feb-18 NEW 25 N/A
5542 07-feb-18 NEW 75 N/A
目前我正在阅读本指南:
https://forgetcode.com/CSharp/1508-Comparing-two-datatables-and-returning-a-datatable-by-matching-one-or-more-columns-using-LINQ#
但我还不明白。希望有人能帮助我解释剩下的部分。
private void btnSelect1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.tBox1.Text = openfiledialog1.FileName;
}
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox1.Text + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
OleDbConnection con = new OleDbConnection(constr);
con.Open();
dropdown_sheet1.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
dropdown_sheet1.DisplayMember = "TABLE_NAME";
dropdown_sheet1.ValueMember = "TABLE_NAME";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void btnLoad1_Click(object sender, EventArgs e)
{
try
{
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox1.Text + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + dropdown_sheet1.SelectedValue + "]", con);
DataTable dt1 = new DataTable();
sda.Fill(dt1);
foreach (DataRow row in dt1.Rows)
{
dataGridView1.DataSource = dt1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnSelect2_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openfiledialog2 = new OpenFileDialog();
openfiledialog2.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
if (openfiledialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.tBox2.Text = openfiledialog2.FileName;
}
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox2.Text + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
OleDbConnection con = new OleDbConnection(constr);
con.Open();
dropdown_sheet2.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
dropdown_sheet2.DisplayMember = "TABLE_NAME";
dropdown_sheet2.ValueMember = "TABLE_NAME";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void btnLoad2_Click(object sender, EventArgs e)
{
try
{
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + tBox2.Text + ";Extended Properties = \"Excel 12.0; HDR=YES;\" ; ";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + dropdown_sheet2.SelectedValue + "]", con);
DataTable dt2 = new DataTable();
sda.Fill(dt2);
foreach (DataRow row in dt2.Rows)
{
dataGridView2.DataSource = dt2;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我已经有了导入和工作表选择,但我不明白比较。
【问题讨论】:
标签: c# excel import datagridview compare