c# - How to get data from every single cell? -
i'm doing app friend works in hospital.
what app doing grab data excel, sort , display it. stuck on grabbing data. excel file has 3 columns , n rows. cells of first column not filled in.
i have tried different ways of taking data, , wasn't able make working right. trying include empty cells search, temp returns me empty strings.
how iterate each row until end, , on every iteration grab data each column, if empty - return ""?
excel.application excelapp = new microsoft.office.interop.excel.application(); excel.workbook excelworkbook = excelapp.workbooks.open(currentexcellocation, 0, true, 5, "", "", false, excel.xlplatform.xlwindows, "", false, true, 0, false, false, false); excel.sheets excelsheets = excelworkbook.worksheets; excel.worksheet excelworksheet = (excel.worksheet)excelsheets.get_item(3); int itotalrows = excelworksheet.usedrange.rows.count; (int = 1; <= itotalrows; ++i) { string templ = (excelworksheet.cells[i, 1] excel.range).specialcells(excel.xlcelltype.xlcelltypeblanks).text.tostring(); if (templ != "") { temp = templ; } string temps = (excelworksheet.cells[i, 2] excel.range).value2.tostring(); string tempd = (excelworksheet.cells[i, 3] excel.range).value2.tostring(); datascheme.symptoms tempsd = new datascheme.symptoms(temps, tempd); if (data.findindex(a => a.location == temp) < 0) { data.add(new datascheme(temp)); } data.find(b => b.location == temp).symplist.add(tempsd); textbox6.text += temp + environment.newline; } excelworkbook.close(0); excelapp.quit(); marshal.finalreleasecomobject(excelapp); marshal.finalreleasecomobject(excelworkbook); marshal.finalreleasecomobject(excelsheets); marshal.finalreleasecomobject(excelworksheet);
get closedxml nuget, follow method return excel data datatable way can data each row.
public static datatable importsheet(string filename) { var datatable = new datatable(); var workbook = new xlworkbook(filename); var xlworksheet = workbook.worksheet(1); var range = xlworksheet.range(xlworksheet.firstcellused(), xlworksheet.lastcellused()); var col = range.columncount(); var row = range.rowcount(); datatable.clear(); (var = 1; <= col; i++) { var column = xlworksheet.cell(1, i); datatable.columns.add(column.value.tostring()); } var firstheadrow = 0; foreach (var item in range.rows()) { if (firstheadrow != 0) { var array = new object[col]; (var y = 1; y <= col; y++) { array[y - 1] = item.cell(y).value; } datatable.rows.add(array); } firstheadrow++; } return datatable; }
Comments
Post a Comment