0
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 格式。使用当前代码,我现在只能读取一列。请告诉我循环或阅读所有列的内容。

4

1 回答 1

0

已经来不及回答了。但它可以帮助寻找类似解决方案的人。

这是给出的一种解决方案:http: //support.microsoft.com/kb/302096

于 2013-12-06T13:03:58.227 回答