c#读取excel指定列的所有数据
时间: 2024-10-27 10:14:21 浏览: 82
在C#中读取Excel指定列的所有数据,可以使用以下步骤:
1. 使用OleDbConnection连接Excel文件,获取Excel数据。
2. 使用OleDbDataAdapter将Excel数据填充到DataSet中。
3. 从DataSet中获取指定列的数据。
以下是一个示例代码,假设要获取Excel文件中第一列的所有数据:
```
public List<string> ReadExcelColumn(string filePath)
{
List<string> columnData = new List<string>();
string connectionString = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT * FROM [Sheet1$]", connection);
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
columnData.Add(reader.GetString(0));
}
}
}
return columnData;
}
```
这个方法将返回一个List<string>,其中包含Excel文件中第一列的所有数据。
阅读全文
相关推荐


















