Format Excel Page in VB
Format Excel Page in VB
Format Excel Page in VB
NET
When programming with Excel in VB.Net, you can
interact with the objects provided by the Excel
object model. You can programmatically create a
worksheet, read worksheet, formatting cell etc. from your VB.Net
application. In this article we will take a closer look at manipulating Excel
cells from your VB.Net application.
The following source code using Microsoft Excel 12.0 Object Library.In
the previous section we saw how to import Microsoft Excel 12.0 Object
Library in the VB.Net project. In order to access the object model from
Visual VB.Net, you have to add the Microsoft Excel 12.0 Object Library to
you project. In the previous chapter you can see a step by step instruction
on how to add Excel library to your project.
How to add Excel Library
xlWorkSheet.Cells(1, 1) = "1234567890"
The following VB.Net program create a mark list in Excel file and format the
grid cells. First we merge these cell and create a custom header , then the
students name and totals format as BOLD . And finally create a border for
the whole mark list part.
Output:
xlWorkBook As Excel.Workbook
xlWorkSheet As Excel.Worksheet
misValue As Object = System.Reflection.Missing.Value
chartRange As Excel.Range
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
'add data
xlWorkSheet.Cells(4,
xlWorkSheet.Cells(4,
xlWorkSheet.Cells(4,
xlWorkSheet.Cells(4,
2)
3)
4)
5)
=
=
=
=
""
"Student1"
"Student2"
"Student3"
xlWorkSheet.Cells(5,
xlWorkSheet.Cells(5,
xlWorkSheet.Cells(5,
xlWorkSheet.Cells(5,
2)
3)
4)
5)
=
=
=
=
"Term1"
"80"
"65"
"45"
xlWorkSheet.Cells(6,
xlWorkSheet.Cells(6,
xlWorkSheet.Cells(6,
xlWorkSheet.Cells(6,
2)
3)
4)
5)
=
=
=
=
"Term2"
"78"
"72"
"60"
xlWorkSheet.Cells(7, 2) = "Term3"
xlWorkSheet.Cells(7, 3) = "82"
xlWorkSheet.Cells(7, 4) = "80"
xlWorkSheet.Cells(7, 5) = "65"
xlWorkSheet.Cells(8,
xlWorkSheet.Cells(8,
xlWorkSheet.Cells(8,
xlWorkSheet.Cells(8,
2)
3)
4)
5)
=
=
=
=
"Term4"
"75"
"82"
"68"
xlWorkSheet.Cells(9,
xlWorkSheet.Cells(9,
xlWorkSheet.Cells(9,
xlWorkSheet.Cells(9,
2)
3)
4)
5)
=
=
=
=
"Total"
"315"
"299"
"238"
Finally
GC.Collect()
End Try
End Sub
End Class