下面列出了org.apache.poi.ss.usermodel.SheetConditionalFormatting#org.apache.poi.ss.usermodel.IndexedColors 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBold(true);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
styles.put("body", style);
return styles;
}
/**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBold(true);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
styles.put("body", style);
return styles;
}
public CellStyle getHeaderCellStyle(SXSSFWorkbook workbook) {
if (headCellStyle == null) {
headCellStyle = workbook.getXSSFWorkbook().createCellStyle();
headCellStyle.setBorderTop(BorderStyle.NONE);
headCellStyle.setBorderRight(BorderStyle.NONE);
headCellStyle.setBorderBottom(BorderStyle.NONE);
headCellStyle.setBorderLeft(BorderStyle.NONE);
headCellStyle.setAlignment(HorizontalAlignment.CENTER);
headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
XSSFColor color = new XSSFColor(new java.awt.Color(217, 217, 217));
headCellStyle.setFillForegroundColor(color);
headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font font = workbook.createFont();
font.setFontName("微软雅黑");
font.setColor(IndexedColors.ROYAL_BLUE.index);
font.setBold(true);
headCellStyle.setFont(font);
headCellStyle.setDataFormat(workbook.createDataFormat().getFormat("@"));
}
return headCellStyle;
}
@SuppressWarnings("unused")
private TableStyle getTableStyle1() {
TableStyle tableStyle = new TableStyle();
Font headFont = new Font();
headFont.setBold(true);
headFont.setFontHeightInPoints((short)22);
headFont.setFontName("楷体");
tableStyle.setTableHeadFont(headFont);
tableStyle.setTableHeadBackGroundColor(IndexedColors.LIGHT_BLUE);
Font contentFont = new Font();
contentFont.setBold(true);
contentFont.setFontHeightInPoints((short)22);
contentFont.setFontName("黑体");
tableStyle.setTableContentFont(contentFont);
tableStyle.setTableContentBackGroundColor(IndexedColors.LIGHT_GREEN);
return tableStyle;
}
/**
* 样式
*
* @return
*/
protected HorizontalCellStyleStrategy buildStyle() {
WriteFont baseFont = new WriteFont();
baseFont.setFontHeightInPoints((short) 12);
baseFont.setColor(IndexedColors.BLACK.getIndex());
// 头
WriteCellStyle headStyle = new WriteCellStyle();
headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headStyle.setWriteFont(baseFont);
// 内容
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
// 这里需要指定 FillPatternType 为 FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色
// 头默认了 FillPatternType 所以可以不指定
contentStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
contentStyle.setWriteFont(baseFont);
contentStyle.setBorderBottom(BorderStyle.THIN);
contentStyle.setBorderRight(BorderStyle.THIN);
return new HorizontalCellStyleStrategy(headStyle, contentStyle);
}
/**
* Create styled workbook.
* @throws IOException If fails
*/
@Test
public void createsWorkbookWithStyles() throws IOException {
final Workbook workbook = new XsWorkbook(
new XsSheet(
new XsRow(
new TextCell("text")
)
)
).with(
new XsStyle(
new ForegroundColor(
IndexedColors.GOLD.getIndex()
)
)
).asWorkbook();
MatcherAssert.assertThat(
workbook.getSheetAt(0).getRow(0)
.getCell(0).getCellStyle().getFillForegroundColor(),
Matchers.equalTo(IndexedColors.GOLD.getIndex())
);
}
@Deprecated
private WriteCellStyle buildWriteCellStyle(Font font, IndexedColors indexedColors) {
WriteCellStyle writeCellStyle = new WriteCellStyle();
if (indexedColors != null) {
writeCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
writeCellStyle.setFillForegroundColor(indexedColors.getIndex());
}
if (font != null) {
WriteFont writeFont = new WriteFont();
writeFont.setFontName(font.getFontName());
writeFont.setFontHeightInPoints(font.getFontHeightInPoints());
writeFont.setBold(font.isBold());
writeCellStyle.setWriteFont(writeFont);
}
return writeCellStyle;
}
public TableStyle style() {
TableStyle tableStyle = new TableStyle();
Font headFont = new Font();
headFont.setBold(true);
headFont.setFontHeightInPoints((short)22);
headFont.setFontName("楷体");
tableStyle.setTableHeadFont(headFont);
tableStyle.setTableHeadBackGroundColor(IndexedColors.BLUE);
Font contentFont = new Font();
contentFont.setBold(true);
contentFont.setFontHeightInPoints((short)22);
contentFont.setFontName("黑体");
tableStyle.setTableContentFont(contentFont);
tableStyle.setTableContentBackGroundColor(IndexedColors.GREEN);
return tableStyle;
}
private void readAndWrite(File file) {
SimpleColumnWidthStyleStrategy simpleColumnWidthStyleStrategy = new SimpleColumnWidthStyleStrategy(50);
SimpleRowHeightStyleStrategy simpleRowHeightStyleStrategy =
new SimpleRowHeightStyleStrategy((short)40, (short)50);
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short)20);
headWriteCellStyle.setWriteFont(headWriteFont);
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontHeightInPoints((short)20);
headWriteCellStyle.setWriteFont(contentWriteFont);
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
OnceAbsoluteMergeStrategy onceAbsoluteMergeStrategy = new OnceAbsoluteMergeStrategy(2, 2, 0, 1);
EasyExcel.write(file, StyleData.class).registerWriteHandler(simpleColumnWidthStyleStrategy)
.registerWriteHandler(simpleRowHeightStyleStrategy).registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(onceAbsoluteMergeStrategy).sheet().doWrite(data());
EasyExcel.read(file, StyleData.class, new StyleDataListener()).sheet().doRead();
}
@XlsPostSave
public void handlePostSave(final Sheet sheet) {
if(!name.equals("平均")) {
return;
}
final Workbook book = sheet.getWorkbook();
for(Point address : positions.values()) {
Cell cell = POIUtils.getCell(sheet, address);
CellStyle style = book.createCellStyle();
style.cloneStyleFrom(cell.getCellStyle());
// 塗りつぶし
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 罫線の上部を変更
style.setBorderTop(BorderStyle.DOUBLE);
cell.setCellStyle(style);
}
}
SerialWriteWorkbook() {
this.swb = new SXSSFWorkbook(100);
this.sh = this.swb.createSheet();
this.rowIndex = 0;
this.headerStyle = (XSSFCellStyle)swb.createCellStyle();
this.headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
//solid fill
this.headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = swb.createFont();
headerFont.setFontHeightInPoints((short)14);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
this.headerStyle.setFont(headerFont);
}
private int addLangCell(Row header, TmxSegement segment) {
int CellNum = header.getLastCellNum();
if (-1 == CellNum) {
CellNum = 0;
}
Cell createCell = header.createCell(CellNum);
CellStyle cellStyle = wb.createCellStyle();
XSSFFont headerFont = (XSSFFont) wb.createFont();
headerFont.setBold(true);
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFont(headerFont);
createCell.setCellValue(segment.getLangCode());
createCell.setCellStyle(cellStyle);
sh.setColumnWidth(CellNum, (100 * 7 + 5) / 7 * 256);
return CellNum;
}
protected CellStyle getHeaderIntervalStyle(P p) {
String styleId = "header-interval";
CellStyle style = iStyles.get(styleId);
if (style == null) {
style = iWorkbook.createCellStyle();
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
if (p.getWidth() == 1) {
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
}
style.setFont(getFont(p));
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
iStyles.put(styleId, style);
}
return style;
}
protected CellStyle getGridNameStyle(P p) {
String styleId = "grid-name";
CellStyle style = iStyles.get(styleId);
if (style == null) {
style = iWorkbook.createCellStyle();
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFont(getFont(p));
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setWrapText(true);
iStyles.put(styleId, style);
}
return style;
}
protected CellStyle getStyle(A f, boolean dashed, String format) {
String styleId = (dashed ? "D" : "")
+ (f.has(F.BOLD) ? "b" : "") + (f.has(F.ITALIC) ? "i" : "") + (f.has(F.UNDERLINE) ? "u" : "")
+ (f.has(F.RIGHT) ? "R" : f.has(F.CENTER) ? "C" : "L")
+ (f.hasColor() ? "#" + Integer.toHexString(f.getColor().getRGB()) : "")
+ (format == null ? "" : "|" + format);
CellStyle style = iStyles.get(styleId);
if (style == null) {
style = iWorkbook.createCellStyle();
if (dashed) {
style.setBorderTop(BorderStyle.DASHED);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
}
style.setAlignment(f.has(F.RIGHT) ? HorizontalAlignment.RIGHT : f.has(F.CENTER) ? HorizontalAlignment.CENTER : HorizontalAlignment.LEFT);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setFont(getFont(f.has(F.BOLD), f.has(F.ITALIC), f.has(F.UNDERLINE), f.getColor()));
style.setWrapText(true);
if (format != null)
style.setDataFormat(iWorkbook.createDataFormat().getFormat(format));
iStyles.put(styleId, style);
}
return style;
}
/**
* XSSFRichTextString.utfDecode()<br/>
* value.contains("_x")<br/>
* Pattern.compile("_x([0-9A-Fa-f]{4})_");
*/
private static void export2007WithStyle(String filePath) {
try {
OutputStream os = new FileOutputStream(filePath);
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(Globals.SHEETNAME);
XSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellValue(TestUtil.RICH_TEXT_STRINGS[0]
+ escape(TestUtil.REGEX + TestUtil.RICH_TEXT_STRINGS[1]
+ TestUtil.REGEX) + TestUtil.RICH_TEXT_STRINGS[2]);
CellStyle style = sheet.getWorkbook().createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(IndexedColors.BLUE.index);
style.setFont(font);
cell.setCellStyle(style);
// richString.applyFont(font);
wb.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void exportExcelWithStyle() {
try {
String filePath = TestUtil.DOC_PATH + File.separator
+ Globals.EXPORT_PRODUCT;
OutputStream os = new FileOutputStream(filePath);
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(Globals.SHEETNAME);
HSSFRichTextString richString = new HSSFRichTextString(
TestUtil.RICH_TEXT_STRING);
HSSFFont font = wb.createFont();
font.setColor(IndexedColors.BLUE.index);
richString.applyFont(font);
sheet.createRow(0).createCell(0).setCellValue(richString);
wb.write(os);
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static Row createHeaders(Map<String, String> headerMap,
Sheet sheet) {
CellStyle style = sheet.getWorkbook().createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
Row header = sheet.createRow(0);
int columnIndex = 0;
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
String headerText = StringUtils.defaultString(entry.getValue());
Cell cell = createCell(header, columnIndex);
cell.setCellValue(headerText);
cell.setCellStyle(style);
sheet.autoSizeColumn(columnIndex);
columnIndex++;
}
return header;
}
public CellStyle build(ExcelCell cell) {
Optional<CellStyle> cs = cell.findStyle(hashCode());
if (cs.isPresent()) {
return cs.get();
}
CellStyle cellStyle = cell.cloneStyle(hashCode());
addFontFormat(cell, cellStyle);
if (header) {
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
addFormat(cell, cellStyle);
if (backgroundColour != null) {
cellStyle.setFillForegroundColor(backgroundColour.getIndex());
cellStyle.setFillPattern(backgroundFillPatternType != null ? backgroundFillPatternType : FillPatternType.SOLID_FOREGROUND);
}
addAlignment(cellStyle);
return cellStyle;
}
/**
* 创建表格样式
*
* @param wb 工作薄对象
* @return 样式列表
*/
private Map<String, CellStyle> createStyles(Workbook wb)
{
// 写入各条记录,每条记录对应excel表中的一行
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
return styles;
}
/**
* a utility class to create style for a border around a cell
*
* @param wb the workbook
* @return the style to use to put border around a cell
*/
public static CellStyle createBorderedStyle(Workbook wb) {
CellStyle style = wb.createCellStyle();
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
return style;
}
private static CellStyle createBorderedStyle(Workbook wb){
BorderStyle thin = BorderStyle.THIN;
short black = IndexedColors.GREY_50_PERCENT.getIndex();
CellStyle style = wb.createCellStyle();
style.setBorderRight(thin);
style.setRightBorderColor(black);
style.setBorderBottom(thin);
style.setBottomBorderColor(black);
style.setBorderLeft(thin);
style.setLeftBorderColor(black);
style.setBorderTop(thin);
style.setTopBorderColor(black);
return style;
}
private static CellStyle createBorderedStyle(Workbook wb){
BorderStyle thin = BorderStyle.THIN;
short black = IndexedColors.GREY_50_PERCENT.getIndex();
CellStyle style = wb.createCellStyle();
style.setBorderRight(thin);
style.setRightBorderColor(black);
style.setBorderBottom(thin);
style.setBottomBorderColor(black);
style.setBorderLeft(thin);
style.setLeftBorderColor(black);
style.setBorderTop(thin);
style.setTopBorderColor(black);
return style;
}
/**
* 创建表格样式
*
* @param wb 工作薄对象
* @return 样式列表
*/
private Map<String, CellStyle> createStyles(Workbook wb)
{
// 写入各条记录,每条记录对应excel表中的一行
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
CellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderTop(BorderStyle.THIN);
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
Font dataFont = wb.createFont();
dataFont.setFontName("Arial");
dataFont.setFontHeightInPoints((short) 10);
style.setFont(dataFont);
styles.put("data", style);
style = wb.createCellStyle();
style.cloneStyleFrom(styles.get("data"));
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Font headerFont = wb.createFont();
headerFont.setFontName("Arial");
headerFont.setFontHeightInPoints((short) 10);
headerFont.setBold(true);
headerFont.setColor(IndexedColors.WHITE.getIndex());
style.setFont(headerFont);
styles.put("header", style);
return styles;
}
@Test
public void addsStyleToCell() throws IOException {
try (final Workbook workbook = new XSSFWorkbook()) {
final String name = "TimesNewRoman";
final Cell cell = new TextCell("text").with(
new XsStyle()
.with(
new ForegroundColor(
IndexedColors.GREY_25_PERCENT.getIndex()
)
)
.with(new FillPattern(FillPatternType.SOLID_FOREGROUND))
.with(new FontStyle().withName(name))
)
.attachTo(
workbook.createSheet().createRow(0)
);
MatcherAssert.assertThat(
cell.getCellStyle().getFillForegroundColor(),
Matchers.equalTo(IndexedColors.GREY_25_PERCENT.getIndex())
);
MatcherAssert.assertThat(
cell.getCellStyle().getFillPatternEnum(),
Matchers.equalTo(FillPatternType.SOLID_FOREGROUND)
);
MatcherAssert.assertThat(
workbook.getFontAt(
cell.getCellStyle().getFontIndex()
).getFontName(),
Matchers.equalTo(name)
);
}
}
/**
* Create custom cell style.
* @throws IOException If fails
*/
@Test
public void createsCustomStyle() throws IOException {
try (final Workbook wbook = new XSSFWorkbook()) {
final Cell cell = wbook.createSheet()
.createRow(0).createCell(0);
new StyleTemplateTest.GrayBackground().attachTo(cell);
MatcherAssert.assertThat(
cell.getCellStyle().getFillForegroundColor(),
Matchers.equalTo(IndexedColors.GREY_25_PERCENT.getIndex())
);
}
}
/**
* Create custom cell.
* @throws IOException If fails
*/
@Test
public void createsCustomCell() throws IOException {
try (final Workbook workbook = new XSSFWorkbook()) {
final Cell cell = new CellTemplateTest.GrayTextCell()
.attachTo(workbook.createSheet().createRow(0));
MatcherAssert.assertThat(
cell.getCellStyle().getFillForegroundColor(),
Matchers.equalTo(IndexedColors.GREY_25_PERCENT.getIndex())
);
}
}
/**
* Ctor.
*/
GrayTextCell() {
super(
new TextCell("someText")
.with(
new XsStyle(
new ForegroundColor(
IndexedColors.GREY_25_PERCENT.getIndex()
)
)
)
);
}
/**
* Add styled row to a sheet.
* @throws IOException If fails
*/
@Test
public void addsRowWithStyleToSheet() throws IOException {
try (final Workbook wbook = new XSSFWorkbook()) {
final Row row =
new XsRow()
.with(new TextCells("a", "b", "c"))
.with(
new TextCell("text")
.with(
new XsStyle(
new ForegroundColor(
IndexedColors.GOLD.getIndex()
)
)
)
)
.with(
new XsStyle(
new ForegroundColor(
IndexedColors.GREY_50_PERCENT.getIndex()
)
)
)
.attachTo(
wbook.createSheet()
);
MatcherAssert.assertThat(
row.getCell((int) row.getLastCellNum() - 2)
.getCellStyle().getFillForegroundColor(),
Matchers.equalTo(IndexedColors.GREY_50_PERCENT.getIndex())
);
MatcherAssert.assertThat(
row.getCell((int) row.getLastCellNum() - 1)
.getCellStyle().getFillForegroundColor(),
Matchers.equalTo(IndexedColors.GOLD.getIndex())
);
}
}
/**
* @param workbook
* @return
*/
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.setWrapText(true);
newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
newCellStyle.setAlignment(HorizontalAlignment.CENTER);
newCellStyle.setLocked(true);
newCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
newCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
newCellStyle.setBorderTop(BorderStyle.THIN);
newCellStyle.setBorderBottom(BorderStyle.THIN);
newCellStyle.setBorderLeft(BorderStyle.THIN);
newCellStyle.setBorderRight(BorderStyle.THIN);
return newCellStyle;
}