Hi,
exworksheet.Cells.CreateRange(1, 1, exworksheet.Cells.MaxDataRow, exworksheet.Cells.MaxDataColumn + 2).SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.Black)
Why is 2 added to maxdaatcolumn
Well, Cells.MaxDataRow and Cells.MaxDataColumn return the maximum row / column index which has data in it. For ...."Why is 2 added to maxdaatcolumn"... Actually, your template file contains the maximum data column "S" but the the cells (which contains data) are merged... i.e. Q,R,S are merged to become Q column (Q2:Q34), so we have to add 2 to MaxDataColumn to get the complete data range of cells for setting borders around them.
Also I want border around each cell containing the data.
Please try the code and it works fine:
Dim exworkbook As Workbook
Dim strFileName As String
Dim exworksheet As Worksheet
Dim strReportName As String
exworkbook = New Workbook()
strFileName = "D:\test\location.xls"
exworkbook.Open(strFileName)
exworksheet = exworkbook.Worksheets(0)
exworksheet.Cells.Merge(1, 1, 1, 8)
exworkbook.ChangePalette(Color.Gold, 55)
exworkbook.ChangePalette(Color.FromArgb(200, 200, 200), 54)
exworksheet.Cells("A1").Style.Font.Color = Color.Red
exworksheet.Cells("A1").Style.BackgroundColor = Color.Gold
exworkbook.Styles.Add()
Dim sty As Style = exworkbook.Styles(0)
sty.Font.Underline = FontUnderlineType.Single
sty.Borders.SetStyle(CellBorderType.Medium)
sty.Font.Color = Color.FromArgb(0, 0, 0)
sty.ForegroundColor = Color.FromArgb(200, 200, 200)
sty.Pattern = BackgroundType.Solid
Dim flag As StyleFlag = New StyleFlag()
flag.FontUnderline = True
flag.FontColor = True
flag.CellShading = True
Dim row As Row = exworksheet.Cells.Rows(0)
row.ApplyStyle(sty, flag)
exworksheet.Cells.SetRowHeight(0, 50)
exworksheet.AutoFitColumns()
Dim index As Integer = exworkbook.Styles.Add()
Dim stl As Style = exworkbook.Styles(index)
stl.Borders(BorderType.TopBorder).LineStyle = CellBorderType.Medium
stl.Borders(BorderType.TopBorder).Color = Color.Black
stl.Borders(BorderType.BottomBorder).LineStyle = CellBorderType.Medium
stl.Borders(BorderType.BottomBorder).Color = Color.Black
stl.Borders(BorderType.LeftBorder).LineStyle = CellBorderType.Medium
stl.Borders(BorderType.LeftBorder).Color = Color.Black
stl.Borders(BorderType.RightBorder).LineStyle = CellBorderType.Medium
stl.Borders(BorderType.RightBorder).Color = Color.Black
Dim range1 As Aspose.Cells.Range = exworksheet.Cells.CreateRange(1, 1, exworksheet.Cells.MaxDataRow, exworksheet.Cells.MaxDataColumn + 2)
range1.Style = stl
range1.Name = "MyRange"
strFileName = "d:\test\locationabc.xls"
exworkbook.Save(strFileName)
Thank you.