npoi icon indicating copy to clipboard operation
npoi copied to clipboard

ISheet.SetDefaultColumnStyle not applied to new cells

Open Xriuk opened this issue 1 year ago • 5 comments

Hi, I'm creating a new sheet with a header row and then the data, I'm trying to apply default style to the whole column, so all the rows after the header will have the same style.

Here's my code:

var sheet = workbook.CreateSheet(sheetName);

// Create header
var rowIndex = 0;
var columnIndex = 0;
var properties = entityType.GetPropertiesOrdered();
var row = sheet.CreateRow(rowIndex++);
foreach (var property in properties) {
  var cell = row.CreateCell(columnIndex);
  cell.CellStyle = (...);
  cell.SetCellValue(property.Name);
  
  sheet.SetDefaultColumnStyle(columnIndex, styles.Default); // Here I set the default style
  
  columnIndex++;
}

// Populate rows
foreach (var dataRow in rows) {
  row = sheet.CreateRow(rowIndex++);
  columnIndex = 0;
  foreach (var property in properties) {
    var cell = row.CreateCell(columnIndex++);

    //cell.CellStyle = styles.Default;

    cell.SetCellValue(...);
  }
}

workbook.Write(..., true);

But this way the cells are not styled, if I instead apply the style manually (by uncommenting the code above) to all the cells then it works.

Xriuk avatar Dec 12 '22 12:12 Xriuk