POI-Excel宽度自适应

public static void autoColumn(XSSFSheet sheet){
    int maxColumn = sheet.getRow(0).getPhysicalNumberOfCells();
    for(int i = 0; i < maxColumn; i++){
        sheet.autoSizeColumn(i);
    }
    for(int columnNum = 0; columnNum <= maxColumn; columnNum++){
        int columnWidth = sheet.getColumnWidth(columnNum) / 256;
        for(int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++){
            Row currentRow;
            if(sheet.getRow(rowNum) == null){
                currentRow = sheet.createRow(rowNum);
            }else{
                currentRow = sheet.getRow(rowNum);
            }

            if(currentRow.getCell(columnNum) != null){
                Cell currentCell = currentRow.getCell(columnNum);
                try {
                    int length = currentCell.toString().getBytes("GBK").length;
                    if(columnWidth < length + 1){
                        columnWidth = length + 8;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        sheet.setColumnWidth(columnNum, columnWidth * 256);
    }
}

 

上一篇:js中 给json对象添加属性和json数组添加元素


下一篇:jquery动态刷新select的值,后台传过来List,前台解析后填充到select的option中