C# 从 Excel 文件中获取值列表

本文关键字:获取 列表 文件 Excel | 更新日期: 2023-09-27 18:32:27

我正在为Excel文件处理dll。在dll中,我想做一个方法,其中客户端将输入要从中获取列表的行,然后输入列from和get列表。像这样:public List GetValueList(int inRow, string fromColumn, string toColumn)

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

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

C# 从 Excel 文件中获取值列表

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"替换为您拥有的实际列号。