title: Apache POI: Elegant way to set borders to a column which contains different styles
body: <p>I'm dynamically creating a new xlsx file using apache-poi. Any column can contain different value types (Numbers, Strings, Booleans, ...). While inserting data into the poi document I set CellStyles depending on the type of the data:</p>
  
  <pre><code>public final XSSFCellStyle cellStyleString;
  public final XSSFCellStyle cellStyleNumber;
  public final XSSFCellStyle cellStyleDate;
  public final XSSFCellStyle cellStyleHeader;
  </code></pre>
  
  <p>This is how my header rows look like:</p>
  
  <pre><code>|   |   |   | Shared Header |
  | H1| H2| H3|SH1|SH2|SH3|SH4|
  </code></pre>
  
  <p>There are "simple" headers and "shared headers" which contain "sub headers". Shared headers reside in merged cells. </p>
  
  <p>No I'd like to have a left border at column <code>SH1</code> and a right border at column <code>SH4</code> to emphasize the grouping. But as any column could contain a mix of all cellstyles, it seems like I have to create CellStyles like </p>
  
  <pre><code>public final XSSFCellStyle cellStyleString;
  public final XSSFCellStyle cellStyleStringBorderLeft;
  public final XSSFCellStyle cellStyleStringBorderRight;
  //and so on for the other styles...
  </code></pre>
  
  <p>Furthermore there could be nested shared header which I'd like to distinguish by different border sizes. So I'd need something like </p>
  
  <pre><code>public final XSSFCellStyle cellStyleString;
  public final XSSFCellStyle cellStyleStringBorderLeftThickLine;
  public final XSSFCellStyle cellStyleStringBorderRightThickLine;
  public final XSSFCellStyle cellStyleStringBorderLeftThinLine;
  public final XSSFCellStyle cellStyleStringBorderRightThinLine;
  //and so on for the other styles...
  </code></pre>
  
  <p>Is there a more elegant way to set the borders of the column regardless of the already existing style?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Although I prefer a clean and simple approach and that for to minimize the number of created styles, I stumpled upon the <a href="https://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFOptimiser.html" rel="nofollow">HSSFOptimiser</a> which removes duplicate cellstyles. I didn't know about that class. Even though I prefer avoiding this utility it fits the problem and deserves to be mentioned here.</p>