nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.7k stars 1.43k forks source link

ISheet.SetDefaultColumnStyle not applied to new cells #976

Open Xriuk opened 1 year ago

Xriuk commented 1 year ago

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.

tonyqus commented 1 year ago

Is this file xlsx?

Xriuk commented 1 year ago

Is this file xlsx?

Yep

davhdavh commented 9 months ago

It doesn't work at all on column level:

// Create a new workbook
var workbook = new XSSFWorkbook();
// Create a new sheet
var sheet = workbook.CreateSheet("gridExport_0");

//// Create a style for the rows
var style = workbook.CreateCellStyle();

// Create a solid foreground fill
style.FillPattern = FillPattern.SolidForeground;
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index; // Set the foreground color
style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Black.Index; // Set the background color

var datetimeStyle = workbook.CreateCellStyle();
var        dataformat = workbook.CreateDataFormat();
datetimeStyle.DataFormat = dataformat.GetFormat("yyyy-MM-dd HH:mm:ss");

// Applying the style to the first row (index 0)
sheet.SetDefaultColumnStyle(0, datetimeStyle);
sheet.SetDefaultColumnStyle(1, datetimeStyle);
var row = sheet.CreateRow(0);
row.RowStyle = style;
var cell = row.CreateCell(0);
cell.SetCellType(CellType.String);
cell.SetCellValue("date1");
cell = row.CreateCell(1);
sheet.SetDefaultColumnStyle(1, datetimeStyle);
cell.SetCellType(CellType.String);
cell.SetCellValue("Date2");

row = sheet.CreateRow(1);
cell = row.CreateCell(0);
cell.SetCellType(CellType.Numeric);
cell.SetCellValue(DateTime.UtcNow);
cell = row.CreateCell(1);
sheet.SetDefaultColumnStyle(1, datetimeStyle);
cell.SetCellType(CellType.Numeric);
cell.SetCellValue(DateTime.UtcNow);
//cell.CellStyle = datetimeStyle;

//create file
using var file = new FileStream("/app/test.xlsx", FileMode.Create, FileAccess.Write);
workbook.Write(file);
tonyqus commented 5 months ago

POI Bug 51037

Bykiev commented 5 months ago

https://github.com/apache/poi/pull/406 PR should be applied too, because fix in https://github.com/apache/poi/pull/338 cause performance issue

jzyzxx commented 3 weeks ago

POI Bug 51037

I have met same trouble,Is this bug fixed in 2.7.1?

Bykiev commented 3 weeks ago

POI Bug 51037

I have met same trouble,Is this bug fixed in 2.7.1?

HI, unfortunately, no. I've tried to create a PR, but some of the tests are failing. You can contribute if you wish: https://github.com/nissl-lab/npoi/pull/1340