Apache POI Excel Cell Background Foreground and Font Color with 17 Java Code Examples
Tags: Apache POI
Java Code Examples for:
- org.apache.poi.ss.usermodel.FillPatternType
- org.apache.poi.ss.usermodel.IndexedColors
- org.apache.poi.ss.usermodel.CellStyle
17 Java code examples below to show how to use different fill patterns to set up Excel cell background and forground using Apache POI library.
Example for background and foreground color with FillPatternType.ALT_BARS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorAltBarsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.ALT_BARS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.ALT_BARS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-alt-bars.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.BIG_SPOTS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorBigSpotsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.ORANGE.getIndex());
cellStyle.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
cellStyle.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-big-spots.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.BRICKS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorBricksExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.PINK.getIndex());
cellStyle.setFillPattern(FillPatternType.BRICKS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
cellStyle.setFillPattern(FillPatternType.BRICKS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-bricks.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.DIAMONDS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorDiamondsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
cellStyle.setFillPattern(FillPatternType.DIAMONDS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
cellStyle.setFillPattern(FillPatternType.DIAMONDS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-diamonds.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.FINE_DOTS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorFineDotsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.ORCHID.getIndex());
cellStyle.setFillPattern(FillPatternType.FINE_DOTS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
cellStyle.setFillPattern(FillPatternType.FINE_DOTS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-fine-dots.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.LEAST_DOTS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorLeastDotsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.LAVENDER.getIndex());
cellStyle.setFillPattern(FillPatternType.LEAST_DOTS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
cellStyle.setFillPattern(FillPatternType.LEAST_DOTS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-least-dots.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.LESS_DOTS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorLessDotsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.INDIGO.getIndex());
cellStyle.setFillPattern(FillPatternType.LESS_DOTS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
cellStyle.setFillPattern(FillPatternType.LESS_DOTS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-less-dots.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.SOLID_FOREGROUND fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorSolidForegroundExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-solid-foreground.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.SPARSE_DOTS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorSparseDotsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.GOLD.getIndex());
cellStyle.setFillPattern(FillPatternType.SPARSE_DOTS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.GOLD.getIndex());
cellStyle.setFillPattern(FillPatternType.SPARSE_DOTS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-sparse-dots.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.SQUARES fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorSquaresExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
cellStyle.setFillPattern(FillPatternType.SQUARES);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
cellStyle.setFillPattern(FillPatternType.SQUARES);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-squares.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THICK_BACKWARD_DIAG fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThickBackwardDiagExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.CORAL.getIndex());
cellStyle.setFillPattern(FillPatternType.THICK_BACKWARD_DIAG);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.CORAL.getIndex());
cellStyle.setFillPattern(FillPatternType.THICK_BACKWARD_DIAG);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thick-backward-diag.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THICK_HORZ_BANDS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThickHorzBandsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
cellStyle.setFillPattern(FillPatternType.THICK_HORZ_BANDS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
cellStyle.setFillPattern(FillPatternType.THICK_HORZ_BANDS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thick-horz-bands.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THICK_VERT_BANDS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThickVertBandsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.VIOLET.getIndex());
cellStyle.setFillPattern(FillPatternType.THICK_VERT_BANDS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
cellStyle.setFillPattern(FillPatternType.THICK_VERT_BANDS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thick-vert-bands.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THIN_BACKWARD_DIAG fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThinBackwardDiagExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.LIGHT_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_BACKWARD_DIAG);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_BACKWARD_DIAG);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thin-backward-diag.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THIN_FORWARD_DIAG fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThinForwardDiagExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.SKY_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_FORWARD_DIAG);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_FORWARD_DIAG);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thin-forward-diag.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THIN_HORZ_BANDS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThinHorzBandsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.PALE_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_HORZ_BANDS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_HORZ_BANDS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thin-horz-bands.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Example for background and foreground color with FillPatternType.THIN_VERT_BANDS fill pattern
package simplesolution.dev;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CellColorThinVertBandsExample {
public static void main(String... args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("example");
Row row = sheet.createRow(1);
row.setHeightInPoints(100);
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(3, 6000);
Cell cell = row.createCell(1);
cell.setCellValue("SimpleSolution.dev");
// Set up font
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up background color
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillBackgroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_VERT_BANDS);
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("SimpleSolution.dev");
// Set up font
font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
font.setBold(true);
// set up foreground color
cellStyle = workbook.createCellStyle();
cellStyle.setFont(font);
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
cellStyle.setFillPattern(FillPatternType.THIN_VERT_BANDS);
cell.setCellStyle(cellStyle);
try(OutputStream outputStream = new FileOutputStream("sample-thin-vert-bands.xlsx")) {
workbook.write(outputStream);
} catch(IOException ex) {
ex.printStackTrace();
}
}
}
Happy Coding 😊
Related Articles
Apache POI Excel Cell Border and Border Color
Apache POI Vertical and Horizontal Excel Cell Alignment
Apache POI Merging Excel Cells
Java Create Excel File .xlsx using Apache POI
Java Read Excel File using Apache POI
Java Read Excel Workbook from File using Apache POI
Java Read Excel Workbook from InputStream using Apache POI
Java Read Password Protected Excel File using Apache POI
Java How to Iterate over Sheets Rows and Cells of Excel file using Apache POI
Java Add Rows to Existing Excel File using Apache POI
Java Add Sheet to Existing Excel File using Apache POI