Populating a listbox from Excel using for loop...

BOB.G

New member
Joined
Mar 29, 2017
Messages
4
Programming Experience
1-3
Ok... so I have a listbox and I manage to bind the Excel worksheet to the list box (after hours of researching on the Internet).
Now I need to read the values from the sheet into the listbox, but unable to locate any suitable solution.
I would be grateful if an expert could help me accomplish this... Thanks in advance..

This is what I have done so far:-

private void LoadExcelSheet(string path, int sheet){

_Application excel = new Excel.Application();

Workbook wb;

Worksheet ws;

int row = 1;

int col = 1;

wb = excel.Workbooks.Open(path);
ws = wb.Worksheets[sheet];

for (row = 1; row < 10; row++){
for (col = 1; col < 6; col++){
listBox1.Items.Add(ws.Cells[row, col].Value2);
}
}
}
//------------------ end of LoadExcelSheet---------------------------

this only display 1 row with each item of data on top of each other eg:-
aaaaaaaa
bbbbbbbb
cccccccc
dddddddd

instead of: aaaaaaaa bbbbbbbb cccccccc dddddddd
 
Last edited:
Back
Top Bottom