Skip to content

Commit

Permalink
make the option headers bold
Browse files Browse the repository at this point in the history
  • Loading branch information
KochTobi committed Sep 24, 2024
1 parent c900260 commit 7548ea6
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import static java.util.Objects.nonNull;

import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
Expand Down Expand Up @@ -89,6 +92,18 @@ public static String getCellValueAsString(Cell cell) {
};
}

static Optional<CellStyle> findBoldCellStyle(Sheet sheet) {
for (int i = 0; i < sheet.getWorkbook().getNumCellStyles(); i++) {
CellStyle cellStyleAt = sheet.getWorkbook().getCellStyleAt(i);
int fontIndex = cellStyleAt.getFontIndex();
Font fontAt = sheet.getWorkbook().getFontAt(fontIndex);
if (fontAt.getBold()) {
return Optional.of(cellStyleAt);
}
}
return Optional.empty();
}

/**
* Adds values to the sheet and returns the named area where they were added.
*
Expand All @@ -110,6 +125,9 @@ public static Name createOptionArea(Sheet sheet, String propertyName,
Cell headerRowCell = headerRow.createCell(columnIndex);
headerRowCell.setCellValue(propertyName);

//if a bold cell style exists, use it
findBoldCellStyle(sheet).ifPresent(headerRowCell::setCellStyle);

var startIndex = 1; // ignore the header at 0
var rowIndex = startIndex;
for (String option : options) {
Expand Down

0 comments on commit 7548ea6

Please sign in to comment.