Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISheet.SetDefaultColumnStyle not applied to new cells #976

Open
Xriuk opened this issue Dec 12, 2022 · 9 comments
Open

ISheet.SetDefaultColumnStyle not applied to new cells #976

Xriuk opened this issue Dec 12, 2022 · 9 comments

Comments

@Xriuk
Copy link

Xriuk commented Dec 12, 2022

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 tonyqus added this to the NPOI 2.6.1 milestone Dec 12, 2022
@tonyqus tonyqus modified the milestones: NPOI 2.6.1, NPOI 2.6.2 Feb 24, 2023
@tonyqus
Copy link
Member

tonyqus commented Feb 24, 2023

Is this file xlsx?

@tonyqus tonyqus removed this from the NPOI 2.6.2 milestone Feb 24, 2023
@Xriuk
Copy link
Author

Xriuk commented Feb 24, 2023

Is this file xlsx?

Yep

@tonyqus tonyqus added this to the NPOI 2.6.1 milestone Feb 25, 2023
@tonyqus tonyqus modified the milestones: NPOI 2.6.1, NPOI 2.6.2 Jun 13, 2023
@davhdavh
Copy link

davhdavh commented Jan 3, 2024

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 tonyqus modified the milestones: NPOI 2.7.0, NPOI 2.7.1 Feb 9, 2024
@tonyqus
Copy link
Member

tonyqus commented May 4, 2024

POI Bug 51037

@Bykiev
Copy link
Collaborator

Bykiev commented May 4, 2024

apache/poi#406 PR should be applied too, because fix in apache/poi#338 cause performance issue

@Bykiev Bykiev self-assigned this May 6, 2024
@tonyqus tonyqus modified the milestones: NPOI 2.7.1, NPOI 2.7.2 Jun 22, 2024
@jzyzxx
Copy link

jzyzxx commented Sep 19, 2024

POI Bug 51037

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

@Bykiev
Copy link
Collaborator

Bykiev commented Sep 19, 2024

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: #1340

@glenn2223
Copy link

glenn2223 commented Nov 8, 2024

HI, unfortunately, no. I've tried to create a PR, but some of the tests are failing. You can contribute if you wish: #1340

That PR was closed, is there a reason? Has a fix already been applied by another PR 🤞

@Bykiev
Copy link
Collaborator

Bykiev commented Nov 8, 2024

HI, unfortunately, no. I've tried to create a PR, but some of the tests are failing. You can contribute if you wish: #1340

That PR was closed, is there a reason? Has a fix already been applied by another PR 🤞

There was an issue with failing unit tests, help is appreciated

@tonyqus tonyqus modified the milestones: NPOI 2.7.2, NPOI 2.7.3 Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants