如何在没有数据库的新gridview中导入excel后进行内连接 [英] How to inner join after import excel in new gridview without database
本文介绍了如何在没有数据库的新gridview中导入excel后进行内连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个gridview,2个用于导入excel,1个用于内部连接
2 gridview用于导入已完成,但对于内部连接尚未,可以帮助我
谢谢
我尝试过:
i have 3 gridview , 2 for import excel and 1 for inner join
2 gridview for import is done , but for inner join not yet , can help me
thanks
What I have tried:
public partial class Form1 : Form
{
public DataTable ReadExcel(string fileName, string fileExt)
{
string conn = string.Empty;
DataTable dtexcel = new DataTable();
if (fileExt.CompareTo(".xls") == 0)
conn = @"provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties='Excel 8.0;HRD=NO;IMEX=1';"; //for below excel 2007
else
conn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=NO';"; //for above excel 2007
using (OleDbConnection con = new OleDbConnection(conn))
{
try
{
OleDbDataAdapter oleAdpt = new OleDbDataAdapter("select * from [Sheet1$]", con); //here we read data from sheet1
oleAdpt.Fill(dtexcel); //fill excel data into dataTable
}
catch { }
}
return dtexcel;
}
public Form1()
{
InitializeComponent();
// BindGrid();
}
private void button1_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user
{
this.richTextBox1.Text = file.FileName;
filePath = file.FileName; //get the path of the file
fileExt = Path.GetExtension(filePath); //get the file extension
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
{
try
{
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath, fileExt); //read excel file
dataGridView1.Visible = true;
dataGridView1.DataSource = dtExcel;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
MessageBox.Show("format file harus .xls atau .xlsx .", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error
}
}
}
private void button3_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user
{
this.richTextBox2.Text = file.FileName;
filePath = file.FileName; //get the path of the file
fileExt = Path.GetExtension(filePath); //get the file extension
if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0)
{
try
{
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath, fileExt); //read excel file
dataGridView2.Visible = true;
dataGridView2.DataSource = dtExcel;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
MessageBox.Show("format file harus .xls atau .xlsx .", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error
}
}
}
推荐答案
,con) ; //这里我们从sheet1
oleAdpt.Fill(dtexcel)读取数据; //将excel数据填入dataTable
}
catch {}
}
返回dtexcel ;
}
public Form1()
{
InitializeComponent();
// BindGrid();
}
private void button1_Click(object sender,EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); / /打开对话框选择文件
if(file.ShowDialog()== System.Windows.Forms.DialogResult.OK)//如果用户选择了一个文件
{
this .richTextBox1.Text = file.Fi leName;
filePath = file.FileName; //获取文件的路径
fileExt = Path.GetExtension(filePath); //获取文件扩展名
if(fileExt.CompareTo(。xls)== 0 || fileExt.CompareTo(。xlsx)== 0)
{
try
{
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath,fileExt); //读取excel文件
dataGridView1.Visible = true;
dataGridView1.DataSource = dtExcel;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
其他
{
MessageBox.Show(格式文件harus .xls atau .xlsx。,警告,MessageBoxButtons.OK,MessageBoxIcon。错误); //自定义messageBox显示错误
}
}
}
private void button3_Click(object sender,EventArgs e)
{
string filePath = string.Empty;
string fileExt = string.Empty;
OpenFileDialog file = new OpenFileDialog(); //打开对话框选择文件
if(file.ShowDialog()== System.Windows.Forms.DialogResult.OK)//如果用户选择了一个文件
{
this.richTextBox2.Text = file.FileName;
filePath = file.FileName; //获取文件的路径
fileExt = Path.GetExtension(filePath); //获取文件扩展名
if(fileExt.CompareTo(。xls)== 0 || fileExt.CompareTo(。xlsx)== 0)
{
try
{
DataTable dtExcel = new DataTable();
dtExcel = ReadExcel(filePath,fileExt); //读取excel文件
dataGridView2.Visible = true;
dataGridView2.DataSource = dtExcel;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
其他
{
MessageBox.Show(格式文件harus .xls atau .xlsx。,警告,MessageBoxButtons.OK,MessageBoxIcon。错误); //自定义messageBox显示错误
}
}
}
", con); //here we read data from sheet1 oleAdpt.Fill(dtexcel); //fill excel data into dataTable } catch { } } return dtexcel; } public Form1() { InitializeComponent(); // BindGrid(); } private void button1_Click(object sender, EventArgs e) { string filePath = string.Empty; string fileExt = string.Empty; OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user { this.richTextBox1.Text = file.FileName; filePath = file.FileName; //get the path of the file fileExt = Path.GetExtension(filePath); //get the file extension if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0) { try { DataTable dtExcel = new DataTable(); dtExcel = ReadExcel(filePath, fileExt); //read excel file dataGridView1.Visible = true; dataGridView1.DataSource = dtExcel; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else { MessageBox.Show("format file harus .xls atau .xlsx .", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error } } } private void button3_Click(object sender, EventArgs e) { string filePath = string.Empty; string fileExt = string.Empty; OpenFileDialog file = new OpenFileDialog(); //open dialog to choose file if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK) //if there is a file choosen by the user { this.richTextBox2.Text = file.FileName; filePath = file.FileName; //get the path of the file fileExt = Path.GetExtension(filePath); //get the file extension if (fileExt.CompareTo(".xls") == 0 || fileExt.CompareTo(".xlsx") == 0) { try { DataTable dtExcel = new DataTable(); dtExcel = ReadExcel(filePath, fileExt); //read excel file dataGridView2.Visible = true; dataGridView2.DataSource = dtExcel; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } else { MessageBox.Show("format file harus .xls atau .xlsx .", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error); //custom messageBox to show error } } }
你必须适应你的对象,但是一般形式是:
You will have to adapt to your objects, but general form is this:
var joinedTables = from table1 in dt1.AsEnumerable()
join table2 in dt2.AsEnumerable() on (int)table1["CustID"] equals (int)table2["CustID"]
select new
{
CustID = (int)table1["ID"],
ColX = (int)table1["ColX"],
ColY = (int)table1["ColY"],
ColZ = (int)table2["ColZ"]
};
祝你好运。如果有帮助,请接受解决方案。谢谢。
Good luck. Please accept the solution if this helps. Thank you.
这篇关于如何在没有数据库的新gridview中导入excel后进行内连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文