Link to home
Start Free TrialLog in
Avatar of tia_kamakshi
tia_kamakshiFlag for United Arab Emirates

asked on

Setting background colour of excel file using HSSFCellStyle

Hi,

I am creating Excel file using org.apache.poi api

Here is the URL

http://www.javaworld.com/javaworld/jw-03-2004/jw-0322-poi.html?page=1

I wanted my data to display in the spreadsheet exactly like in the above example

I am able to create Excel file and can see font colour changed to Blue and black

But I am not able to set background colour of the cell.

Can anyone please help me in fixing the background colour exactly same as in the example

Please see my code in the code snippet text area

I wanted to change the colour of cell alteranetvely as in example

Please guide

Many Thanks
public void generateCustomerListExcel(ArrayList<Customer> arr, String strFileName, NewsletterBO newsletterBO)
            throws Exception
    {
    
        Collection client = new HashSet();
        
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Report");
        
 
        HSSFCellStyle columnHeaderStyle = wb.createCellStyle();
        columnHeaderStyle.setFillBackgroundColor(
        HSSFColor.BLUE_GREY.index);
 
        
 
        HSSFFont font = wb.createFont();        
        font.setColor(HSSFColor.BLACK.index);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
 
        HSSFFont headerFont = wb.createFont();
        headerFont.setColor(HSSFColor.BLUE.index);
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 
        // Create the style
        HSSFCellStyle cellStyle= wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
 
        HSSFCellStyle cellHeaderStyle= wb.createCellStyle();
        cellHeaderStyle.setFont(headerFont);
        cellHeaderStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
 
 
        if(!arr.isEmpty())
        {
            Iterator<Customer> itr = arr.iterator();
 
            int rowNum =0;
 
            HSSFRow rowa = sheet.createRow(rowNum);
 
            HSSFCell cell0a=  rowa.createCell(0);
            cell0a.setCellValue("Title");
            cell0a.setCellStyle(cellHeaderStyle);
 
            HSSFCell cell1a=  rowa.createCell(1);
            cell1a.setCellValue("FirstName");
            cell1a.setCellStyle(cellHeaderStyle);
            //row.createCell(2).setCellValue(cust.FirstName);
 
            HSSFCell cell2a=  rowa.createCell(2);
            cell2a.setCellValue("LastName");
            cell2a.setCellStyle(cellHeaderStyle);
 
            HSSFCell cell3a=  rowa.createCell(3);
            cell3a.setCellValue("CompanyName");
            cell3a.setCellStyle(cellHeaderStyle);
 
            HSSFCell cell4a=  rowa.createCell(4);
            cell4a.setCellValue("EmailAddress");
            cell4a.setCellStyle(cellHeaderStyle);
 
               rowNum = 1;
            while(itr.hasNext())
            {
                Customer cust = (Customer)itr.next();
                client.add(new Customer(cust.Title, cust.FirstName, cust.LastName, cust.CompanyName, cust.EmailAddress));
 
                HSSFRow row = sheet.createRow(rowNum);
 
                HSSFCell cell1=  row.createCell(0);
                cell1.setCellValue(cust.Title);
                cell1.setCellStyle(cellStyle);
 
                HSSFCell cell2=  row.createCell(1);
                cell2.setCellValue(cust.FirstName);
                cell2.setCellStyle(cellStyle);
                //row.createCell(2).setCellValue(cust.FirstName);
 
                HSSFCell cell3=  row.createCell(2);
                cell3.setCellValue(cust.LastName);
                cell3.setCellStyle(cellStyle);
 
                HSSFCell cell4=  row.createCell(3);
                cell4.setCellValue(cust.CompanyName);
                cell4.setCellStyle(cellStyle);
 
                HSSFCell cell5=  row.createCell(4);
                cell5.setCellValue(cust.EmailAddress);
                cell5.setCellStyle(cellStyle);
 
                rowNum = rowNum+1;
            }
        }
 
        Map beans = new HashMap();
        beans.put("distributionlist", client);
 
        XLSTransformer transformer = new XLSTransformer();
        FileOutputStream fileOut = new FileOutputStream(newsletterBO.OutputAttachmentFolderPath+strFileName);
        
        wb.write(fileOut);
        fileOut.close();
    }

Open in new window

Avatar of contactkarthi
contactkarthi
Flag of United States of America image

you should set foreground color not back ground
also set style as solid HSSFCellStyle.SOLID_FOREGROUND

style.setFillForegroundColor((short) HSSFCellStyle.YOUR_COLOR);
Avatar of tia_kamakshi

ASKER

Many Thanks for your reply

If I wish light gray colour in the back ground of the cell with solid foreground

Can you please fix my cell code

HSSFCellStyle cellStyle= wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setFillBackgroundColor(HSSFColor.BLACK.index);
        cellStyle.setFillForegroundColor((short) HSSFCellStyle.YOUR_COLOR);

I am not able to find colour properties here like

        cellStyle.setFillForegroundColor((short) HSSFCellStyle.Grey); // NO grey colour

Many Thanks for your help


ASKER CERTIFIED SOLUTION
Avatar of contactkarthi
contactkarthi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your reply.

I have tried, but I cannot see any change in the background of the cell

I tried

HSSFCellStyle cellStyle= wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor((short) HSSFColor.TEAL.index);
        cellStyle.setFillBackgroundColor((short)HSSFColor.DARK_YELLOW.index);

Please see sample output attach file.

It doesn't work

Please guide
public void generateCustomerListExcel(ArrayList<Customer> arr, String strFileName, NewsletterBO newsletterBO)
            throws Exception
    {
        Collection client = new HashSet();
        
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Report");
        //create a style for the header cell
 
        HSSFCellStyle columnHeaderStyle = wb.createCellStyle();
        columnHeaderStyle.setFillBackgroundColor(
        HSSFColor.BLUE_GREY.index);
 
        /*HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        columnHeaderStyle.setFont(font);
        */
 
        HSSFFont font = wb.createFont();
        //font.setColor(HSSFFont.COLOR_RED);
        font.setColor(HSSFColor.BLACK.index);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
 
        HSSFFont headerFont = wb.createFont();
        headerFont.setColor(HSSFColor.BLUE.index);
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 
        // Create the style
        HSSFCellStyle cellStyle= wb.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor((short) HSSFColor.TEAL.index);
        cellStyle.setFillBackgroundColor((short)HSSFColor.DARK_YELLOW.index);
        //
        //cellStyle.setFillForegroundColor((short) HSSFCellStyle.);
        //cellStyle.setFillForegroundColor((short) HSSFColor.GREY_80_PERCENT.index);
        //cellStyle.setFillBackgroundColor(HSSFColor.BLACK.index);
 
        HSSFCellStyle cellHeaderStyle= wb.createCellStyle();
        cellHeaderStyle.setFont(headerFont);
        cellHeaderStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
 
 
        if(!arr.isEmpty())
        {
            Iterator<Customer> itr = arr.iterator();
 
            int rowNum =0;
 
            HSSFRow rowa = sheet.createRow(rowNum);
 
            HSSFCell cell0a=  rowa.createCell(0);
            cell0a.setCellValue("Title");
            cell0a.setCellStyle(cellHeaderStyle);
 
            HSSFCell cell1a=  rowa.createCell(1);
            cell1a.setCellValue("FirstName");
            cell1a.setCellStyle(cellHeaderStyle);
            //row.createCell(2).setCellValue(cust.FirstName);
 
            HSSFCell cell2a=  rowa.createCell(2);
            cell2a.setCellValue("LastName");
            cell2a.setCellStyle(cellHeaderStyle);
 
            HSSFCell cell3a=  rowa.createCell(3);
            cell3a.setCellValue("CompanyName");
            cell3a.setCellStyle(cellHeaderStyle);
 
            HSSFCell cell4a=  rowa.createCell(4);
            cell4a.setCellValue("EmailAddress");
            cell4a.setCellStyle(cellHeaderStyle);
 
               rowNum = 1;
            while(itr.hasNext())
            {
                Customer cust = (Customer)itr.next();
                client.add(new Customer(cust.Title, cust.FirstName, cust.LastName, cust.CompanyName, cust.EmailAddress));
 
                HSSFRow row = sheet.createRow(rowNum);
 
                HSSFCell cell1=  row.createCell(0);
                cell1.setCellValue(cust.Title);
                cell1.setCellStyle(cellStyle);
 
 
                HSSFCell cell2=  row.createCell(1);
                cell2.setCellValue(cust.FirstName);
                cell2.setCellStyle(cellStyle);
                //row.createCell(2).setCellValue(cust.FirstName);
 
                HSSFCell cell3=  row.createCell(2);
                cell3.setCellValue(cust.LastName);
                cell3.setCellStyle(cellStyle);
 
                HSSFCell cell4=  row.createCell(3);
                cell4.setCellValue(cust.CompanyName);
                cell4.setCellStyle(cellStyle);
 
                HSSFCell cell5=  row.createCell(4);
                cell5.setCellValue(cust.EmailAddress);
                cell5.setCellStyle(cellStyle);
 
                rowNum = rowNum+1;
            }
        }
 
        Map beans = new HashMap();
        beans.put("distributionlist", client);
 
        XLSTransformer transformer = new XLSTransformer();
        FileOutputStream fileOut = new FileOutputStream(newsletterBO.OutputAttachmentFolderPath+strFileName);
        //transformer.transformWorkbook(wb, beans);
        //wb.write(fileOut);
        wb.write(fileOut);
        fileOut.close();
    }

Open in new window

Sample.xls
Many Thanks. It works great