Free Support Forum -

Help with formatting


I’m working on a worksheet. There are a couple of things I’m having trouble doing.

Firstly the worksheet I am working on contains 6 columns; I’d like to undertake some conditional formatting that basically changes the background colour if a value is found in the row. So if cell E1 contains the text ‘Test’ then change the background colour on cells A1:E1

The next challenge is to set the ‘wrap text’ field on all populated cells, set the column widths and then auto expand the rows so that it expands to its maximum?

I’ve read the guides but they don’t really provide the functionality I specifically need – so I wuld appreciate some examples.

Many Thanks.


Thanks for your query.

  1. Well, you may check the cell’s value (via using Cell.StringValue attribute) and format the range of cells (A1:E1) accordingly, see the sample code segment for your reference:
    Sample code:

         Range range = sheet.Cells.CreateRange("A1", "E1");
             Style style = workbook.CreateStyle();
             style.Pattern = BackgroundType.Solid;
             style.ForegroundColor = Color.Green;
             //Specify font attributes if you want.
             style.Font.Color = Color.White;
             style.HorizontalAlignment = TextAlignmentType.Center;
             StyleFlag flag1 = new StyleFlag();
             flag1.CellShading = true;
             flag1.FontColor = true;
             flag1.HorizontalAlignment = true;
             range.ApplyStyle(style, flag1);
  2. See the sample code for your reference:
    Sample code:

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[“Sheet1”];
Cells cell = ws.Cells;
cell.SetColumnWidth(0, 35);
cell[0, 0].PutValue(“Voor meer informatie\nga naar de Informatiepagina op\nThis is the third line”);
cell[0, 0].GetStyle().IsTextWrapped = true;
wb.Save(“d:\test\out1.xls” );

Hope, this helps a bit.

Thats great thanks. All working now.


Good to know that the suggested code segments are helpful to you. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.