2

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
shivani yadav
  • 19
  • 1
  • 9

1 Answers1

2

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));
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • still getting error if I use as follows sheet.setTabColor(IndexedColors.RED.getIndex()) – shivani yadav Oct 10 '19 at 09:42
  • @Arvind Kumar Avinash: Whether this is correct depends on the `apache poi` version. Up to version `3.16` there was `public void setTabColor(int colorIndex)` and the `colorIndex` could be `IndexedColors.RED.getIndex()`. But that was removed in version `3.17`. Now in current versions there is only `public void setTabColor(XSSFColor color)`. So a `XSSFColor` must be used as parameter here. – Axel Richter Oct 10 '19 at 10:01
  • Error is : The method setTabColor(XSSFColor) in the type XSSFSheet is not applicable for the arguments (short) – shivani yadav Oct 10 '19 at 10:06
  • even I tried like sheet.setTabColor(0); no use again same error as specified – shivani yadav Oct 10 '19 at 10:08
  • Can anyone help me out – shivani yadav Oct 10 '19 at 10:11
  • Thank you Arvind Kumar Avinash updated answer is working fine – shivani yadav Oct 10 '19 at 11:15