2

我正在转换一些使用 Excel 的 vb.net 代码来使用 MS 提供的 Excel 互操作(版本 14)。我已经成功地隐藏了很多代码,但我还有最后一个问题,我无法设置 Excel 表单标签标题文本。我使用的是 Shapes 而不是 Pictures,我尝试对 Shapes.AddLabel 做同样的事情,但我做不到。

这是我使用 COM 对象的旧代码

With ExcelObject
    .ActiveSheet.OLEObjects.Add(ClassType:="Forms.Label.1", Link:=False,
    DisplayAsIcon:=False, Left:=iTempLeft, Top:=iTempTop,
    Width:=iTempWidth, Height:=iTempHeight).Select() 
    iLabels = iLabels + 1
    sLabelName = "Label" & CStr(iLabels)
    .ActiveSheet.OLEObjects(sLabelName).object.Caption = gudtDrawingAndDefectXYsForPrint(iDrawing).sDrawingName
    .ActiveSheet.OLEObjects(sLabelName).Placement = Microsoft.Office.Interop.Excel.XlPlacement.xlMoveAndSize 
End With

现在这是我尝试使用互操作的更新代码

With ExcelObject
     CType(.ActiveSheet, Excel.Worksheet).Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, iTempLeft, iTempTop, iTempWidth, iTempHeight).Name = sLabelName
     CType(.ActiveSheet, Excel.Worksheet).Shapes.Item(sLabelName).Placement = Excel.XlPlacement.xlMoveAndSize
     CType(.ActiveSheet, Excel.Worksheet).Shapes.Item(sLabelName).Title = gudtDrawingAndDefectXYsForPrint(iDrawing).sDrawingName
End Width

标题不会像使用 COM 对象的“标题”那样在屏幕上显示任何内容。

4

1 回答 1

2

这对我有用。(在 VS 2010 Ultimate + Office 2010 中测试

ACTIVEX 控件

    Dim xlApp As New Excel.Application
    Dim xlWb As Excel.Workbook
    Dim xlsheet As Excel.Worksheet
    Dim oleObj As Excel.OLEObject

    With xlApp
        .Visible = True

        xlWb = .Workbooks.Add

        xlsheet = xlWb.Sheets("Sheet1")

        With xlsheet
            oleObj = .OLEObjects.Add(ClassType:="Forms.Label.1", Link:=False,
                    DisplayAsIcon:=False, Left:=200, Top:=100,
                    Width:=100, Height:=35)

            oleObj.Object.caption = "Sid"
        End With
    End With

表单控件

    Dim xlApp As New Excel.Application
    Dim xlWb As Excel.Workbook
    Dim xlsheet As Excel.Worksheet
    Dim Shp As Excel.Shape

    With xlApp
        .Visible = True

        xlWb = .Workbooks.Add
        xlsheet = xlWb.Sheets("Sheet1")

        With xlsheet
            Shp = .Shapes.AddLabel(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 513, 128, 90, 27)

            Shp.TextFrame2.TextRange.Text = "Sid"

        End With
    End With
于 2013-10-17T13:35:43.467 回答