简体   繁体   English

如何将数组复制到 C# 中的 Excel 范围?

[英]How to copy an Array to an Excel Range in C#?

How do I copy an Array to an Excel Range in C#?如何将数组复制到 C# 中的 Excel 范围?
Why does the below code not work?为什么下面的代码不起作用?
If I assign it like this, then in each cell is the same value如果我这样分配它,那么在每个单元格中都是相同的值

Workbook workbook;
Worksheet worksheet;
List<double> flow = new List<double>();

workbook = excel.Workbooks.Open(filename);
worksheet = workbook.Worksheets.Add();

worksheet.Range[$"$A$1:$A{flow.count}"].Value = flow.ToArray();

Of course the list is filled with values.当然,列表中充满了值。 I have omitted this part here.我在这里省略了这部分。

replace your last line with用你的最后一行替换

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

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

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