【问题标题】:c# get value list from excel filec#从excel文件中获取值列表
【发布时间】:2021-07-01 11:39:09
【问题描述】:

我正在为 Excel 文件制作一个 dll。 在dll中我想做一个方法,客户端将输入行以获取列表,然后输入列从并获取列表。 就像是: public List GetValueList(int inRow, string fromColumn, string toColumn)

问题是——我怎样才能做到这一点?在 Excel 中有“AZX”、“AA”等列...我不能只做“fromColumn++”。

有什么想法吗?我希望我自己解释一下。

【问题讨论】:

    标签: c# string excel


    【解决方案1】:

    Worksheet 对象的Cells 成员将行号和列号都作为整数。所以你可以这样做:

    List<object> GetValueList(Worksheet WS, int inRow, int fromColumn, int toColumn)
    {
        List<object> MyList = new List<object>(toColumn - fromColumn + 1);
        for(int i=fromColumn; i<=toColumn; i++)
            MyList.Add(WS.Cells[inRow, i].Value);
    
        return MyList;
    }
    

    注意 fromColumn 和 toColumn 都是整数。如果您需要从字母列号(如 BD 或 AFH)转换,只需使用 WS.Range("BD" + "1").Column,将“BD”替换为您拥有的实际列号。

    【讨论】:

    • 我认为“WorkSheet”是一个错字。它应该是“工作表”
    • @JayBrown:是的,已修复。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多