Quantcast
Channel: Java Programming Forum - Learn Java Programming - Apache POI
Viewing all articles
Browse latest Browse all 120

Problem getting cell Color (XSSF)

$
0
0
Hi all,
I would like to read de backgroung color of cells, but i am always getting null while trying my code.

Surfing the net people always use HSF for this task but i need to use XSSF instead.

Code:

public class ExcelSheetReader
{
        /**
        * This method is used to read the data's from an excel file.
        * @param fileName - Name of the excel file.
        */
        private void readExcelFile(String fileName)
        {
                /**
                * Create a new instance for cellDataList
                */
                List cellDataList = new ArrayList();
                try
                {
                        /**
                        * Create a new instance for FileInputStream class
                        */
                        FileInputStream fileInputStream = new FileInputStream(fileName);

                        /**
                        * Create a new instance for HSSFWorkBook Class
                        */

                        XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
                        XSSFSheet xssfSheet = workBook.getSheetAt(0);
                        /**
                        * Iterate the rows and cells of the spreadsheet
                        * to get all the datas.
                        */
                        Iterator rowIterator = xssfSheet.rowIterator();
                        rowIterator.next();
                        rowIterator.next();
                        rowIterator.next();
                        while (rowIterator.hasNext())
                        {
                                XSSFRow xssfRow = (XSSFRow) rowIterator.next();
                                Iterator iterator = xssfRow.cellIterator();
                                while (iterator.hasNext())
                                {
                                        XSSFCell hssfCell =  (XSSFCell) iterator.next();
                                        XSSFColor  cellColor = (hssfCell.getCellStyle().getFillBackgroundColorColor());
                                        XSSFCellStyle style = hssfCell.getCellStyle();
                                       
                                        System.out.println ("****");
                                        System.out.println (hssfCell.toString());
                                        //XSSFColor xsfColor = style.getFillBackgroundColorColor();
                                        XSSFColor xsfColor = style.getFillBackgroundXSSFColor();
                                        if ( (xsfColor) != null ){
                                                System.out.println (cellColor.toString());
                                        }
                                        System.out.println ("****");

                                }

                        }
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }

        }

Some help would be appreciated!
Thanks in advance

Viewing all articles
Browse latest Browse all 120

Trending Articles