c# - 如何打开多个excel文件并在DataGridView中打开它们
问题描述
我正在编写一个程序来过滤许多 Excel 文件并在 DataGridView 中显示结果。我从OpenFileDialog
(我可以选择一个文件路径)开始显示我的过滤器有效,但现在我想选择许多文件并打开它们DataGridView
然后过滤它们。
WFA (Visual Studio) 中是否有执行此操作的组件?我的意思是像 OpenFileDialog 但它打开/选择许多文件?我尝试使用此代码,但它显示了最后选择的文件,谢谢!
在这里输入代码
private void Button2_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;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
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();
drop_down_sheet.DataSource = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
drop_down_sheet.DisplayMember = "TABLE_NAME";
drop_down_sheet.ValueMember = "TABLE_NAME";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
}
解决方案
OpenFileDialog 控件具有Multiselect
允许您在同一个对话框中选择多个文件的属性。
openFileDialog1.Filter = "Excel files|*.xlsx|All files|*.*";
openFileDialog1.Multiselect = true;
然后稍后您可以使用该属性Filenames
(而不是Filename
)来检索您的文件:
foreach (string filePath in openFileDialog1.FileNames)
{
// ...
}
查看 MSDN了解更多信息。
推荐阅读
- dictionary - 在 pyspark rdd.mapPartitions(f) 中,f 应该是生成器吗?
- javascript - Javascript/Jquery:根据输入类型的值或空值计算值
- visual-studio - Azure (C#) System.IO.FileNotFoundException:找不到文件
- xen - 无法确定 /sys 是否为挂载点:错误的文件描述符
- python - `and` 和 `or` 语句的意外结果
- javascript - 如何在javascript中添加列
- javascript - 如何在 Ajax 中调用此数据块?
- null - 从 Null 指针加载,为什么这不会使我的程序崩溃?
- google-apps-script - 仅当单元格显示“是”时才获取填充单元格的日期
- python - 如何使用数据集(nd.array)在散点图中对误差条(x 和 y)进行颜色映射?