如何使用LinqToExcel获得不带标题的单元格值 [英] How to use LinqToExcel to get cell values without header
问题描述
我有一个这样的精通...
I have an excel like this...
| ------------ 表格 --------------- |
| ------------------------------------ |
| 名称 ------- 姓氏 ---- |
|鲍勃----------值----------- |
| 年龄 ----------------------------- |
| 30 ------------------------------- |
| ---------- 地址 ------------- |
| ------------------------------------ |
| 街道 ------- 邮政编码-|
|值----------值--------- |
|------------ 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).
如何使用LinqToExcel.dll做到这一点. 而且,如果我的工作表中有一个CheckBox,如何获取所选值(是否选中).
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).
我的代码(c#.net)
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
推荐答案
嗯,据我所知,您的excel文件应该看起来像这样
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.
我希望这对您有用!
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++;
}
}
}
}
这篇关于如何使用LinqToExcel获得不带标题的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!