List of usage examples for org.apache.poi.xssf.usermodel XSSFColor equals
@Override
public boolean equals(Object o)
From source file:com.tutorial.excelreadwrite.excelFunctions.java
public void convertColor(int r, int g, int b, int numColors) { //Get the userDefinedColor and set the style userDefinedColor = new XSSFColor(new java.awt.Color(r, g, b)); XSSFCellStyle userDefinedCS = workbook.createCellStyle(); userDefinedCS.setFillForegroundColor(userDefinedColor); userDefinedCS.setFillPattern(CellStyle.SOLID_FOREGROUND); //Create an arrayList and add foreground colors that will be converted and then remove them List<XSSFColor> listOfColors = new ArrayList(); for (int i = 0; i < numColors; ++i) { try { //First row of excel document will be reserved for obtaining the colors of the foreground used listOfColors.add(sheet.getRow(0).getCell(i).getCellStyle().getFillForegroundXSSFColor()); sheet.getRow(0).getCell(i).setCellStyle(null); } catch (NullPointerException ex) { throw new NullPointerException("Either incorrect # colors entered OR colors NOT SET."); }// w w w . j a v a2 s .c o m } //Set-up rowIterator and get Row Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); //Set-up cellIterator and get Cell Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); //Null-Check for Cell if (cell != null) { //Get the Cell Style, Null-Check for Cell Style XSSFCellStyle currCellStyle = (XSSFCellStyle) cell.getCellStyle(); if (currCellStyle != null) { //Get the fillForeground color XSSFColor fgColor = currCellStyle.getFillForegroundXSSFColor(); //cycle through ArrayList and compare if any of the colors listed matches for (XSSFColor col : listOfColors) { if (col.equals(fgColor)) { cell.setCellStyle(userDefinedCS); } } } } } } }
From source file:com.tutorial.excelreadwrite.excelFunctions.java
public void markHorizontal(int spacesApart){ //Set-up rowIterator and get Row Iterator<Row> rowIterator = sheet.iterator(); while(rowIterator.hasNext()){ Row row = rowIterator.next(); /*from ww w . j av a 2 s . c o m*/ //Set-up cellIterator and get Cell Iterator<Cell> cellIterator = row.cellIterator(); while(cellIterator.hasNext()){ Cell cell = cellIterator.next(); //Obtains the Cell Style XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle(); //Checks to see if the Cell Style is null; if null, go to next cell if(cellStyle != null){ //Checks to see what color is the cell's color XSSFColor cellColor = cellStyle.getFillForegroundXSSFColor(); //Checks to see if color is null; if not compare to accept only editted or userDefined cells if(cellColor != null){ //Checks if current cell is userDefined or editted //If it is not, then go to the next cell if(cellColor.equals(mark.getFillForegroundXSSFColor()) || cellColor.equals(userDefinedColor)){ //Set boolean isCellMarked to false before proceeding isCellMarked = false; //Define Cell to be (spacesApart+1) away //So if x = current cell then the cell that is 5 spacesApart = // [x][][][][][][x] Cell cellMark = sheet.getRow(cell.getRowIndex()).getCell(cell.getColumnIndex() + spacesApart + 1); //Checks to see if cell is null; if present, get its Cell Style if(cellMark != null){ XSSFCellStyle cellMarkStyle = (XSSFCellStyle)cellMark.getCellStyle(); //Checks to see if the style is null; if present, get its color if(cellMarkStyle != null){ XSSFColor cellMarkColor = cellMarkStyle.getFillForegroundXSSFColor(); //Checks to see if the color is null; if present, compare colors if(cellMarkColor != null){ if(cellMarkColor.equals(userDefinedColor)){ isCellMarked = true; } } } } /* ** CHECK#1: 'isCellMarked' ** If isCellMarked is marked true, start iterating through the ** cells in between and check if null or not userDefinedStyle */ if(isCellMarked == true){ for(int i = 1; i <= spacesApart; ++i){ Cell isNull = sheet.getRow(cell.getRowIndex()).getCell(cell.getColumnIndex()+i); //Checks to see if the cell is null; if color is present, set isCellMarked to false if(isNull != null){ XSSFCellStyle cellCheckIfNullCellStyle = (XSSFCellStyle)isNull.getCellStyle(); if(cellCheckIfNullCellStyle != null){ XSSFColor cellCheckIfNullColor = cellCheckIfNullCellStyle.getFillForegroundXSSFColor(); if(cellCheckIfNullColor != null){ if(cellCheckIfNullColor.equals(userDefinedColor)){ isCellMarked = false; break; } } } } } } /* ** CHECK#2: 'isCellMarked2' ** If isCellMarked remains as true, set the two cell's style */ if(isCellMarked == true){ cell.setCellStyle(mark); cellMark.setCellStyle(mark); } } } } } } }