using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace ReadingExcel
{
public class Program
{
public static void Main(string[] k)
{
string connectionString = @"Provider= Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SaiKiran\Desktop\MyExl.xlsx;Extended Properties=Excel 12.0 Xml";
//Create the connection
System.Data.OleDb.OleDbConnection excelConnection = new System.Data.OleDb.OleDbConnection(connectionString);
string excelQuery = @"Select * from [Sheet1$]";
//string excelQuery1 = @"Select * from [Sheet1$]";
System.Data.OleDb.OleDbCommand excelCommand = new System.Data.OleDb.OleDbCommand(excelQuery, excelConnection);
excelConnection.Open();
System.Data.OleDb.OleDbDataReader excelReader;
excelReader = excelCommand.ExecuteReader();
while (excelReader.Read())
{
//Globals.Sheet1.Cells[1, 3] = "Set my data";
Console.WriteLine("id={0}", excelReader[0].ToString());
Console.ReadLine();
}
excelConnection.Close();
}
}
}
我的 excel 中有 A、B、C 列,它们是 .xlsx 格式。使用当前代码,我现在只能读取一列。请告诉我循环或阅读所有列的内容。