将 excel 个单元格范围内的值添加到 <List>
Adding a value to <List> from range of excel cells
数据取自 Excel,使用 ClosedXML。问题是我不知道列中会有多少个值。也许5,也许更多。因此,我希望 ClosedXML 考虑范围内所有填充的单元格并将它们添加到列表中。这是我的代码现在的样子:我从每个单元格中手动获取值。
var tasks = workbook.Worksheet(1);
Tasks = new List<Task>()
{
new Task()
{
TaskName = tasks.Cell("A2").GetFormattedString(),
TaskStart = tasks.Cell("B2").GetFormattedString(),
TaskEnd = tasks.Cell("C2").GetFormattedString(),
TaskStatus = tasks.Cell("D2").GetFormattedString()
},
new Task()
{
TaskName = tasks.Cell("A3").GetFormattedString(),
TaskStart = tasks.Cell("B3").GetFormattedString(),
TaskEnd = tasks.Cell("C3").GetFormattedString(),
TaskStatus = tasks.Cell("D3").GetFormattedString()
}}
您可以检查您的单元格中是否有内容,如果是则添加任务。由于列是线性的,因此增加一个计数器并将其用作单元格名称的一部分。
var tasks = workbook.Worksheet(1);
var Tasks = new List<Task>();
int loopCount = 2; //your starting row
while(tasks.Cell("A" + loopCount).GetFormattedString().Length > 0)
{
Tasks.Add(new Task()
{
TaskName = tasks.Cell("A" + loopCount).GetFormattedString(),
TaskStart = tasks.Cell("B" + loopCount).GetFormattedString(),
TaskEnd = tasks.Cell("C" + loopCount).GetFormattedString(),
TaskStatus = tasks.Cell("D" + loopCount).GetFormattedString()
}
loopCount++;
}
我希望这是清楚的,并能帮助您朝着正确的方向前进。
我的建议是使用 LINQ 并循环访问 ws.FirstCell().CurrentRegion.RowsUsed()
集合。
using (var wb = new XLWorkbook("test2.xlsx"))
{
var ws = wb.Worksheets.First();
var tasks = ws.FirstCell().CurrentRegion.RowsUsed()
.Select(row => new Task()
{
TaskName = row.Cell(1).GetFormattedString(),
TaskStart = row.Cell(2).GetFormattedString(),
TaskEnd = row.Cell(3).GetFormattedString(),
TaskStatus = row.Cell(4).GetFormattedString()
})
.ToList();
}
数据取自 Excel,使用 ClosedXML。问题是我不知道列中会有多少个值。也许5,也许更多。因此,我希望 ClosedXML 考虑范围内所有填充的单元格并将它们添加到列表中。这是我的代码现在的样子:我从每个单元格中手动获取值。
var tasks = workbook.Worksheet(1);
Tasks = new List<Task>()
{
new Task()
{
TaskName = tasks.Cell("A2").GetFormattedString(),
TaskStart = tasks.Cell("B2").GetFormattedString(),
TaskEnd = tasks.Cell("C2").GetFormattedString(),
TaskStatus = tasks.Cell("D2").GetFormattedString()
},
new Task()
{
TaskName = tasks.Cell("A3").GetFormattedString(),
TaskStart = tasks.Cell("B3").GetFormattedString(),
TaskEnd = tasks.Cell("C3").GetFormattedString(),
TaskStatus = tasks.Cell("D3").GetFormattedString()
}}
您可以检查您的单元格中是否有内容,如果是则添加任务。由于列是线性的,因此增加一个计数器并将其用作单元格名称的一部分。
var tasks = workbook.Worksheet(1);
var Tasks = new List<Task>();
int loopCount = 2; //your starting row
while(tasks.Cell("A" + loopCount).GetFormattedString().Length > 0)
{
Tasks.Add(new Task()
{
TaskName = tasks.Cell("A" + loopCount).GetFormattedString(),
TaskStart = tasks.Cell("B" + loopCount).GetFormattedString(),
TaskEnd = tasks.Cell("C" + loopCount).GetFormattedString(),
TaskStatus = tasks.Cell("D" + loopCount).GetFormattedString()
}
loopCount++;
}
我希望这是清楚的,并能帮助您朝着正确的方向前进。
我的建议是使用 LINQ 并循环访问 ws.FirstCell().CurrentRegion.RowsUsed()
集合。
using (var wb = new XLWorkbook("test2.xlsx"))
{
var ws = wb.Worksheets.First();
var tasks = ws.FirstCell().CurrentRegion.RowsUsed()
.Select(row => new Task()
{
TaskName = row.Cell(1).GetFormattedString(),
TaskStart = row.Cell(2).GetFormattedString(),
TaskEnd = row.Cell(3).GetFormattedString(),
TaskStatus = row.Cell(4).GetFormattedString()
})
.ToList();
}