Ask questions
Poi How to customize cell background color ... solve the problem
Example one: A concrete look at the note
import org.apache.poi.hssf.usermodel. *;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
/ **
* Created by Ay on 2016/4/29.
* /
public class PoiBackgroundColorTest {
public static void main (String [] args) throws Exception {
// Create a copy
HSSFWorkbook excel = new HSSFWorkbook ();
// Create the first sheet
HSSFSheet sheet = excel.createSheet ("My POI Journey");
// Create the first line
HSSFRow row = sheet.createRow ((short) 0);
// Create the first cell
HSSFCell cell = row.createCell ((short) 0);
// Set the value of the cell
cell.setCellValue ("Ay");
// Generate cell style
HSSFCellStyle style = excel.createCellStyle ();
// Set the background color
style.setFillForegroundColor (HSSFColor.LIME.index);
// solid fill foreground foreground color
style.setFillPattern (HSSFCellStyle.SOLID_FOREGROUND);
cell.setCellStyle (style);
// Write to hard disk by streaming
FileOutputStream out = new FileOutputStream ("D: /old_color.xls");
excel.write (out);
out.close ();
// ====== Here is the point, you will customize the style of the cell soon =============
cell.setCellValue ("Al");
// Get the palette color board
HSSFPalette palette = excel.getCustomPalette ();
// This is the key point, the specific is to put the previous color HSSFColor.LIME.index
// Replace with RGB (51,204,204) sapphire blue
// You can change to RGB (0,255,127)
palette.setColorAtIndex (HSSFColor.LIME.index, (byte) 0, (byte) 255, (byte) 127);
// ====== Here is the point, you will customize the style of the cell soon =============
out = new FileOutputStream ("D: /new_color.xls");
excel.write (out);
out.close ();
}