【问题标题】:How to put array into excel range如何将数组放入excel范围
【发布时间】:2012-03-12 06:56:51
【问题描述】:

我知道如何将单个单元格写入 excel,但是当我在数组 excel 表上尝试它时,只填充了最后一个值

这是我的范围

 Excel.Range ServiceName = (Excel.Range)_sheet.get_Range(_sheet.Cells[38, "B"] as Excel.Range, _sheet.Cells[45, "B"] as Excel.Range);

_ServiceName 是包含 1,2,3,4,5,6 的列表

for (int i = 0; i < _ServiceName.Count; i++)
            {
                ServiceNameArray[0, i] = _ServiceName[i];

            }

这是我尝试写入 excel,但正如我所说,excel 书中只有最后一项 (6)

 for (int i = 0; i < _ServiceName.Count; i++)
            {
                ServiceName.set_Value(Type.Missing, ServiceNameArray[0,i]);
            }

有人有想法吗?

【问题讨论】:

    标签: c# wpf excel interop


    【解决方案1】:

    大卫皮拉斯是对的。你还在那里做一些其他奇怪的事情,我可以根据要求详细说明。

    现在我只想指出,您可以直接将 范围的.Value 属性分配给数组

    ServiceName.Value2 = _ServiceName.toArray();
    

    对于更大量的数据,这要快得多。

    (旁注:如果你想对公式做同样的事情,出于某种奇怪的原因,你必须采取额外的步骤(加倍时间):

    range.Formula = array;
    range.Formula = range.Formula;
    

    除非有更好的方法我不知道yet。)

    【讨论】:

      【解决方案2】:

      我看到您在 ServiceName 数组上循环以一个接一个地获取所有值,但没有看到您在每次循环迭代时更改单元格范围内的焦点单元格。当然,我会说,你只看到最后一个值,因为你总是在同一个地方一个接一个地写所有值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多