Click here to Skip to main content
15,504,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I wrap and align text in an excel work sheet using
Updated 13-Jul-10 4:37am

Here is one i made earlier, for a similiar question asked in the VB forums only a few weeks ago;

Private Sub doSomething()

    Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
    Dim xlWk As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add()
    Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = xlWk.Worksheets(1)
    xlApp.Visible = True

    xlSheet.Range("A1").Value = "Some Text Value on 1 line"

    xlSheet.Range("A5").Value = "Some text value on" + vbCrLf + "2 lines"


    xlSheet.Range("B10").Value = "Some text aligned to the center"
    xlSheet.Range("B10").ColumnWidth = 120
    xlSheet.Range("B10").CurrentRegion.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter

End Sub

NOTE: there are other HorizontalAlignment constants available in the object model,e.g. HorizontalAlignment.Left (.Right) (.Center) but these do not work, you must use the one shown above.
Share this answer
xlBottom = -4107
xlCenter = -4108
xlJustify = -4130
xlLeft = -4131
xlRight = -4152
xlTop = -4160
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900