将Excel单元格与中心VB对齐-未声明xlCenter [英] Align Excel cell to center VB - xlCenter is not declared

查看:202
本文介绍了将Excel单元格与中心VB对齐-未声明xlCenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2013 Visual Basic,MS ACCESS 2013,EXCEL 2013

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

我的程序将另存为我的datagrid中的数据到excel.我将Access 2013用作数据库 这是我的代码:

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

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'未声明.由于其保护级别,它可能无法访问

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

推荐答案

xlCenterMicrosoft.Office.Interop.Excel.Constants的成员.

由于已将Microsoft.Office.Interop.Excel分配给名称Excel,因此可以像这样引用该常量...

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

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

这篇关于将Excel单元格与中心VB对齐-未声明xlCenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆