It works fine but I can produce several Excel file with lots of different styles in each file. Rather than create a style for each cell, I told myself that I would pool the code in an abstract class. But then, it poses a big problem. Imagine that I wanted a gray cell as the one we just created.So I create my style in my abstract class. Now I want a gray cell with centered content and another with centered content and borders . I will not create a style for each possibles combinations. What would have been nice is that you can add multiple styles in a cell.

Here is my solution that allows you to merge several styles together.

/**
* Merges multiple CellStyle each other.
* If two CellStyle have a style in common.
* The following takes over the former.
* @Param String A list of CellStyle to merge made
* from a list contained in a HashMap.
* @return CellStyle The CellStyle containing all styles..
*/
protected CellStyle mergeCellStyle(String... cellStyles) {
// Vacuum style that will serve as a basis for comparison.
CellStyle defaultStyle = wb.createCellStyle();
// Base CellStyle that will serve to aggregate styles to merge.
CellStyle currentStyle = wb.createCellStyle();
try {
// We loop the CellStyles merge
for(String cellStyleString:cellStyles) {
// Is recovered CellStyle to merge with the PREV (s)
CellStyle cellStyle = styles.get(cellStyleString)
// We retrieves the list of methods of the CellStyle object
// that we want to take into consideration when merging.
Set<String> keys = mergeMethods.keySet();
// We loops through the list of these methods.
for(String key : keys)
Method method = cellStyle.getClass().getMethod(key);
Class<?> returnType = method.getReturnType();
Object result = method.invoke(cellStyle);
Object resultBase = methode.invoke(currentStyle);
Object defaultStyle = method.invoke(defaultStyle);
/* We compare if there is a change from the current style.
* and if this change does not match the base style
* to avoid backtracking.
*/
if(result.hashCode() != resultBase.hashCode()
&amp;&amp; result.hashCode() != resultDefault.hashCode()) {
// If there is a change we modified the current style.
currentStyle.getClass().getMethod(mergeMethods.get(key), returnType)
.invoke(currentStyle, result);
}
}
}
}
// The merged style is returned.
return currentStyle;
// Catch clause
}

Note here that the hashcode is used for comparison. Indeed, as we use reflexivity, the result of the invocation of a method is of type Object. So the difference with the != will not give a relevant result. And as the types used by POI to change styles are primitive, a difference made with " ". Equals() will not give the desired result either.

For those who ask why I do not use recursion to retrieve methods dynamically? The answer is that the MaClasse.getMethods () method returns the set of public, private ,_ protected methods of the class. We would therefore have to filter methods to exclude. So I preferred to include methods that i want to used.