c#讀取100個表格sheet1指定數據
时间: 2024-01-23 08:03:21 浏览: 8
您好,您可以使用C#中的OleDb來读取Excel文件中的数据。以下是一个示例代码,可以读取Excel文件中的第一个sheet中的前100行数据。
```csharp
using System.Data.OleDb;
string fileName = "yourExcelFile.xlsx";
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT TOP 100 * FROM [Sheet1$]", connection);
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
// Access data here using reader.GetValue() method
}
}
```
请注意,此示例假定Excel文件的第一个sheet名称为"Sheet1",如果您的sheet名称不同,请将查询语句中的sheet名称更改为真实的sheet名称。