Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java POI自动调整columun的大小_Java_Excel_Apache Poi_Autosize - Fatal编程技术网

Java POI自动调整columun的大小

Java POI自动调整columun的大小,java,excel,apache-poi,autosize,Java,Excel,Apache Poi,Autosize,对于我的项目,我使用java和POI库创建了一个excel文件。 我有几张纸,还有很多不同的信息。对于一些我可以使用模板,只插入数据 但对于其他一些数据,我不知道我将获得的信息的长度 我需要excel很好地显示,因此我使用不同的样式,我的问题是自动调整大小: /** * Remplit les cellules d'un onglet * @param nomOnglet Le nom de l'onglet à remplir * @param cellules Les vale

对于我的项目,我使用java和POI库创建了一个excel文件。 我有几张纸,还有很多不同的信息。对于一些我可以使用模板,只插入数据

但对于其他一些数据,我不知道我将获得的信息的长度

我需要excel很好地显示,因此我使用不同的样式,我的问题是自动调整大小:

    /**
 * 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);
    }
}
/**
*雷姆普利特小赛璐珞酒店
*@param nomOnglet Le nom de l'longletáremplir
*她是一个小女孩
*@技术异常
*/
私有void remplirOnglet(HSSF工作簿类、字符串nomOnglet、列表单元)引发TechniqueException{
HSSFSheet onglet=类别欧元getSheet(nomOnglet);
if(onglet==null)
onglet=classeur.createSheet(nomOnglet);
int colMax=0;
用于(单元导出单元:单元){
int idLigne=cellle.getRow()-1;
int idColonne=cellle.getColumn()-1;
colMax=Math.max(colMax,idColonne);
HSSFRow-ligne=onglet.getRow(idLigne);
如果(ligne==null)
ligne=onglet.createRow(idLigne);
HSSFCell poiCell=ligne.getCell(idColonne);
如果(poiCell==null)
poiCell=ligne.createCell(idColonne);
poiCell.setCellType(HSSFCell.CELL\U TYPE\U字符串);
setCellValue(cellle.getValeur());
if(Cellle.isStyled()){
苹果风格(蜂窝状、波伊塞尔状);
if(cellle.getStyleType().equals(cellleexport.CELL\u STYLE\u TYPE.TITRE\u BLOC))
熔结硅藻土(onglet,cellule,2);
}
}

对于(int i=0;i对于那些感兴趣的人,我通过在自动调整大小后添加一个特定值来纠正这个问题:

onglet.autoSizeColumn(i, true); 
onglet.setColumnWidth(i, onglet.getColumnWidth(i) + 3*256);
256表示一个字符的大小


它并不完美,但在大多数情况下对我都有效。

这是lib或excel的问题。我也有这个问题,但我没有找到解决方案。对于那些感兴趣的人,我通过在autoSize之后添加一个特定值来纠正这个问题:onglet.autoSizeColumn(I,true);onglet.setColumnWidth(I,onglet.getColumnWidth(I)+3*256);