'How to use LinqToExcel to get cell values without header
I have an excel like this...
|------------ Form ---------------|
|------------------------------------|
| Name ------- Last Name----|
| Bob ---------- value -----------|
| Age -----------------------------|
| 30 -------------------------------|
|---------- Address -------------|
|------------------------------------|
| Street ------- Postal code--|
| value ---------- value ---------|
..and so on, like a register form, as you can see the value that i need to get, is in the next row(under the title).
How can I do that with LinqToExcel.dll. And if I had a CheckBox in my sheet, how can i get the selected value (checked or not).
My code (c# .net)
var rows = from c in excel.WorksheetNoHeader(sheetName)
select c;
foreach (var item in rows)
{
string aux = item[0].Value.ToString();
}
Ty
Solution 1:[1]
Well for what I understood your excel file should look something like this

I implemented some code to read this record, but I think it should work for more than only one. As you can see, I am iterating into the set of rows and avoiding odd rows assuming that they are the titles.
I hope this works for you!
namespace Demo.Stackoverflow
{
using System;
using System.Collections.Generic;
using System.Linq;
using LinqToExcel;
public class Person
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
LoadExcel();
Console.ReadLine();
}
private static void LoadExcel()
{
var directorio = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Book.xls");
var book = new ExcelQueryFactory(directorio);
var rows = from c in book.WorksheetNoHeader()
select c;
List<Person> people = new List<Person>();
int i = 1;
foreach (var row in rows)
{
if (i % 2 == 0)
{
if (people.Count != 0 && people.Last().Age == 0)
{
people.Last().Age = Convert.ToInt32(row[0].Value.ToString());
}
else
{
Person per = new Person()
{
Name = row[0].Value.ToString(),
LastName = row[1].Value.ToString()
};
people.Add(per);
}
}
i++;
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | luis_laurent |
