I made the following change to the code and it worked.
titleLenght = 2;
cell1.setCellValue(excelTitle);
boolean headerStyle = true;
for (int j = 0; j < list.size(); j++) {
Row row = mySheet.createRow(j+2+titleLength);
List<String> l2= list.get(j);
for(int k=0; k<l2.size(); k++)
{
Cell cell = row.createCell(k);
cell.setCellValue(l2.get(k));
if(headerStyle){
**cell.setCellStyle(headingCellStyle);
int origColWidth = mySheet.getColumnWidth(k);
mySheet.autoSizeColumn(k);
// Making sure col width is not going less than the header width
if (origColWidth > mySheet.getColumnWidth(k)) {
mySheet.setColumnWidth(k, origColWidth);
}**
}else{
cell.setCellStyle(dataCellStyle);
}
}
headerStyle = false;
}
// Set title style element at last so the first column will not take
// width of title field
cell1.setCellStyle(titleCellStyle);
In my previous code, for the header, autoSizeColumn(K) was returning zero since there is no style associated to it. To be specific no font was assigned since I set used autoSizeColumn(k) before setting style. Hence the width was getting returned as zero. This made all the columns getting hidden.
Now I have added a condition in such a way that width wont become zero at any case. I have moved the title styling to end so that It don't affect the width of the first header column.
Conclusion:
- Create Cell
- Set value and Set Style
- Set autoColumnSize or calculate column width