This project is read-only.

Applying cell style to a merged region

Aug 3, 2012 at 8:34 PM

Given the following...

      IFont font3 = wb.CreateFont();
      font3.FontHeightInPoints = ((short)8);
      font3.Boldweight = (short)FontBoldWeight.NORMAL;
      style = wb.CreateCellStyle();
      style.Alignment = HorizontalAlignment.CENTER;
      style.BorderTop = CellBorderType.THIN;
      style.BorderBottom = CellBorderType.THIN;
      style.BorderLeft = CellBorderType.THIN;
      style.BorderRight = CellBorderType.THIN;
      style.IsLocked = true;
      style.VerticalAlignment = VerticalAlignment.CENTER;
      styles.Add("title_fema", style);

      // create the row that will contain text common to all sheets
      row = ws.CreateRow(rowIndex++);
      row.HeightInPoints = 17;
      cell = row.CreateCell(0);
      cell.CellStyle = _cellStyles["title_fema"];

      // merge all cells on the row
      cellRange = new CellRangeAddress(row.RowNum, row.RowNum, 0, columnCount);

When the spreadsheet is opened in Excel, only the first cell in the row has borders. Ideas anyone?


Mar 5, 2013 at 2:18 PM
Hello, I pretty sure you solved this one or no longer need it but I'm adding a solution anyway for anyone in the future.
I'd the same issue and I solved it by applying the same style to all cells included in the merged region before adding the merged region to the sheet.

so for you own code, it will be something like this before the "merge all cells on the row" comment
for(var i=0; i<=columnCount ; i++)
    var cell = row.GetCell(i) ?? row.CreateCell(i);
    cell.CellStyle = _cellStyles["title_fema"];