c# - RemoveDuplicates function - how can I set multiple columns? -
i'm trying use removeduplicates
function using excel.interop
, i'm stuck how pass column array. know cannot pass simple int[]
array, gives exception @ runtime, , can pass single integer , works, want able select columns use @ runtime.
my current code in c# looks this:
using excel = microsoft.office.interop.excel; private void removeduplicates(excel.application excelapp, excel.range range, int[] columns) { range.removeduplicates(excelapp.evaluate(columns), excel.xlyesnoguess.xlno); }
and works fine if using 1 column, if columns
array has more 1 value, first 1 used.
in vba, equivalent function be:
sub removebadexample() dim colstouse colstouse = array(1, 2) selection.removeduplicates columns:=evaluate(colstouse), header:=xlyes end sub
which fails use both columns. however, if change this:
selection.removeduplicates columns:=(colstouse), header:=xlyes
it works fine. guess question equivalent in c#?
this test run worked me in unit test using 4.5 etc. did not throw exception @ rate.
application app = new application(); workbook wb = app.workbooks.open("c:\\data\\abc.xlsx", type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing); worksheet ws = wb.sheets[1]; range rng = ws.range["a1:c5", type.missing]; object cols = new object[]{1, 2}; rng.removeduplicates(cols, xlyesnoguess.xlyes);
be aware excel cell indices define ranges 1 based, not zero. if passing in bad range throw kind of exception.
object temp = range.cells[1][1].value;
Comments
Post a Comment