本文概述
- Apache POI Excel单元格颜色示例
【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合并单元
- apache poi excel单元格边界
- apache poi excel单元格
- apache poi excel对齐单元格
- apache poi excel日期单元格
- apache poi excel工作簿
- apache poi excel文档处理
- apache poi安装
- apache poi的特性