如何在apache POI中自动调整excel中的列

时间:2016-10-19 12:04:22

标签: java excel apache-poi

我正在用apache poi创建一个excel文件,生成excel但是我无法根据单元格值调整列我发布代码到目前为止我已经完成了

这就是我在excel中创建标题的方法

HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        sheet.protectSheet("password");
        sheet.autoSizeColumn(15);
        HSSFFont hSSFFont = wb.createFont();
        hSSFFont.setFontName(HSSFFont.FONT_ARIAL);
        hSSFFont.setFontHeightInPoints((short) 8);

        CellStyle style = wb.createCellStyle();
        /* cell style for locking */
        CellStyle lockedCellStyle = wb.createCellStyle();
        lockedCellStyle.setLocked(true);

        HSSFRow row = null;

        HSSFCell cell = null;

        row = sheet.createRow(0);
        int headercolumnNo = 0;



        //1st Column Header for Indicator
        cell = row.createCell(headercolumnNo);
        cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(0)));
        style.setWrapText(true);
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFont(hSSFFont);
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(style);
        headercolumnNo = 1;

        cell = row.createCell(headercolumnNo);  //2nd Column Header for Firstname
        cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(1)));
        style.setWrapText(true);
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFont(hSSFFont);
        cell.setCellStyle(style);
        headercolumnNo = headercolumnNo + 1;

        cell = row.createCell(headercolumnNo);  //2nd Column Header for Firstname
        cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(2)));
        style.setWrapText(true);
        style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
        style.setFont(hSSFFont);
        cell.setCellStyle(style);
        headercolumnNo = headercolumnNo + 1;

and this is how i have  populated the values in that excel file 

for(CarrierActiveUser carrierActiveUser : listOfCarrierUser){

            int columnNo = 0;
            row = sheet.createRow(j + 1);

            cell = row.createCell(columnNo);
            if(null != carrierActiveUser.getFistName()){

                cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getFistName()));
                 lockedCellStyle.setFont(hSSFFont);
                 cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                cell.setCellStyle(lockedCellStyle);
            }

            columnNo = columnNo + 1;
            cell = row.createCell(columnNo);
            if(null != carrierActiveUser.getLastName()){

                cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getLastName()));
                   lockedCellStyle.setFont(hSSFFont);
                cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                cell.setCellStyle(lockedCellStyle);
            }

            columnNo = columnNo + 1;
            cell = row.createCell(columnNo);
            if(null != carrierActiveUser.getLastName()){

                cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getEmailId()));
                lockedCellStyle.setFont(hSSFFont);
                cell.setCellStyle(lockedCellStyle);

            }else{
                cell.setCellValue(new HSSFRichTextString(" "));
                cell.setCellStyle(lockedCellStyle);
            }

请有人帮我调整列,我是apache poi的新手

Current output

1 个答案:

答案 0 :(得分:6)

您可以使用HSSFSheet.autoSizeColumn(columnNumber)方法完美地对齐列。

此方法调整列宽以适合内容,请阅读doc

设置所有列的所有单元格值后,可以使用此方法,在当前代码中,在for循环后调用此方法。

示例代码

sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);

注意 - 您必须对要对齐的所有列单独执行此操作,并且应在将数据填充到Excel后对sheet.autoSizeColumn(columnNumber)进行调用。在填充数据之前调用不会产生任何影响。