颜色Color是单元格的基本样式,单元格默认颜色为黑色
- 单元格边框颜色 - 单元格填充色 - 单元格字体颜色
POI中预定义了56种颜色,索引从0x8 - 0x40, 索引0x40定义一种默认颜色 - 黑色
org.apache.poi.ss.usermodel.IndexedColors
org.apache.poi.hssf.util.HSSFColor$HSSFColorPredefined
// 设置填充色
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
cellStyle.setFillForegroundColor(HSSFColorPredefined.BLUE.getIndex());
// 设置字体颜色
font.setColor(IndexedColors.WHITE.getIndex());
font.setColor(IndexedColors.WHITE.getIndex());
当56预定义颜色无法满足需要时,可以自定义颜色,HSSFColor和XSSFColor自定义颜色方式完全不同
1. 根据HSSFWorkbook获取自定义调色面板HSSFPalette
2. 用自定义颜色覆盖索引[0x8, 0x40)
3. 使用覆盖的索引,即自定义的颜色
// 获取工作簿的调色面板
HSSFPalette palette = hssfWorkbook.getCustomPalette();
// 将调色面板中蓝色索引替换为红色
palette.setColorAtIndex(HSSFColorPredefined.BLUE.getIndex(), (byte) 255, (byte) 0, (byte) 0);
// 会出现红色字体
font.setColor(HSSFColorPredefined.BLUE.getIndex());
1. 根据一个java.awt.Color实例或RGB字节数组创建一个XSSFColor实例color
2. 直接传递实例color对象
XSSFColor color = new XSSFColor(new java.awt.Color(255, 0, 0));
xSSFCellStyle.setFillForegroundColor(color);
xSSFFont.setColor(color);
package hssf.sheet.color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
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.Font;
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;
/** * 使用默认调色面板 * */
public class ExportHSSFColor {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\test.xls");
if (file.exists()) {
file.delete();
}
BufferedOutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xls"));
exportExcel(out);
} finally {
out.close();
}
}
/** * @param out * @throws IOException */
private static void exportExcel(BufferedOutputStream out) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet();
sheet.setColumnWidth(0, 20*256);
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell((short) 0);
cell.setCellValue("调色板");
/*****************************自定义颜色**************************************************/
// 获取工作簿的调色面板
HSSFPalette palette = workbook.getCustomPalette();
// 将调色面板中蓝色索引替换为红色
palette.setColorAtIndex(HSSFColorPredefined.BLUE.getIndex(), (byte) 255, (byte) 0, (byte) 0);
// 将白色索引替换为黑色
palette.setColorAtIndex(HSSFColorPredefined.WHITE.getIndex(), (byte) 0, (byte) 0, (byte) 0);
/**************************************************************************************/
CellStyle style = workbook.createCellStyle();
// 填充色
style.setFillForegroundColor(HSSFColorPredefined.BLUE.getIndex());
// style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 字体颜色
Font font = workbook.createFont();
font.setColor(HSSFColorPredefined.WHITE.getIndex());
style.setFont(font);
cell.setCellStyle(style);
workbook.write(out);
}
}
package hssf.sheet.color;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
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.Font;
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.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExportXSSFColor {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\test.xlsx");
if (file.exists()) {
file.delete();
}
BufferedOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xlsx");
out = new BufferedOutputStream(fileOutputStream);
exportExcel(out);
} finally {
out.close();
}
}
/** * @param out * @throws IOException */
private static void exportExcel(BufferedOutputStream out) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
sheet.setColumnWidth(0, 20*256);
Row row = sheet.createRow((short) 0);
Cell cell = row.createCell((short) 0);
cell.setCellValue("调色板");
// 创建一个单元格样式
XSSFCellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);
/*****************************使用默认颜色**************************************************/
// 填充色
// style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillForegroundColor(HSSFColorPredefined.BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 字体颜色
Font font = workbook.createFont();
font.setColor(IndexedColors.WHITE.getIndex());
style.setFont(font);
/**************************************************************************************/
/*****************************自定义颜色**************************************************/
XSSFColor color = new XSSFColor(new java.awt.Color(255, 0, 0));
style.setFillForegroundColor(color);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
XSSFColor color1 = new XSSFColor(new java.awt.Color(0, 0, 0));
// 字体颜色
XSSFFont font1 = workbook.createFont();
font1.setColor(color1);
style.setFont(font1);
/**************************************************************************************/
workbook.write(out);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/lipinganq/article/details/78132152
内容来源于网络,如有侵权,请联系作者删除!