I tried using setFillForegroundColor and setFillBackgroundColor to change the cell color of the excel file.
However, I failed, and I really did not know what the problem was. I worked a lot on the Internet and could not find the right way to set the color.
Below is the code I'm writing:
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestColor {
public static void main(String[] args) {
File f = new File("test.xlsx");
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("no blue");
XSSFCellStyle style = wb.createCellStyle();
XSSFColor myColor = new XSSFColor(Color.BLUE);
style.setFillForegroundColor(myColor);
style.setFillBackgroundColor(myColor);
cell.setCellStyle(style);
try {
FileOutputStream fos = new FileOutputStream(f);
wb.write(fos);
wb.close();
fos.flush();
fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
And this is the final result.

How to set cell color to blue?
I am using poibbin-3.12-20150511.zip from https://poi.apache.org/download.html
source
share