Apache poi autosizecolumn不起作用

时间:2014-11-14 14:34:58

标签: excel apache-poi autosize

我想在excel文件中写一个list of object。但autoSizeColumn无效。我的代码如下:

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();
        }

它不会根据最大的列大小调整列。我使用poi的最后一个版本我看到了很多这样的问题。我在填充单元格后调整了列的大小。但这是也没有工作。提前谢谢

修改 我的columnAutoSize(sheet)位于以下位置:

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

0 个答案:

没有答案