简体   繁体   English

从Excel C#中的数据行中查找最小列数

[英]Finding minimum column count from data rows in Excel C#

In an Excel document suppose there are data as shown below: 在一个Excel文档中,假设有如下所示的数据:

1 a 1个
2 bc 公元前2年
3 def 3 def
4 ghijk 4 ghijk
5 lmn 5百万

Considering the very first two rows being header, data rows are from the third line. 考虑到头两行是标题,数据行来自第三行。 How could the minimum column count can be found from this data given that is 3, because for all rows to have common data into the all columns. 在给定为3的情况下,如何从此数据中找到最小列数,因为对于所有行来说,所有列都具有公共数据。

What I have is two classes as shown below 我有两个类,如下所示

 public class RowEntity
 {
     public List<String> ColumnValues { get; set; }
 }
 public class FileEntity
 {
     public List<RowEntity> RowValues { get; set; }
 } 

Data rows can be accessed may be like below: 可以访问的数据行可能如下所示:

List <RowEntity> dataRows = fileObj.RowValues.GetRange(int index, int count);

I need to have something like: 我需要有类似的东西:

    private int GetMinimumColumnCount(List <RowEntity> dataRows)
    {
        int minColCount;
        FileEntity fileObj = new FileEntity(); // to access the rows from the Excel file
        int headerLines; // can be changed but here set in given example

        // calculate the minColumnCount for the 

        return minColCount; 
    }

Any idea how? 任何想法如何?

Got it sorted eventually 最终得到了排序

The reason for using nullable int type is that the very first value can be null but having it 0 initially will never do the check right. 使用可为空的int类型的原因是,第一个值可以为null,但最初将其设置为0将永远不会执行检查权限。

    private int? GetMinimumColumnCount(List <RowEntity> dataRows)
    {
        int ? minColumnCount = null;

        for (int i = 0; i < dataRows.Count; i++)
        {
            if (minColumnCount == null || minColumnCount > dataRows[i].ColumnValues.Count) 
            {
                minColumnCount = dataRows[i].ColumnValues.Count; 
            }     
        }
        return minColumnCount; 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM