Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,10 @@ public void setCellStyle(CellStyle style)
}

/**
* Return the cell's style.
* Return the cell's style. Since POI v5.2.3, this returns the column style if the
* cell has no style of its own. If no column default style is set, the row default style is checked.
* This method has always fallen back to return the default style
* if there is no other style to return.
*
* @return the cell's style. Never null. Default cell style has zero index and can be obtained as
* <code>workbook.getCellStyleAt(0)</code>
Expand All @@ -593,24 +596,37 @@ public void setCellStyle(CellStyle style)
@Override
public CellStyle getCellStyle()
{
if (_style == null) {
CellStyle style = getDefaultCellStyleFromColumn();
if (style == null) {
SXSSFWorkbook wb = getSheet().getWorkbook();
style = wb.getCellStyleAt(0);
}
_style = style;
if (_style != null) {
return _style;
}
CellStyle style = getDefaultCellStyleFromColumn();
if (style == null) {
style = getDefaultCellStyleFromRow();
}
if (style == null) {
SXSSFWorkbook wb = getSheet().getWorkbook();
style = wb.getCellStyleAt(0);
}
return _style;
return style;
}

private CellStyle getDefaultCellStyleFromColumn() {
CellStyle style = null;
SXSSFSheet sheet = getSheet();
if (sheet != null) {
style = sheet.getColumnStyle(getColumnIndex());
int idx = sheet._sh.getColumnHelper().getColDefaultStyle(getColumnIndex());
if (idx >= 0) {
return getSheet().getWorkbook().getCellStyleAt(idx);
}
}
return style;
return null;
}

private CellStyle getDefaultCellStyleFromRow() {
SXSSFRow row = (SXSSFRow) getRow();
if (row != null && row.isFormatted()) {
return row.getRowStyle();
}
return null;
}

/**
Expand Down
33 changes: 21 additions & 12 deletions poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ public XSSFCellStyle getCellStyle() {
if (style == null) {
style = getDefaultCellStyleFromColumn();
}
if (style == null) {
style = getDefaultCellStyleFromRow();
}
if (style == null && _stylesSource.getNumCellStyles() > 0) {
style = _stylesSource.getStyleAt(0);
}
return style;
}

Expand All @@ -587,24 +593,27 @@ private XSSFCellStyle getExplicitCellStyle() {
}

private XSSFCellStyle getDefaultCellStyleFromColumn() {
XSSFCellStyle style = null;
XSSFSheet sheet = getSheet();
if (sheet != null) {
style = (XSSFCellStyle) sheet.getColumnStyle(getColumnIndex());
int idx = sheet.getColumnHelper().getColDefaultStyle(getColumnIndex());
if (idx >= 0) {
return _stylesSource.getStyleAt(idx);
}
}
return style;
return null;
}

private XSSFCellStyle getDefaultCellStyleFromRow() {
XSSFRow row = getRow();
if (row != null && row.isFormatted()) {
return row.getRowStyle();
}
return null;
}

protected void applyDefaultCellStyleIfNecessary() {
XSSFCellStyle style = getExplicitCellStyle();
if (style == null) {
XSSFSheet sheet = getSheet();
if (sheet != null) {
XSSFCellStyle defaultStyle = getDefaultCellStyleFromColumn();
if (defaultStyle != null) {
setCellStyle(defaultStyle);
}
}
if (getExplicitCellStyle() == null) {
setCellStyle(getCellStyle());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@
package org.apache.poi.xssf.streaming;

import org.apache.poi.ss.tests.usermodel.BaseTestXRow;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* Tests for XSSFRow
Expand Down Expand Up @@ -66,4 +76,85 @@ void testCellColumn() throws IOException {
}
}

@Test
void testSetRowStylePropagatedToCells() throws IOException {
try (SXSSFWorkbook wb = new SXSSFWorkbook()) {
SXSSFSheet sheet = wb.createSheet("test");

// create a bold style
XSSFCellStyle boldStyle = (XSSFCellStyle) wb.createCellStyle();
XSSFFont boldFont = (XSSFFont) wb.createFont();
boldFont.setBold(true);
boldStyle.setFont(boldFont);

// apply style to row, then create cells
SXSSFRow row = sheet.createRow(0);
row.setRowStyle(boldStyle);

SXSSFCell cell0 = row.createCell(0);
cell0.setCellValue("Header A");
SXSSFCell cell1 = row.createCell(1);
cell1.setCellValue("Header B");

// cells without explicit style should inherit the row style via getCellStyle()
CellStyle cellStyle0 = cell0.getCellStyle();
assertNotNull(cellStyle0);
assertTrue(wb.getFontAt(cellStyle0.getFontIndex()).getBold(),
"cell should inherit bold font from row style");

CellStyle cellStyle1 = cell1.getCellStyle();
assertNotNull(cellStyle1);
assertTrue(wb.getFontAt(cellStyle1.getFontIndex()).getBold(),
"cell should inherit bold font from row style");

// a cell with an explicit style should NOT be overridden by row style
CellStyle plainStyle = wb.createCellStyle();
SXSSFCell cell2 = row.createCell(2);
cell2.setCellStyle(plainStyle);
cell2.setCellValue("Plain");

CellStyle cellStyle2 = cell2.getCellStyle();
assertFalse(wb.getFontAt(cellStyle2.getFontIndex()).getBold(),
"cell with explicit non-bold style should remain non-bold");
}
}

@Test
void testSetRowStylePropagatedAfterWrite() throws IOException {
try (SXSSFWorkbook wb = new SXSSFWorkbook()) {
SXSSFSheet sheet = wb.createSheet("test");

// create a bold style
XSSFCellStyle boldStyle = (XSSFCellStyle) wb.createCellStyle();
XSSFFont boldFont = (XSSFFont) wb.createFont();
boldFont.setBold(true);
boldStyle.setFont(boldFont);

// apply style to row, then create cells
SXSSFRow row = sheet.createRow(0);
row.setRowStyle(boldStyle);
row.createCell(0).setCellValue("Column A");
row.createCell(1).setCellValue("Column B");
row.createCell(2).setCellValue("Column C");

// write and read back — SXSSFITestDataProvider returns XSSFWorkbook
XSSFWorkbook wb2 = SXSSFITestDataProvider.instance.writeOutAndReadBack(wb);

XSSFSheet sheet2 = wb2.getSheet("test");
XSSFRow row2 = sheet2.getRow(0);
assertNotNull(row2);

for (int i = 0; i < 3; i++) {
XSSFCell cell = row2.getCell(i);
assertNotNull(cell, "cell " + i + " should exist after read-back");
XSSFCellStyle style = cell.getCellStyle();
assertNotNull(style, "cell " + i + " should have a style after read-back");
assertTrue(wb2.getFontAt(style.getFontIndex()).getBold(),
"cell " + i + " should be bold after write/read-back");
}

wb2.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,86 @@ void duplicateRows() throws IOException {
}
}

@Test
void testSetRowStylePropagatedToCells() throws IOException {
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
final XSSFSheet sheet = workbook.createSheet("test");

// create a bold style
XSSFCellStyle boldStyle = workbook.createCellStyle();
XSSFFont boldFont = workbook.createFont();
boldFont.setBold(true);
boldStyle.setFont(boldFont);

// apply style to row, then create cells
final XSSFRow row = sheet.createRow(0);
row.setRowStyle(boldStyle);

XSSFCell cell0 = row.createCell(0);
cell0.setCellValue("Header A");
XSSFCell cell1 = row.createCell(1);
cell1.setCellValue("Header B");

// cells without explicit style should inherit the row style via getCellStyle()
XSSFCellStyle cellStyle0 = cell0.getCellStyle();
assertNotNull(cellStyle0);
assertTrue(workbook.getFontAt(cellStyle0.getFontIndex()).getBold(),
"cell should inherit bold font from row style");

XSSFCellStyle cellStyle1 = cell1.getCellStyle();
assertNotNull(cellStyle1);
assertTrue(workbook.getFontAt(cellStyle1.getFontIndex()).getBold(),
"cell should inherit bold font from row style");

// a cell with an explicit style should NOT be overridden by row style
XSSFCellStyle plainStyle = workbook.createCellStyle();
XSSFCell cell2 = row.createCell(2);
cell2.setCellStyle(plainStyle);
cell2.setCellValue("Plain");

XSSFCellStyle cellStyle2 = cell2.getCellStyle();
assertFalse(workbook.getFontAt(cellStyle2.getFontIndex()).getBold(),
"cell with explicit non-bold style should remain non-bold");
}
}

@Test
void testSetRowStylePropagatedAfterWrite() throws IOException {
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
final XSSFSheet sheet = workbook.createSheet("test");

// create a bold style
XSSFCellStyle boldStyle = workbook.createCellStyle();
XSSFFont boldFont = workbook.createFont();
boldFont.setBold(true);
boldStyle.setFont(boldFont);

// apply style to row, then create cells
final XSSFRow row = sheet.createRow(0);
row.setRowStyle(boldStyle);
row.createCell(0, CellType.STRING).setCellValue("Column A");
row.createCell(1, CellType.STRING).setCellValue("Column B");
row.createCell(2, CellType.STRING).setCellValue("Column C");

// write and read back — exercises onDocumentWrite() ->
// applyDefaultCellStyleIfNecessary()
try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook)) {
XSSFSheet sheet2 = wb2.getSheet("test");
XSSFRow row2 = sheet2.getRow(0);
assertNotNull(row2);

for (int i = 0; i < 3; i++) {
XSSFCell cell = row2.getCell(i);
assertNotNull(cell, "cell " + i + " should exist after read-back");
XSSFCellStyle style = cell.getCellStyle();
assertNotNull(style, "cell " + i + " should have a style after read-back");
assertTrue(wb2.getFontAt(style.getFontIndex()).getBold(),
"cell " + i + " should be bold after write/read-back");
}
}
}
}

private void fillData(int startAtRow, Sheet sheet) {
Row header = sheet.createRow(0);
for (int rownum = startAtRow; rownum < 2; rownum++) {
Expand Down
18 changes: 17 additions & 1 deletion poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,30 @@ public void setCellStyle(HSSFCellStyle style) {

/**
* get the style for the cell. This is a reference to a cell style contained in the workbook
* object.
* object. If the cell does not have an explicit style assigned, this method will fall back
* to returning the column or row style (if any).
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(int)
*/
@Override
public HSSFCellStyle getCellStyle()
{
short styleIndex=_record.getXFIndex();
ExtendedFormatRecord xf = _book.getWorkbook().getExFormatAt(styleIndex);

if (styleIndex == 0x0f) {
// Fallback to column style
HSSFCellStyle colStyle = _sheet.getColumnStyle(getColumnIndex());
if (colStyle != null) {
return colStyle;
}

// Fallback to row style
HSSFCellStyle rowStyle = getRow().getRowStyle();
if (rowStyle != null) {
return rowStyle;
}
}

return new HSSFCellStyle(styleIndex, xf, _book);
}

Expand Down
Loading