Wednesday, July 1, 2015

NPOI set custom cell background color [RGB format]

    Public Sub SetCellBackColorCustomColorRGB()
        Dim srcWB As HSSFWorkbook = Nothing
        Dim filePath = "C:\test.xls"
        Using fs As New FileStream(filePath, FileMode.Open, FileAccess.Read)
            srcWB = New HSSFWorkbook(fs)
        End Using

        Dim sheet As HSSFSheet = srcWB.GetSheetAt(0)
        Dim cell As HSSFCell = sheet.GetRow(1).GetCell(1)
        Dim style As HSSFCellStyle = srcWB.CreateCellStyle

        'Get custom palette
        Dim palette = srcWB.GetCustomPalette

        'Set color index at palette
        palette.SetColorAtIndex(57, 184, 255, 113)

        'Set color from color palette
        style.FillForegroundColor = palette.GetColor(57).GetIndex
        style.FillPattern = FillPattern.SolidForeground

        cell.CellStyle = style

        Dim savePath As String = filePath
        Using fs As New FileStream(savePath, FileMode.Create, FileAccess.Write)
            srcWB.Write(fs)
        End Using

        MsgBox("done")

    End Sub

2 comments:

  1. That solved my problem. thank you.

    ReplyDelete
  2. Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. http://psiprograms.com

    ReplyDelete