c# - Efficently insert rows + data -
i have excel has tens of thousands of rows. need insert data randomly within excel. below have function using main concern shifting , inserting data. need better idea or method can same mine faster.
a answer show method , comment explain how works. can reused if else needs it.
here mine:
private void shiftrows(int from, int numberof) { from++; range r = oxl.get_range("a" + from.tostring(), "a" + from.tostring()).entirerow; (int = 0; < numberof; i++) r.insert(microsoft.office.interop.excel.xlinsertshiftdirection.xlshiftdown); } public void inputrowdata(string[] data, int rds) { int bestrow = getrowbyrds_a(rds); string[] formateddata = formatoutput(bestrow, data); string val = getvalueofcell(bestrow, 6); if (val != null) { shiftrows(bestrow, data.length); bestrow++; } else shiftrows(bestrow, data.length - 1); // transform formated data string[,] string[][] spliteddata = formateddata.select(s => s.split('\t')).toarray(); var colcount = spliteddata.max(r => r.length); var exceldata = new string[spliteddata.length, colcount]; (int = 0; < spliteddata.length; i++) { (int j = 0; j < spliteddata[i].length; j++) { exceldata[i, j] = spliteddata[i][j]; } } osheet.get_range("a" + bestrow.tostring()).resize[spliteddata.length, colcount].value = exceldata; mainwindow.mainwindowdispacter.begininvoke(new system.action(() => mainwindow.mainwindow.debugtextbox.appendtext("done " + rds + " input!" + environment.newline))); }
my best guess for
loop r.insert
makes slow because numberof
large. causes excel shift whole spreadsheet numberof
times inserting 1 row @ time.
it's lot faster if insert required number of rows @ once:
private void shiftrows(int from, int numberof) { // excel starts @ 1 = from+1; // insert numberof rows @ row oxl.rows(string.format("{0}:{1}", from, from+numberof)) .insert(microsoft.office.interop.excel.xlinsertshiftdirection.xlshiftdown); }
additional questions can help:
how fast , how fast want it? in situation become slow?
Comments
Post a Comment