我想将自定义颜色设置为单元格的背景.
我使用HSSFWorkbook(不能使用其他任何东西).
HSSFPalette palette = aWorkBook.getCustomPalette();
Color col = new Color(backgroundColor);
HSSFColor myColor = palette.addColor((byte) 10, (byte) 11, (byte) 12);
Run Code Online (Sandbox Code Playgroud)
我收到此错误: java.lang.RuntimeException: Could not find free color index
我尝试使用setFillForegroundColor和setFillBackgroundColor来更改excel文件的单元格颜色.
但是,我失败了,我真的不知道问题是什么.我用Google搜索了好几个小时,仍然无法找到正确的方法来设置颜色.
以下是我写的代码:
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");
// set the color of the cell
XSSFCellStyle style = wb.createCellStyle();
XSSFColor myColor = new XSSFColor(Color.BLUE);
style.setFillForegroundColor(myColor);
style.setFillBackgroundColor(myColor);
cell.setCellStyle(style); // this command seems to fail …Run Code Online (Sandbox Code Playgroud)