multithreading - Read a huge Excel column from C# -
i've excel (could 2010 or 2013 don't know if can problem later) document 4 columns. first 3 columns store phone numbers string 10 or more characters. 4 column , forever store 1, 2, 3 or 4 , it's category. need check if each number in column a appears in column b , c or not think in read excel cells each column , store in list (isn't implemented yet because problem i'll explain below). make code:
private void btncargarexcel_click(object sender, eventargs e) { if (this.openfiledialog1.showdialog() == dialogresult.ok) { if (system.io.file.exists(openfiledialog1.filename)) { filepath.text = openfiledialog1.filename.tostring(); excel.application xlapp; excel.workbook xlworkbook; excel.worksheet xlworksheet; excel.range range; string str; int rcnt = 0; xlapp = new microsoft.office.interop.excel.application(); xlworkbook = xlapp.workbooks.open(openfiledialog1.filename, 0, true, 5, "", "", true, microsoft.office.interop.excel.xlplatform.xlwindows, "\t", false, false, 0, true, 1, 0); xlworksheet = (excel.worksheet)xlworkbook.worksheets.get_item(1); range = xlworksheet.usedrange; (rcnt = 1; rcnt <= range.rows.count; rcnt++) { str = (range.cells[rcnt, 1] excel.range).value2.tostring(); //bd.add(cleanstring(str)); bd.add(cleanstring(str, 10)); } (rcnt = 1; rcnt <= range.rows.count; rcnt++) { str = (range.cells[rcnt, 2] excel.range).value2.tostring(); //bd.add(cleanstring(str)); bl.add(cleanstring(str, 10)); } (rcnt = 1; rcnt <= range.rows.count; rcnt++) { str = (range.cells[rcnt, 3] excel.range).value2.tostring(); //bd.add(cleanstring(str)); cm.add(cleanstring(str, 10)); } nrosprocesados.text = bd.count().tostring(); listbox1.datasource = bd; noprocesadosbl.text = bl.count().tostring(); listbox2.datasource = bl; noprocesadoscm.text = cm.count().tostring(); listbox3.datasource = cm; xlworkbook.close(true, null, null); xlapp.quit(); releaseobject(xlworksheet); releaseobject(xlworkbook); releaseobject(xlapp); } else { messagebox.show("no se pudo abrir el fichero!"); system.runtime.interopservices.marshal.releasecomobject(appexcel); appexcel = null; system.windows.forms.application.exit(); } } } so iterate trough cells in column , store every number in list after make strings changes can see in code. problem here column has 797340 cells, column b has 91617 cells , column c has 95891 cells if run application, load excel , wait pc hang out (even having 12gb of ram , core i3 processor) , need open task manager , end task. best solution in order want (leave non repeated numbers) , not hang out pc? fine divide things in separately threads (i don't know because i'm starting in c# appreciated) each cycle? yours opinion topic?
edit: add new , clean method
so after read , read , members here improve code bit i've problem (commented below code). see code now:
// goes first when declare vars public static system.array objrowavalues; // goes in action when click button (i leave relevant part) excel.application xlapp; excel.workbook xlworkbook; excel.worksheet xlworksheet; excel.range range, rngarowlast; string str; int rcnt = 0; long lastacell, fullrow; xlapp = new microsoft.office.interop.excel.application(); xlworkbook = xlapp.workbooks.open(openfiledialog1.filename, 0, true, 5, "", "", true, microsoft.office.interop.excel.xlplatform.xlwindows, "\t", false, false, 0, true, 1, 0); xlworksheet = (excel.worksheet) xlworkbook.worksheets.get_item(1); range = xlworksheet.usedrange; fullrow = xlworksheet.rows.count; lastacell = xlworksheet.cells[fullrow, 1].end(excel.xldirection.xlup).row; rngarowlast = xlworksheet.get_range("a1", "a" + lastacell); objrowavalues = (system.array) rngarowlast.cells.value; now because i'll fill listbox values objrowavalues , listbox accepts list datasource need convert objrowavalues list of string. try this itsn't working me. help?
unfortunately, more of vb.net guy - have converted code you. hope works out of box - not use kind of tool here, had no way test it.
public void test() { object[,] rawdata = null; dynamic range = xlworksheet.usedrange; //i unsure here correct order - not work excel @ work, might have change following lange, if columns needs before rows or rawdata = range.value2; //i using list here, because lists lot easier work simple arrays list<list<string>> realdata = new list<list<string>>(); //start @ 1 because excel-delivered array not have values @ index 0 - 1-based array ever encounter in .net (x = 1; x <= information.ubound(rawdata, 1); x++) { list<string> templist = new list<string>(); (y = 1; y <= information.ubound(rawdata, 2); y++) { templist.add(rawdata[x, y].tostring()); } realdata.add(templist); } //you should finished here... }
Comments
Post a Comment