繁体   English   中英

如何在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 ”的引用即可(就像您使用Microsoft.Office.Interop.Excel为Excel所做的那样)。

这样,VB.net应该向您显示msoXXX常数:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM