【发布时间】:2014-11-03 21:25:42
【问题描述】:
我想将我的文本文件 https://imageshack.com/i/eyRUOp6fj 转换为特定的 excel 格式,如下所示 https://imageshack.com/i/iqVnLA9vj。我该怎么做。
在文本文件中,有许多组件具有相同的位置名称。所以基本上我想将具有相同位置名称的所有组件聚集到一行,第二列中的组件总数等等。请指导我如何做到这一点。
代码片段:
string[] lines = File.ReadAllLines(@"c:\bleh\testdata.txt");
List<Item> allItems = new List<Item>(lines.Length);
Dictionary<string, List<Item>> itemsByLocation = new Dictionary<string, List<Item>> (StringComparer.OrdinalIgnoreCase);
// loop the file, start at 1 assuming headings first row
for (int i = 1; i < lines.Length; i++)
{
// nothing interesting here, just parsing the file
string[] columns = lines[i].Split(new char[] { ';', ',' });
Item item = new Item() {
Designator = columns[ORDINAL_DESIGNATOR],
MaxPn = columns[ORDINAL_MAXPN],
Footprint = columns[ORDINAL_FOOTPRINT],
Location = columns[ORDINAL_LOCATION] };
allItems.Add(item);
List<Item> itemsForThisKey = null;
if (itemsByLocation.TryGetValue(item.Location, out itemsForThisKey) == false)
{
// we don't already have this location in the dictionary, add it
itemsForThisKey = new List<Item>();
itemsByLocation.Add(item.Location, itemsForThisKey);
}
itemsForThisKey.Add(item); // add this item to the relevant grouping
}
我在项目 Error 1 The type or namespace name 'Item' could not be found (are you missing a using directive or an assembly reference?) 中遇到错误
另外,Error 7 The name 'ORDINAL_DESIGNATOR' does not exist in the current context 的错误请有帮助!!!
非常感谢。
【问题讨论】:
-
你已经尝试过做什么?您应该向我们展示您为解决问题所做的努力。
-
@furkle 我只需要一个建议,我可以从哪里开始。谢谢
标签: c# excel visual-studio-2010 visual-studio grouping