Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinqToExcel - Need to start at a specific row

I'm using the LinqToExcel library. Working great so far, except that I need to start the query at a specific row. This is because the excel spreadsheet from the client uses some images and "header" information at the top of the excel file before the data actually starts.

The data itself will be simple to read and is fairly generic, I just need to know how to tell the ExcelQueryFactory to start at a specific row.

I am aware of the WorksheetRange<Company>("B3", "G10") option, but I don't want to specify an ending row, just where to start reading the file.

Using the latest v. of LinqToExcel with C#

like image 487
contactmatt Avatar asked Aug 31 '12 16:08

contactmatt


2 Answers

I just tried this code and it seemed to work just fine:

var book = new LinqToExcel.ExcelQueryFactory(@"E:\Temporary\Book1.xlsx");

var query =
    from row in book.WorksheetRange("A4", "B16384")
    select new
    {
        Name = row["Name"].Cast<string>(),
        Age = row["Age"].Cast<int>(),
    };

I only got back the rows with data.

like image 66
Enigmativity Avatar answered Sep 27 '22 17:09

Enigmativity


I suppose that you already solved this, but maybe for others - looks like you can use

var excel = new ExcelQueryFactory(path);
var allRows = excel.WorksheetNoHeader();

//start from 3rd row (zero-based indexing), length = allRows.Count() or computed range of rows you want
for (int i = 2; i < length; i++)
{
    RowNoHeader row = allRows.ElementAtOrDefault(i);
    //process the row - access columns as you want - also zero-based indexing
}

Not as simple as specifying some Range("B3", ...), but also the way. Hope this helps at least somebody ;)

like image 43
podvlada Avatar answered Sep 27 '22 17:09

podvlada