如何创建Excel形状(折线)?

时间:2018-07-23 09:53:20

标签: excel vb.net

我想从VB.NET在Excel中绘制多边形。每当我使用package main /* #cgo CFLAGS: -I./cgolang/include #cgo LDFLAGS: -L./cgolang/lib -laxxxxxx #include "axxxxxx.h" */ import "C" import "fmt" func main() { fmt.Printf("hello, world\n") } 方法时,都会出现以下错误:

  

“指定的参数的数据类型不正确。”

我的代码是:

AddPolyline

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

VBA Excel对象库(Interop.Excel)没有开箱即用的Point类/结构。您需要使用单打的二维数组,而不是点的数组。这应该起作用。

Dim arr As Single(,) = {{25, 100}, {100, 50}, {150, 50}, {25, 100}}
Dim rc As Excel.Shape = objSheet.Shapes.AddPolyline(arr)

如果已经有一个点的数组/列表(从某处接收),并且想要使用它,则可以编写一个辅助函数以将其转换为2D数组。看起来像这样:

Private Function PointsTo2DArray(points As IList(Of Point)) As Single(,)
    Dim arr(points.Count - 1, 1) As Single
    For i = 0 To points.Count - 1
        arr(i, 0) = points(i).X
        arr(i, 1) = points(i).Y
    Next

    Return arr
End Function

然后,您可以轻松地执行以下操作:

Dim X1 As New Point(25, 100)
Dim X2 As New Point(100, 50)
Dim X3 As New Point(150, 50)
Dim X4 As New Point(25, 100)

' Assuming you already have this array.
' If you Then don't, don't create one and use a 2D array directly as shown above.
Dim arrPoint() As Point = {X1, X2, X3, X4}

Dim rc As Excel.Shape = xlSheet.Shapes.AddPolyline(PointsTo2DArray(arrPoint))

两种情况下的结果:

Polyline

参考:

Shapes.AddPolyline Method