Free Support Forum - aspose.com

Trouble looping through rows in a worksheet in order to apply style to each row

Smile [:)]I would like to loop through the rows in my worksheet and apply a style to all of the cells. The style will set the following properties

verticalalignment = top, horizontalalignment = left

Here is the code I am using to setup the style

Dim excel As Excel = New Excel()

excel.Styles.Add()

'Accessing the newly added Style to the Excel object

Dim eStyle As Aspose.Cells.Style = excel.Styles(0)

'Setting the vertical alignment of the text

eStyle.VerticalAlignment = TextAlignmentType.Top

'Setting the horizontal alignment of the text

eStyle.HorizontalAlignment = TextAlignmentType.Left

Here is the code I am trying to use without success to loop through the rows in the worksheet

Dim j As Integer

Dim contentRow As Aspose.Cells.Row = Nothing

For j = 0 To sheet.Cells.Rows.Count

contentRow = sheet.Cells.Rows.Item(j)

contentRow.Style = eStyle

Next

Thanks for your help,

Tyler

Hi Tyler,

To format a whole worksheet, please try latest Aspose.Cells v4.1 with following code:

Dim workbook As Workbook = New Workbook()
Dim style As Aspose.Cells.Style = workbook.Styles(workbook.Styles.Add())
style.VerticalAlignment = TextAlignmentType.Top
style.HorizontalAlignment = TextAlignmentType.Left

Dim flag As StyleFlag = New StyleFlag()
flag.HorizontalAlignment = True
flag.VerticalAlignment = True

Dim cells As Cells = workbook.Worksheets(0).Cells
Dim i As Integer
For i = 0 To 255
cells.Columns(i).ApplyStyle(style, flag)
Next

That works great. Many thanks for your help.

Tyler