Click here to Skip to main content
15,896,557 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 vb.net?
Posted
Updated 13-Jul-10 3:37am
v2

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

VB
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.Columns().AutoFit()
    xlSheet.Rows().AutoFit()

    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
 
v3
VB
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