Excel VB.Net - 将数组转换为范围并将值设置为工作表
Excel VB.Net - Convert array to range and set value to worksheet
几个小时以来,我一直在尝试为这个错误寻找解决方案,我找到的所有解决方案都没有修复我在设置 Range.Value2 = Value 时遇到的错误
在尝试将数组填充到 excel 电子表格时。
目前数组的长度只有 1,我已经用更大的数组尝试过,到目前为止它只是在最后一行代码中抛出这个异常:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
代码:
Imports ExcelDna.Integration
Imports Excel = Microsoft.Office.Interop.Excel
Public Function TestFunction()
'Dim data = New Object(1, 2) {{1, 2, 3}, {2, 3, 4}}
Dim app As Excel.Application = CType(ExcelDnaUtil.Application, Excel.Application)
Dim worksheet As Excel.Worksheet = CType(app.ActiveWorkbook.ActiveSheet, Excel.Worksheet)
Dim rng As Excel.Range = CType(worksheet.Cells(2, 1), Excel.Range)
Dim arr() As String = New String() {"hello world"}
'Tried this rng.Value2 = arr as well
rng.Resize(1, arr.Length).Value2 = arr
End Function
我的目标是能够将矩阵复制到 excel 电子表格,但到目前为止我
只需要帮助修复代码抛出数组的错误。
感谢您以后的回复!
Range.Value2
是二维的array/matrix,你也得用这样的数组
Dim arrLine1() As String = {"Hello World!", "red", "green"}
' convert to 2-dimensional array
Dim arr(,) As String
Redim arr(1, arrLine1.Length)
For i = 0 To arrline1.Length - 1
arr(0, i) = arrLine1(i)
Next
rng.Resize(1, arr.GetUpperBound(1)).Value2 = arr
几个小时以来,我一直在尝试为这个错误寻找解决方案,我找到的所有解决方案都没有修复我在设置 Range.Value2 = Value 时遇到的错误 在尝试将数组填充到 excel 电子表格时。 目前数组的长度只有 1,我已经用更大的数组尝试过,到目前为止它只是在最后一行代码中抛出这个异常:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
代码:
Imports ExcelDna.Integration
Imports Excel = Microsoft.Office.Interop.Excel
Public Function TestFunction()
'Dim data = New Object(1, 2) {{1, 2, 3}, {2, 3, 4}}
Dim app As Excel.Application = CType(ExcelDnaUtil.Application, Excel.Application)
Dim worksheet As Excel.Worksheet = CType(app.ActiveWorkbook.ActiveSheet, Excel.Worksheet)
Dim rng As Excel.Range = CType(worksheet.Cells(2, 1), Excel.Range)
Dim arr() As String = New String() {"hello world"}
'Tried this rng.Value2 = arr as well
rng.Resize(1, arr.Length).Value2 = arr
End Function
我的目标是能够将矩阵复制到 excel 电子表格,但到目前为止我 只需要帮助修复代码抛出数组的错误。 感谢您以后的回复!
Range.Value2
是二维的array/matrix,你也得用这样的数组
Dim arrLine1() As String = {"Hello World!", "red", "green"}
' convert to 2-dimensional array
Dim arr(,) As String
Redim arr(1, arrLine1.Length)
For i = 0 To arrline1.Length - 1
arr(0, i) = arrLine1(i)
Next
rng.Resize(1, arr.GetUpperBound(1)).Value2 = arr