apache poi excel单元格颜色

本文概述

  • Apache POI Excel单元格颜色示例
Apache POI允许我们同时在背景和前景中设置单个单元格的颜色。为此,它提供了有助于设置单元格颜色的方法。
【apache poi excel单元格颜色】在下面的示例中,我们将创建两个单元格并将颜色分别填充到背景和前景。参见示例。
Apache POI Excel单元格颜色示例
package poiexample; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; 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; public class ColorExample { public static void main(String[] args) throws FileNotFoundException, IOException { try (OutputStream fileOut = new FileOutputStream("srcmini.xls")) { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet"); Row row = sheet.createRow(1); CellStyle style = wb.createCellStyle(); // Setting Background color style.setFillBackgroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(FillPatternType.BIG_SPOTS); Cell cell = row.createCell(1); cell.setCellValue("srcmini"); cell.setCellStyle(style); // Setting Foreground Color style = wb.createCellStyle(); style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); cell = row.createCell(2); cell.setCellValue("A Technical Portal"); cell.setCellStyle(style); wb.write(fileOut); }catch(Exception e) { System.out.println(e.getMessage()); } } }

输出:
apache poi excel单元格颜色

文章图片

    推荐阅读