c# - 如何读取文件夹下的excel文件名和扩展名(xls,xlsx)?
问题描述
是否可以读取文件夹下的所有 excel 文件名和 ext(xls,xlsx)?
这是文件夹名称,我想读取所有 excel 文件名和 ext(xls,xlsx)
下面的代码是我尝试过的,但我想用 ext 读取所有文件名。任何人都可以给出最好的建议来获得解决方案。
private static void ReadExcelFileSheetWithOleDb()
{
//Microsoft.Jet.OLEDB.4.0 --- Microsoft.ACE.OLEDB.12.0
//D:\\List_of_cid.xlsx
string con = string.Empty;
//string ext = Path.GetExtension("./Excel_Folder/List_of_cid.xlsx").ToLower();
//string path = Path.GetFullPath("~/Excel_Folder/").ToString();
//string[] fileArrayxls = Directory.GetFiles(@"~\Excel_Folder\", "*.xls");
//string[] fileArrayxlsx = Directory.GetFiles(@"~\Excel_Folder\", "*.xlsx");
string ext = Path.GetExtension("D:\\List_of_cid.xlsx").ToLower();
if (ext.Trim() == ".xls")
{
// con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + "; Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\List_of_cid.xlsx; Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
}
else if (ext.Trim() == ".xlsx")
{
con = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\List_of_cid.xlsx; Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";
}
using (OleDbConnection oledbConn = new OleDbConnection(con))
{
// OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "List_of_cid");
foreach (var m in ds.Tables[0].DefaultView)
{
Console.WriteLine(((DataRowView)m).Row.ItemArray[0] + " " + ((DataRowView)m).Row.ItemArray[1]);
}
// oledbConn.Close();
}
catch (Exception e)
{
Console.WriteLine("Error :" + e.Message);
}
}
}
我是编码新手。
解决方案
推荐阅读
- opencv - OpenCV vs 3rd 方人脸识别 SDK
- automated-tests - 由于从 UFT 中的 UI 自动化继承的 API 调用缺失或损坏而失败
- php - 服务器端缓存与客户端缓存以维护键值映射
- python - 在我自己的笔记本电脑上连接到在树莓派上运行的 MySQL
- android - 在同一活动中打开片段
- gwt - gxt ComboBox 的 addValueChangeHandler 仅在关注另一个字段时触发
- mysql - 在 MySQL 视图中,以 union 开头还是以 union 结尾更快?
- nlp - 有没有更好的方法从 Twitter 数据中检测个性?
- python - 如何对多维字典中的值求和?
- firebase - 使用 Firebase 电子邮件链接登录进行 e2e 测试?