0

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.

J.P
  • 143
  • 1
  • 9
  • This is a problem of the lib or the problem of excel. I have this problem, too, and I found no solution. – IQV Jan 30 '17 at 09:05
  • For those interested, I corrected the issue by adding a specific value after the autoSize : onglet.autoSizeColumn(i, true); onglet.setColumnWidth(i, onglet.getColumnWidth(i) + 3*256); – J.P Jan 30 '17 at 10:50

1 Answers1

0

For those interested, I corrected the issue by adding a specific value after the autoSize :

onglet.autoSizeColumn(i, true); 
onglet.setColumnWidth(i, onglet.getColumnWidth(i) + 3*256);

256 represent the size of one character.

It's not perfect, but it works for most cases for me.

J.P
  • 143
  • 1
  • 9