Apache poi autosizecolumn is not working

831 views Asked by At

i want to write a list of object to an excel file.But autoSizeColumn is not working.My code is below :

List<ReportTmPerHour> reportTm = reportTm();

        Iterator<ReportTmPerHour> iterator = reportTm.iterator();
        String fileName="C:\\Users\\kkk\\Desktop\\mmm_ccc.xlsx";
        Workbook workbook = null;
        if(fileName.endsWith("xlsx")){
            workbook = new XSSFWorkbook();
        }else if(fileName.endsWith("xls")){
            workbook = new HSSFWorkbook();
        }else{
            try {
                throw new Exception("invalid file name, should be xls or xlsx");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Sheet sheet = workbook.createSheet("TM");


        int rowIndex = 0;
        createHeadersForTm(sheet);

        while(iterator.hasNext()){
            ReportTmPerHour reportTmPerHour = iterator.next();
            Row row = sheet.createRow(++rowIndex);
            Cell cell = row.createCell(0);
            cell.setCellValue(reportTmPerHour.getIsemriNo());
            cell.setCellStyle(arg0);
            Cell cell10 = row.createCell(1);
            cell10.setCellValue(reportTmPerHour.getSube());
            Cell cell11=row.createCell(2);
            cell11.setCellValue(reportTmPerHour.getAob());
            Cell cell12=row.createCell(3);
            cell12.setCellValue(reportTmPerHour.getKesintiBildirim());
            Cell cell13=row.createCell(4);
            cell13.setCellValue(reportTmPerHour.getAciklama());
            Cell cell14=row.createCell(5);
            cell14.setCellValue(reportTmPerHour.getEtkilenenMahalleler());
            Cell cell15=row.createCell(6);
            if (reportTmPerHour.getBaslamaZamani()!=null) {
                cell15.setCellValue(reportTmPerHour.getBaslamaZamani());
            }
            Cell cell16=row.createCell(7);
            cell16.setCellValue(reportTmPerHour.getTahminiBitisZamani());
            Cell cell17=row.createCell(8);
            cell17.setCellValue(reportTmPerHour.getTmName());
            Cell cell18=row.createCell(9);
            cell18.setCellValue(reportTmPerHour.getEtkilenensayi());
        }
        columnAutoSize(sheet);

        //lets write the excel data to file now
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(new File(fileName));
            workbook.write(fos);
            fos.close();
            System.out.println(fileName + " written successfully");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

It does not adjust the columns according to the biggest column size.I use the last version of poiI have seen lots of questions like this.I adjust the size of columns after filling the cells.But this is not working also.Thanx in advance

EDIT My columnAutoSize(sheet) is below :

private void columnAutoSize(Sheet sheet) {
        for(int columnPosition = 1; columnPosition< 9; columnPosition++) {
            sheet.autoSizeColumn(columnPosition);
       }
    }
0

There are 0 answers