使用C#在for循环中创建泛型列表的对象 [英] Create Object Of Generic List in for loop using C#

查看:174
本文介绍了使用C#在for循环中创建泛型列表的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友......



我参与了一个项目,我需要编写代码来读取excel文件并将数据保存在另一个excel文件中。但Source excel文件可以包含任意数量的列,即Somtetimes它可能包含3个文件列,或者有时它可能包含8个文件列,如下所示。



< pre lang =text> Col-A Col-B Col-C
1 3 5
2 4 6





源Excel文件的列是动态的。但我知道他们的数据类型。一切都是字符串。这些列可以包含任意数量的值。所有列彼此之间具有固定数量的行。假设所有列可能有2000行。我需要将目标文件中的列保存为低于其他列的值,如下所示。



列值
Col- A $
Col-A 2
Col-B 3
Col-B 4
Col-C 5
Col-C 6





为此我需要计算源文件中的列数,并且我在计算源文件中的列数时成功了。但我无法在运行时为它们创建动态列表对象,以将它们的值存储在相应的列表对象中。请帮忙。我想要C#代码。

解决方案

首先收集列标题的列表< string>

然后创建一个 List< string> 的数组或列表,其中包含与每列对应的条目。

当你浏览文件,当您从一行中获取每个值时,将其添加到相应列的列表< string>

当您完成读取后,您可以将每个列的值输出为具有列名称的行和每个值的值。


我将值存储在Dictionary< string,List<字符串>>,我只会存储每列实际使用的行数值的Cell数据。



您可以使用'UsedRange函数in Excel确定(对于整个工作表)最后一行使用的是什么。请注意,'UsedRange将返回一个Range,其中包含任何点上的每个Cell都有一个值,与Cell现在是否有值无关。



并且,有些您可以编程(理论上......我自己没有这样做),为每列计算第一列和最后一行在该列中具有真实数据的行;见:[ ^ ]。



然而,这种方法会可能意味着你想写这样的东西:

  public  Dictionary< string,List< dctColNameToColData>> ;; 

public class XLColumnData
{
public string 名称{ set ; get ; }

public int StartRow {设置; get ; }

public int EndRow {设置; get ; }

public List< string> ColumnData { set ; get ; }

public XLColumnData( string name, int srow, int erow,List< string> cdata)
{
// 在这里初始化...
}
}

我没有Excel在这台机器上,所以不能比这更具体。


Hello Friends...

I m involved in project in which i need to code for reading excel file and save the data in another excel file. But Source excel file can contain any number of columns i.e Somtetimes it may contain 3 columns in file or sometimes it may contain 8 columns in file etc such as follows.

Col-A       Col-B      Col-C
1           3          5
2           4          6



The columns of Source Excel file are dynamic. But I know their datatypes. All are string. And these columns can contain any number of values. All columns have a fixed number of rows to each other. Suppose All columns may have a 2000 rows. I need to save columns in destination file as one below other with values such as follows.

Columns       Values
Col-A         1
Col-A         2
Col-B         3
Col-B         4
Col-C         5
Col-C         6



For that I need to count number of columns in Source File and i successed in counting the number of columns in source file. But I failed to create dynamic List Objects for them at runtime to store their values in respective List Objects. Please help m. I want C# code for it.

解决方案

First collect a List<string> of the column headings.
Then create an array or list of List<string> with an entry that corresponds to each column.
As you go through the file, as you get each value from a row, add it to the List<string> for the corresponding column.
When you're done reading, you can output each of the column's values as lines with the column name and a value for each value.


I would store the values in a Dictionary<string, List<string>>, and I would store only the Cell data for the number of row values per column that were actually used.

You could use the 'UsedRange function in Excel to determine (for the entire worksheet) what the last row used was. Note that 'UsedRange will return a Range that includes every Cell that at any point had a value, independent of whether that Cell has a value now.

And, with some programming you could (in theory ... I have not done this myself), calculate for each column what the first, and last, rows were in that column that had "real" data; see: [^].

However, that approach would probably mean you'd want to write something like this:

public Dictionary<string, List<dctColNameToColData>>; 

public class XLColumnData
{
    public string Name { set; get; }
    
    public int StartRow { set; get; }

    public int EndRow { set; get; }

    public List<string> ColumnData { set; get; }

    public XLColumnData(string name, int srow, int erow, List<string> cdata)
    {
       // initialize here ...
    }
}

I don't have Excel on this machine, so can't be more specific than this.


这篇关于使用C#在for循环中创建泛型列表的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆