【发布时间】:2017-08-24 03:06:57
【问题描述】:
我想从多个 Excel 单元格中获取数据并将其连接起来,使其成为一个大字符串。
单元格 1 和 2 中的原始字符串示例:
单元格1:
the_red_bucket_01, the_blue_duck_01,_the green_banana_02, the orange_bear_01
单元格2:
the_purple_chair_01, the_blue_coyote_01,_the green_banana_02, the orange_bear_01
我希望新列表看起来像这样,每个项目都是列表中自己的字符串对象。一旦数据加入一个大列表,我想将列表中的每个项目分隔成它自己的对象,方法是在逗号后分隔每个项目:
the_red_bucket_01
the_blue_duck_01
the green_banana_02
the orange_bear_01
the_red_chair_01
the_blue_coyote_01
the green_banana_02
the orange_bear_01
然后我想删除重复项,以便控制台只显示每个项目的 1,无论它们有多少。
现在我的代码将每个单元格视为两个单独的列表,尽管列表中的每个对象都是独立的。
例如,如果我想要列表中的第一项,它会返回 cell1 和 cell2 中的第一项,而我只想要一个大列表并返回一个项目。 我似乎无法让我的 foreach/if 语句起作用。 .
List<string> fn = new List<string>();
foreach (Excel.Range itemo in xlRng)
{
string itemString = (string)itemo.Text;
fn.AddRange((from each in itemString.Split(',')
select each.Trim()).ToArray<string>());
}
foreach (string combo in fn.Distinct())
{
Console.WriteLine(combo);
}
【问题讨论】:
标签: c# excel visual-studio list