We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Setting color of individual cells not working for me

I am attempting to change both back and fore ground colors of individual cells (rather than complete rows and/or columns) using the code below but the best I get is foreground color change. My version of cells is 4.5 working in VS2005. Is there something I am doing incorrectly?

Thanks for your assistance

   Public Sub DoingColors()
      Dim wb As Workbook = New Workbook()
      wb.Worksheets.Add()
 
      Dim ws As Worksheet = wb.Worksheets(0)
 
      ws.Cells("A1").Style.Pattern = BackgroundType.VerticalStripe
 
      ws.Cells("A1").Style.BackgroundColor = Color.AliceBlue
      ws.Cells("A1").Style.ForegroundColor = Color.Black
      ws.Cells("A1").PutValue("Hello World")
 
      ws.Cells("A2").PutValue(100)
 
      Dim Data As Cell = ws.Cells("B1")
      Data.PutValue(Today)
      Data.Style.Number = 17
      Data.Style.BackgroundColor = Color.Black
      Data.Style.ForegroundColor = Color.White
 
      Data = ws.Cells("B2")
      Data.PutValue(Today)
      Data.Style.Number = 18
      Data.Style.BackgroundColor = Color.Red
      Data.Style.Font.Color = Color.White
      Data.Style.Font.IsBold = True
 
      wb.Save("MyBook2003.xls", FileFormatType.Excel2003)
   End Sub

    Public Sub PrepareCellsLibrary()
      If IO.File.Exists("Aspose.Cells.lic") Then
         Dim license As Aspose.Cells.License = New Aspose.Cells.License
         license.SetLicense("Aspose.Cells.lic")
 
         If Not IO.Directory.Exists(My.Settings.ExcelFolder) Then
            IO.Directory.CreateDirectory(My.Settings.ExcelFolder)
         End If
      End If
   End Sub

Hi Kevin,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.

Please try the attached latest version of Aspose.Cells. Also I have modified your code a bit to set pattern for background color. I would like to mention that for any cell, when you are setting the background color, you have to set the Style.Pattern property as well. Please see the following code which will help you get your desired result,

Dim wb As Workbook = New Workbook()

wb.Worksheets.Add()

Dim ws As Worksheet = wb.Worksheets(0)

ws.Cells("A1").Style.Pattern = BackgroundType.VerticalStripe

ws.Cells("A1").Style.BackgroundColor = System.Drawing.Color.AliceBlue

ws.Cells("A1").Style.ForegroundColor = System.Drawing.Color.Black

ws.Cells("A1").PutValue("Hello World")

ws.Cells("A2").PutValue(100)

Dim Data As Cell = ws.Cells("B1")

Data.PutValue(Today)

Data.Style.Number = 17

Data.Style.Pattern = BackgroundType.VerticalStripe

Data.Style.BackgroundColor = System.Drawing.Color.Black

Data.Style.ForegroundColor = System.Drawing.Color.White

Data = ws.Cells("B2")

Data.PutValue(Today)

Data.Style.Number = 18

Data.Style.Pattern = BackgroundType.VerticalStripe

Data.Style.BackgroundColor = System.Drawing.Color.Red

Data.Style.Font.Color = System.Drawing.Color.White

Data.Style.Font.IsBold = True

wb.Save("MyBook2003.xls", FileFormatType.Excel2003)

Thank You & Best Regards,

So the following code does work but what about setting a background color without a pattern?


      ws.Cells("A1").Style.Pattern = BackgroundType.VerticalStripe
      ws.Cells("A1").Style.BackgroundColor = Color.Yellow
      ws.Cells("A1").Style.ForegroundColor = Color.DarkMagenta
      ws.Cells("A1").PutValue("Hello World This is a test")

I was hoping I could do something like below (see highlight code) but that caused no background color change.

      ws.Cells("A1").Style.Pattern = BackgroundType.Solid
      ws.Cells("A1").Style.BackgroundColor = Color.Yellow
      ws.Cells("A1").Style.ForegroundColor = Color.DarkMagenta
      ws.Cells("A1").PutValue("Hello World This is a test")

Concerning the attached DLL, I got an error, should there been an XML file with it? I went back to the original one.

Thanks for your assistance so far

Hi Kevin,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for considering Aspose.


First, MS Excel 97-2003 only contains 56 kinds of color in the standard palette. Please check the following link for color list in Workbook.ChangePalette method.

http://www.aspose.com/documentation/file-format-components/aspose.cells-for-.net-and-java/aspose.cells.workbook.changepalette.html

So, if you want to set a custom color, you have to use Workbook.ChangePalette method to change the color palette first.

See following codes to change the color palette:


workbook.ChangePalette(System.Drawing.Color.DarkMagenta, 55);
ws.Cells("A1").Style.Pattern = BackgroundType.VerticalStripe
ws.Cells("A1").Style.BackgroundColor = Color.Yellow
ws.Cells("A1").Style.ForegroundColor = Color.DarkMagenta
ws.Cells("A1").PutValue("Hello World This is a test")

Now, if you need to set the pattern as Solid then only set the Style.ForegroundColor, no need to set the Style.BackgroundColor.

Please see the following code to add foreground color when Background type is Solid:

workbook.ChangePalette(System.Drawing.Color.DarkMagenta, 55);
ws.Cells("A1").Style.Pattern = BackgroundType.Solid
ws.Cells("A1").Style.ForegroundColor = Color.DarkMagenta
ws.Cells("A1").PutValue("Hello World This is a test")

As per the issue regarding the error in the new dll, please send us a bit more details about the error you are getting and if it is possible, please create a sample console application with the new dll to generate the error and post it here. we will figure it out soon.

Thank You & Best Regards,