如何在 Shape.ShapeStyle 中使用 msoShapeStylePresetXX

How to use msoShapeStylePresetXX in Shape.ShapeStyle

如何在 Shape.ShapeStyle 中访问 msoShapeStylePresetXX?

我正在使用 Visual studio 2012 Visual Basic 和 Excel(2013 或更低版本)

我想在我的形状上使用 msoShapeStylePresetXX 作为形状样式

这是我的代码:

Imports System.Data.OleDb
Imports Excel = Microsoft.Office.Interop.Excel

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'AccessdbtestDataSet.country' table. You can move, or remove it, as needed.
        Me.CountryTableAdapter.Fill(Me.AccessdbtestDataSet.country)
        Dim con As New OleDbConnection
        Dim query As String = "SELECT * FROM  country"
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=accessdbtest.accdb"
        con.Open()
        Dim dt As New DataTable
        Dim ds As New DataSet

        ds.Tables.Add(dt)
        Dim da As New OleDbDataAdapter(query, con)
        da.Fill(dt)
        DataGridView1.DataSource = ds.Tables(0)
        con.Close()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer
        Dim xlCenter As String = "center"

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")



        xlWorkSheet.Range("A1:D1").MergeCells = True
        xlWorkSheet.Range("A2:D2").MergeCells = True
        xlWorkSheet.Range("A3:D3").MergeCells = True
        xlWorkSheet.Cells(1, 1) = "Republic of the Philippines"
        xlWorkSheet.Cells(2, 1) = "NCR"
        xlWorkSheet.Cells(3, 1) = "Manila"
        xlWorkSheet.Range("A1:A1").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
        xlWorkSheet.Range("A2:A2").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
        xlWorkSheet.Range("A3:A3").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter
        For i = 5 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                xlWorkSheet.Cells(i + 1, j + 1) = _
                    DataGridView1(j, i - 3).Value.ToString()
            Next
        Next


        xlApp.Range("A4").Select()
        xlApp.ActiveWindow.FreezePanes = True

        Dim shp As Excel.Shape
        Dim clLeft As Double
        Dim clTop As Double
        Dim clWidth As Double
        Dim clHeight As Double

        Dim cl As Excel.Range
        ''Dim shpOval As Excel.Shape
        xlApp.Range("G5").Select()

        cl = xlApp.Range(xlApp.Selection.Address)  '<-- Range("C2")

        clLeft = cl.Left
        clTop = cl.Top
        clHeight = cl.Height
        clWidth = cl.Width

        shp = xlApp.ActiveSheet.Shapes.AddShape(1, clLeft, clTop, 100, 100)

        //This line of code is my problem <----------------------------<|
        shp.ShapeStyle = msoShapeStylePresetXX


        Debug.Print(shp.Left = clLeft)
        Debug.Print(shp.Top = clTop)

        xlWorkSheet.SaveAs("C:\excel\vbexcel.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("You can find the file C:\vbexcel.xlsx")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class

您只需在项目中添加对“office.dll”的引用(就像您对 Excel 和 Microsoft.Office.Interop.Excel 所做的一样).

这样,VB.net 应该会显示 msoXXX 常量: