Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Cell Alignments: Numerical values for e.g. xlLeft, xlRight or xlCenter?

I've been trying to align Excel cell text values. I've tried the more common xlLeft, xlRight but this doesn't seem to work. The error was xlLeft wasn't declared. I am using Visual Studios and creating a aspx page with VB.

Here is a sample of my code:

    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    'Start a new workbook in Excel
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Add

    'Add data to cells of the first worksheet in the new workbook
    oSheet = oBook.Worksheets(1)

    oSheet.Range("A1:E1").Merge()
    oSheet.Range("A1").Value = "Hello"
    oSheet.Range("A2:E2").Merge()
    oSheet.Range("A2").Value = "There "
    oSheet.Range("A1:A4").Font.Bold = True
    oSheet.Range("A1").HorizontalAlignment = -4131
like image 397
Derek Avatar asked Aug 05 '11 06:08

Derek


3 Answers

xcl.Range("J:J").EntireColumn.HorizontalAlignment = _
Microsoft.Office.Interop.Excel.Constants.xlCenter
like image 81
Jalapeno Bob Avatar answered Oct 14 '22 19:10

Jalapeno Bob


For VerticalAlignment:

Top:    -4160
Center: -4108
Bottom: -4107

And HorizontalAlignment:

Left:    -4131
Center:  -4108
Right:   -4152
like image 38
Mike Woodhouse Avatar answered Oct 14 '22 20:10

Mike Woodhouse


You can use xlLeft:

Imports Microsoft.Office.Interop.Excel

...
oSheet.Range("A1").HorizontalAlignment = Constants.xlLeft
like image 33
Hamster_NL Avatar answered Oct 14 '22 19:10

Hamster_NL