简体   繁体   English

如何使用OLEDB for C#在Excel中执行“从[sheet1 $]其中列为L的*中选择*”?

[英]How can I do this “select * from [sheet1$] where column is L” in Excel using OLEDB for C#?

I had asked about this question before but since the requirements changed, I am going to seek for answers again. 我之前曾问过这个问题 ,但是由于要求已更改,因此我将再次寻求答案。

I am trying to get all of the contents within the L column of Excel or anything under the DocumentNo heading, which by the way is placed 7 cells below the first row. 我试图在Excel的L列中或DocumentNo标题下的所有内容中获取所有内容,顺便说一下,该内容位于第一行下方的7个单元格中。 So the DocumentNo data is at L:7. 因此,DocumentNo数据位于L:7。 It is followed by a blank cell, then cells of document numbers. 它后面是空白单元格,然后是文档编号的单元格。 I want to get all of the document numbers and place it in an array to be used in succeeding functions. 我想获取所有文档编号,并将其放置在数组中以在后续功能中使用。

Help on this please. 请对此提供帮助。 Thanks. 谢谢。

You can do this using a range modifier to tell it where to go 您可以使用范围修饰符来告诉它去哪里

For example if you want just L column in your example 例如,如果您只想在示例中使用L列

SELECT DocumentNo
FROM [sheet1$L7:Ll40]

This is assuming that the document numbers end at L140. 假设文档编号以L140结尾。

   try          
        {      
            //Create a OLEDB connection for Excel file    
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +  
                "Data Source=" + "d:\\data.xls" + ";" +  
                "Extended Properties=Excel 8.0;";   
            OleDbConnection objConn = new OleDbConnection(connectionString); 
            objConn.Open();               
            // Creating a command object to read the values from Excel file   
            OleDbCommand ObjCommand = new OleDbCommand("SELECT DocumentNo FROM [Sheet1$]", objConn);  
            // Creating a Read object             
            OleDbDataReader objReader = ObjCommand.ExecuteReader();  

            // Looping through the values and displaying   

            //if (objReader.

            while (objReader.Read())       
            {

                object obj = objReader["DocumentNo"];

            }                
            //Disposing the objects  
            objReader.Dispose();   
            ObjCommand.Dispose();  
            objConn.Dispose();     
        }         
        catch (Exception ex)     
        {            
            MessageBox.Show(ex.Message); 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用oledb C#知道哪个Excel工作表是空的? - how to know which excel sheet is empty using oledb c#? 使用OLEDB适配器从Excel工作表中选择 - Select from Excel Sheet using OLEDB Adapter 我想使用oleDB select命令从Excel工作表中选择具有多字列名的列? - I want to select column from a Excel sheet which has multi word coloumn name using oleDB select command? 在C#中使用OLEDB从Excel选择单元格地址 - Select cell adress from Excel using OLEDB in C# 如何使用oledb在C#中将下拉列数据插入到Excel中 - how to insert dropdown column data into excel in c# using oledb 如何从c#中的excel表中选择特定列? - How can i select specific columns from excel sheet in c#? 如何在查询C#的Excel工作表中选择一组行和列? - How can I select a set of rows and columns from an excel sheet in query for C#? C#-使用Oledb在Excel中获取特定工作表的NamedRanges - C# - Get NamedRanges of a particular sheet in Excel using Oledb 如何使用c#在VS 2012中使用Microsoft.Ace.Oledb在Excel工作表中插入新行 - How to insert a new Row in Excel sheet using Microsoft.Ace.Oledb in VS 2012 using c# 如何将 excel 工作表的最后一列值复制到另一个 excel 工作表的最后一列中 - How do I copy last column value of an excel sheet to another excel sheet's last column using asp.net c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM