[英]How to import many excel selected files in datagridview in C#
I have "import button" in my WFA to select which file I want to import it, my code loads the files which I selected,but it import only the last one.我的 WFA 中有“导入按钮”到 select 我要导入哪个文件,我的代码加载了我选择的文件,但它只导入最后一个文件。 Here is load button I can load files in datagridview but I can not import them I import just the last one.
这是加载按钮我可以在 datagridview 中加载文件但我不能导入它们我只导入最后一个。 How I can do to import all selected files, I'm really tired of this assigenment please help.
我如何才能导入所有选定的文件,我真的厌倦了这种分配,请帮忙。
enter code here在此处输入代码
private void Button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "XML Files, Text Files, Excel Files|*.xlsx; *.xls; *.xml; *.txt; ";
openFileDialog1.Multiselect = true;
DataTable table = new DataTable();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
//tb_path is textbox
tb_path.Text = file;
// excelFilePath_com = tb_path.Text;
string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
OleDbConnection con = new OleDbConnection(constr);
con.Open();
DataTable dt1 = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
table.Merge(dt1);
drop_down_sheet.DataSource = dt1;
//dro_down_sheet is combobox to choose which sheet to import
drop_down_sheet.DisplayMember = "TABLE_NAME";
drop_down_sheet.ValueMember = "TABLE_NAME";
}
}
dataGridView1.DataSource = table;
}
catch (Exception e1)
{
MessageBox.Show(e1.Message + e1.StackTrace);
}
}
And here is my import button:这是我的导入按钮:
enter code here在此处输入代码
private void Button1_Click(object sender, EventArgs e)
{
try
{
string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From[" + drop_down_sheet.SelectedValue + "]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
dataGridView1.DataSource = dt;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
Thank you:谢谢:
Hope that i didnt misunderstood your question... You can only bind 1 source to 1 combobox(I think) Incase you want to bind 1 table per each combobox item, its must be a different thing.希望我没有误解您的问题...您只能将 1 个源绑定到 1 个组合框(我认为)如果您想为每个 combobox 项目绑定 1 个表,它必须是不同的东西。
private void Button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "XML Files, Text Files, Excel Files|*.xlsx; *.xls; *.xml; *.txt; ";
openFileDialog1.Multiselect = true;
DataTable table = new DataTable();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
//tb_path is textbox
tb_path.Text = file;
// excelFilePath_com = tb_path.Text;
string constr = string.Format("Provider = Microsoft.ACE.OLEDB.12.0; Data Source =" + tb_path.Text + ";Extended Properties = \"Excel 12.0; HDR=Yes;\"; ");
OleDbConnection con = new OleDbConnection(constr);
con.Open();
DataTable dt1 = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
table.Merge(dt1);
}
}
drop_down_sheet.DataSource = table;
//dro_down_sheet is combobox to choose which sheet to import
drop_down_sheet.DisplayMember = "TABLE_NAME";
drop_down_sheet.ValueMember = "TABLE_NAME";
dataGridView1.DataSource = table;
}
catch (Exception e1)
{
MessageBox.Show(e1.Message + e1.StackTrace);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.