How can add different colour for each worksheet in excel using Apache POI

与世无争的帅哥 提交于 2019-12-20 05:41:49

问题


When you have multiple worksheets in a workbook, you usually click on a sheet's name at the bottom of the page to view it. My question is then to know, if that "button" containing the sheet's name can take a color such as blue, green and so on.

    XSSFWorkbook wb = new XSSFWorkbook(); 
    XSSFSheet sheet = wb.createSheet(); 
    CTColor color = CTColor.Factory.newInstance(); 
    color.setIndexed(IndexedColors.RED.getIndex()); 
    sheet.getCTWorksheet().getSheetPr().setTabColor(color); 

I have tried above but no use

        try (XSSFWorkbook wb = new XSSFWorkbook()) {

        XSSFSheet sheet1 = wb.createSheet("1e");



        XSSFSheet sheet = wb.createSheet("1econtent");
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 15);
        font.setColor(IndexedColors.WHITE.getIndex());

        How can I add different colours sheet and sheet1 in workbook

回答1:


Please do it as follows:

sheet.setTabColor(IndexedColors.RED.getIndex());

Please check https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/IndexedColors.html for more details.

EDIT:

Please do it as follows with the latest version of Apache POI:

byte[] rgb=DefaultIndexedColorMap.getDefaultRGB(IndexedColors.RED.getIndex());
sheet.setTabColor(new XSSFColor(rgb,null));


来源:https://stackoverflow.com/questions/58319267/how-can-add-different-colour-for-each-worksheet-in-excel-using-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!