POI显示Excel表格具体配置颜色

public static void main(String[] args)   {
    short colorNum = 2;//每行显示的颜色的个数
    short width1 = 2000;//颜色序宽度
    short width = 6000;//颜色名栏位的宽度
    XSSFWorkbook workbook = new XSSFWorkbook();
    CellStyle thStyle = workbook.createCellStyle();

    XSSFSheet sheet = workbook.createSheet("IndexedColors遍历");
    Row row =null;
    Cell cell;
    short i,j,index=0;
    String text="";
    for(i=0;i<colorNum;i++){
        sheet.setColumnWidth((short) i*3 , width1 );
        sheet.setColumnWidth((short) i*3 + 1 , width );
    }
    for (IndexedColors c : IndexedColors.values()){
        i=(short) (index/colorNum);
        j=(short) (index%colorNum);
        thStyle = workbook.createCellStyle();
        thStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  //设置前景填充样式
        thStyle.setFillForegroundColor(c.getIndex());
        row = j==0?sheet.createRow(i):row;//如果到了换行的时候row new一下 否则就当什么都没有发生
        row.createCell(3*j).setCellValue("  "+(c.getIndex() + 1000 + "").substring(1,4));
        row.createCell(3*j+1).setCellValue(c+"");
        row.createCell(3*j+2).setCellStyle(thStyle);
        index++;//计数
    }

    sheet = workbook.createSheet("30X"+colorNum+"颜色遍历");

    for( i=0;i<30;i++ ){
        row = sheet.createRow(i);
        for(j=0;j<colorNum;j++){
            thStyle = workbook.createCellStyle();
            thStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);  //设置前景填充样式
            int size =(colorNum*i + j);
            thStyle.setFillForegroundColor((short)size);
            row.createCell(2*j).setCellValue((short)size);
            row.createCell(2*j+1).setCellStyle(thStyle);
        }
    }

    FileOutputStream fileOut = null;
    try {
        fileOut = new FileOutputStream("D:/POI颜色大全.xlsx");
        workbook.write(fileOut);
        fileOut.close();
    } catch (Exception e) {
        System.out.println("保存xlsx的时候发生的异常");
        e.printStackTrace();
    }
}

  

原文地址:https://www.cnblogs.com/lewisat/p/15632795.html