Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并从Excel文件中读取值_C#_.net_Visual Studio_Visual Studio 2010_Excel - Fatal编程技术网

C# 并从Excel文件中读取值

C# 并从Excel文件中读取值,c#,.net,visual-studio,visual-studio-2010,excel,C#,.net,Visual Studio,Visual Studio 2010,Excel,我有一个Excel文件,列有名称(数据源不是由我控制的,它是由客户机提供给我的)。尽管列发生了更改,但列标题从未更改 在文件中,它被称为“名字” 如何访问同一列中每个单元格中的数据?我将看一看Microsoft提供的示例: 您可以这样做,使用ODBC连接到文件并下载工作表的内容 private bool DownloadExcelData(string fileName, ref DataTable informationDT) { //

我有一个Excel文件,列有名称(数据源不是由我控制的,它是由客户机提供给我的)。尽管列发生了更改,但列标题从未更改

在文件中,它被称为“名字”


如何访问同一列中每个单元格中的数据?

我将看一看Microsoft提供的示例:


您可以这样做,使用ODBC连接到文件并下载工作表的内容

 private bool DownloadExcelData(string fileName, ref DataTable informationDT)
            {
                // bool success
                bool success = true;

                // open the file via odbc
                string connection = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
                connection = String.Format(connection, FilePath + fileName);
                OleDbConnection conn = new OleDbConnection(connection);
                conn.Open();

                try
                {
                    // retrieve the records from the first page
                    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Information$]", conn);
                    OleDbDataAdapter adpt = new OleDbDataAdapter(cmd);
                    adpt.Fill(informationDT);
                }
                catch { success = false; }

                // close the connection
                conn.Close();
                return success;
            }
以下是xls和xlsx文件的一些ODBC连接示例:

<add name="xls" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;'" />
    <add name="xlsx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0" />


我过去曾成功地使用读取Excel文件

我过去使用过,发现它很容易使用。

将Excel文件作为数据库打开。这样您就不必担心柱的位置:

string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyExcelFile.xls;Extended Properties=\"Excel 8.0;HDR=YES\"";
using (var conn = new System.Data.OleDb.OleDbConnection(connString)) {
    conn.Open();
    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("Select * From [SheetName$]", conn);
    OleDbDataReader reader = cmd.ExecuteReader();
    int firstNameOrdinal = reader.GetOrdinal("First Name");
    int lastNameOrdinal = reader.GetOrdinal("Last Name");
    while (reader.Read()) {
        Console.WriteLine("First Name: {0}, Last Name: {1}", 
            reader.GetString(firstNameOrdinal), 
            reader.GetString(lastNameOrdinal));
    }
}

提示:Excel列“标题”与数据库列名不同——它们只是带有“标题”文本的常规单元格。因此,您需要读取第一行单元格中的文本以获得“标题”。在第一行的单元格之间循环,以查找具有所需标题文本的单元格。然后,一旦您找到一个text=column name的单元格,您就知道它下面的列包含数据。这对我来说很有用,尽管我不得不将构建配置从“AnyCPU”更改为“x86”。显然,运行x64时,Microsoft.Jet.OLEDB.4.0不可用。是的,不幸的是,Jet没有64位接口。