简体   繁体   中英

c# get value list from excel file

I'm working on a dll for Excel file. In the dll I want to do a method in which the client will enter the row to get the list from, and then enter the column from and to get list. something like: public List GetValueList(int inRow, string fromColumn, string toColumn)

The problem is- how can I get do it? in Excel there are columns like "AZX", "AA" and so on... I can't just do "fromColumn++".

Any ideas? I hope I explained myself.

The Cells member of the WorkSheet object takes both row and column numbers as integers. So you can do something like this:

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;
}

Note that both fromColumn and toColumn are integers. If you need to convert from an alphabetic column number (like BD or AFH), simply use WS.Range("BD" + "1").Column , replacing "BD" with the actual column number you have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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