简体   繁体   English

c# interop excel:将一维数组写入excel范围

[英]c# interop excel: Write 1D array to excel range

I want to copy an 1D array to a column range in excel.我想将一维数组复制到 excel 中的列范围。 I'm using interop for this purpose.为此,我正在使用interop

I have already tried these things:我已经尝试过这些事情:

  • range.get_Resize(Ary.Length, 1).Value2 = Ary;
  • range.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, Ary);
  • and as simple as range.Value = Ary;range.Value = Ary;一样简单range.Value = Ary;

I have tried using even range.value2 , but these things copy the very first index value in the array to the entire range.我什range.value2尝试使用range.value2 ,但这些东西将数组中的第一个索引值复制到整个范围。

So say suppose, if there are 200 rows in the range and the array contains integers 101-300, than only 101 is copied throughout the range with the above tried methods.因此假设,如果范围中有 200 行并且数组包含整数 101-300,那么使用上述尝试过的方法在整个范围内仅复制 101。

Can anyone please help me with this?任何人都可以帮我解决这个问题吗? It would be more helpful if someone can explain me this strange behavior!如果有人可以向我解释这种奇怪的行为会更有帮助! Thanks in advance.提前致谢。

Note: I know I can do it through a for loop , but that takes time.注意:我知道我可以通过for 循环来完成,但这需要时间。 I would surely like to know a method which takes less than a minute to iterate a million rows.我肯定想知道一种方法,它需要不到一分钟的时间来迭代一百万行。

I seriously don't know what exactly is wrong with the above methods.我真的不知道上述方法到底有什么问题。 But I found the solution:但我找到了解决方案:

Excel.Range range = sheetSource.UsedRange.Columns[columnIndx];
range.Value = application.WorksheetFunction.Transpose(Ary);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM