Wednesday 5 September 2012

VB.NET - Excel - How to format font and interior color

       Dim fileTest As String = "C:\Temp\ExcelTest\test.xlsx"
        If File.Exists(fileTest) Then
            File.Delete(fileTest)
        End If

        Dim oExcel As Object
        oExcel = CreateObject("Excel.Application")
        Dim oBook As Excel.Workbook
        Dim oSheet As Excel.Worksheet

        oBook = oExcel.Workbooks.Add
        oSheet = oExcel.Worksheets(1)

        oSheet.Range("A1").Value = "Name"
        oSheet.Range("B1").Value = "Salary"
        oSheet.Range("A2").Value = "Frank"
        oSheet.Range("B2").Value = 32000

        ' now let's format it.
        oSheet.Range("A1:B1").Interior.Color = RGB(204, 204, 255)
        oSheet.Range("B2").NumberFormat = "$#,##0.00;[Red]$#,##0.00"

        oBook.SaveAs(fileTest)
        oBook.Close()
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing

Same example on YouTube

2 comments:

  1. Thanks for taking the time to discuss this, I feel strongly that love and read more on this topic. If possible, such as gain knowledge, would you mind updating your blog with additional information? It is very useful for me. this

    ReplyDelete