vba 将 Excel 单元格与 VB 中心对齐 - 未声明 xlCenter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28915252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:26:40  来源:igfitidea点击:

Align Excel cell to center VB - xlCenter is not declared

vb.netexcelexcel-vbams-access-2013excel-2013vba

提问by zerenin

Im using Visual Studio 2013 Visual Basic, MS ACCESS 2013, EXCEL 2013

我使用的是 Visual Studio 2013 Visual Basic、MS ACCESS 2013、EXCEL 2013

My program Save As the data in my datagrid to excel. I use access 2013 as my database Here is my code:

我的程序将我的数据网格中的数据另存为 Excel。我使用 access 2013 作为我的数据库 这是我的代码:

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)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        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

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



        xlWorkSheet.Range("A1:D1").MergeCells = True
        xlWorkSheet.Cells(1, 1) = "Republic of the Philippines"

    //I got a problem in this line of code. The program gives a message that xlCenter is not declared
        xlWorkSheet.Range("H15:H16").VerticalAlignment = xlCenter

        For i = 1 To DataGridView1.RowCount - 2
            For j = 0 To DataGridView1.ColumnCount - 1
                xlWorkSheet.Cells(i + 1, j + 1) = _
                    DataGridView1(j, i).Value.ToString()
            Next
        Next

        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

My problem is The program gives the message:

我的问题是程序给出了消息:

'xlCenter' is not declared. It may be inaccessible due to its protection level

未声明“xlCenter”。由于其保护级别,它可能无法访问

回答by HansUp

xlCenteris a member of Microsoft.Office.Interop.Excel.Constants.

xlCenter是 的成员Microsoft.Office.Interop.Excel.Constants

Since you assigned Microsoft.Office.Interop.Excelto the name Excel, you can reference that constant like this ...

由于您分配Microsoft.Office.Interop.Excel了 name Excel,您可以像这样引用该常量......

xlWorkSheet.Range("H15:H16").VerticalAlignment = Excel.Constants.xlCenter

回答by Steven Martin

You will need to declare it yourself as its not included in that import( its part of System.Windows)

您需要自己声明它,因为它不包含在导入中(它是 System.Windows 的一部分)

Const xlCenter = -4108