C# 如何在C中的datagridview中导入多个excel选定文件#
我在WFA中有“导入按钮”来选择要导入的文件,我的代码加载我选择的文件,但它只导入最后一个文件。 这里是加载按钮,我可以在datagridview中加载文件,但我不能导入它们,我只导入最后一个。我如何才能导入所有选定的文件,我真的厌倦了这种协助请帮助 在这里输入代码C# 如何在C中的datagridview中导入多个excel选定文件#,c#,import,C#,Import,我在WFA中有“导入按钮”来选择要导入的文件,我的代码加载我选择的文件,但它只导入最后一个文件。 这里是加载按钮,我可以在datagridview中加载文件,但我不能导入它们,我只导入最后一个。我如何才能导入所有选定的文件,我真的厌倦了这种协助请帮助 在这里输入代码 private void Button1_Click(object sender, EventArgs e) { try { OpenFileDialog ope
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);
}
}
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);
}
}
这是我的导入按钮:
在这里输入代码
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);
}
}
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);
}
}
谢谢:希望我没有误解你的问题。。。 您只能将一个源绑定到一个组合框(我认为) 如果您想为每个combobox项绑定一个表,那么它必须是不同的东西
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);
}
}
我想你需要这个,我有相同的列名和不同的单元格值,我想把行放在后面,你有没有其他的想法,没有merege可能会创建一个包含所有文件的新excel文件。但是我怎样才能用Aspose.cell实现呢你能在代码中告诉我吗