For my project, with java and the POI lib I create an excel file. I've several sheets, and a lot of different information. For a few I can use a template and just insert the data.
But for some other data I don't know the lenght of the information I'm going to get.
I need the excel to present well so I use different styles, my problem is with the autosize :
/**
* Remplit les cellules d'un onglet
* @param nomOnglet Le nom de l'onglet à remplir
* @param cellules Les valeurs à insérer dans l'onglet
* @throws TechniqueException
*/
private void remplirOnglet(HSSFWorkbook classeur, String nomOnglet, List<CelluleExport> cellules) throws TechniqueException {
HSSFSheet onglet = classeur.getSheet(nomOnglet);
if(onglet == null)
onglet = classeur.createSheet(nomOnglet);
int colMax = 0;
for (CelluleExport cellule : cellules) {
int idLigne = cellule.getRow()-1;
int idColonne = cellule.getColumn()-1;
colMax = Math.max(colMax, idColonne);
HSSFRow ligne = onglet.getRow(idLigne);
if(ligne == null)
ligne = onglet.createRow(idLigne);
HSSFCell poiCell = ligne.getCell(idColonne);
if(poiCell == null)
poiCell = ligne.createCell(idColonne);
poiCell.setCellType(HSSFCell.CELL_TYPE_STRING);
poiCell.setCellValue(cellule.getValeur());
if(cellule.isStyled()) {
applyStyle(cellule, poiCell);
if(cellule.getStyleType().equals(CelluleExport.CELL_STYLE_TYPE.TITRE_BLOC))
fusionnerDroite(onglet, cellule, 2);
}
}
for(int i=0; i <= colMax; i++){
onglet.autoSizeColumn(i, true);
}
}
The Auosize works fine, most of the time. For a few cells, with bold or bigger font it doesn't work.
Does anyone know a way to avoid this problem ? I haven't find anything online, and I think it's a problem with the lib.