简体   繁体   中英

Retrieve data from excel “csv format” similar to linq take skip

I need to retrieve specific portion from CSV file depend on row number

在此处输入图片说明

Ex: from row number 5 to 12

I'm using OleDbConnection and tried to use this query:

select * from [sheet1.csv$A5:A12]

But it doesn't work.

C# Code:

 var filename = @"C:\Users\A\Desktop\passwords.csv";
        var connString = string.Format(
            @"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""",
            Path.GetDirectoryName(filename)
        );
        using (var conn = new OleDbConnection(connString))
        {
            conn.Open();
            var query = "SELECT * FROM [" + Path.GetFileName(filename) + "$A5:A12]";
            using (var adapter = new OleDbDataAdapter(query, conn))
            {
                var ds = new DataSet("CSV File");
                adapter.Fill(ds);
            }
        }

Error

The Microsoft Jet database engine could not find the object 'passwords.csv$A5:A12'.  Make sure the object exists and that you spell its name and the path name correctly.

This shows an example using both StreamReader (its just a text file remember) and the Jet driver:

http://www.codeproject.com/Articles/27802/Using-OleDb-to-Import-Text-Files-tab-CSV-custom

So lose the "$A5:A12] bit, and use a DataReader instead. Then call the Read method 5 times to move to the fifth row.

I am not a C# programmer just a good googler.

Instead of using OleDbConnection i'm just use File Class

var lines = File.ReadLines(filename);
var rows = lines.Skip(4).Take(8);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM