如何在Apache POI中设置固定列宽

时间:2017-02-07 10:14:20

标签: java apache-poi

如何在Apache POI中设置固定列宽。 我想把我的第一列固定宽度。

我试过过sheet.setColumnWidth(0,1000);             cellStyle.setWrapText(真); //设置wordwrap它没有反映

    public XSSFWorkbook generateReport(List<Dto> result, boolean isRes, boolean isRes1) {
    XSSFWorkbook workbook = null;
    XSSFSheet sheet = null;
    XSSFRow row = null;
    XSSFCell cell = null;
    String[] headers = null;
    int rowNum = 0;
    int colNum = 0;
    CellStyle cellStyle = null;
    CellStyle headerStyle = null;
    XSSFFont font = null;
    CellStyle datecellStyle = null;
    /* set the weight of the font */



    try {
        workbook = new XSSFWorkbook();

        headers = new String[] { ...values goes here...};
        row = sheet.createRow(rowNum);
        font = workbook.createFont();
        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);

        headerStyle = workbook.createCellStyle();
        headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        headerStyle.setFillForegroundColor((short) 200);
        headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        headerStyle.setFont(font);

        cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);



        datecellStyle = workbook.createCellStyle();
        datecellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("dd-MMM-yyyy"));
        datecellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        datecellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        datecellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        datecellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);



        /**
         * Writing Headers
         */
        for (String header : headers) {
            cell = row.createCell(colNum);
            cell.setCellValue(header);
            cell.setCellStyle(headerStyle);
            ++colNum;
        }

        /**
         * Writing Other Rows
         */
        SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
        for (Dto detail : result) {
            ++rowNum;
            colNum = 0;
            row = sheet.createRow(rowNum);
            cell = row.createCell(colNum);
            //sheet.setColumnWidth(0, 4000);
            cell.
            if(null != detail.getGid()){
                cell.setCellValue(detail.getGid());
            }else{
                cell.setCellValue("-");
            }
            cell.setCellStyle(cellStyle);

            ++colNum;
            cell = row.createCell(colNum);
            if(null != detail.getName()){
                cell.setCellValue(detail.getName());
            }else{
                cell.setCellValue("-");
            }
            cell.setCellStyle(cellStyle);

            ++colNum;
            cell = row.createCell(colNum);
            if(null != detail.getNGid()){
                cell.setCellValue(detail.getNGid());
            }else{
                cell.setCellValue("-");
            }
            cell.setCellStyle(cellStyle);

            ++colNum;
            cell = row.createCell(colNum);
            if(null != detail.getName()){
                cell.setCellValue(detail.getName());
            }else{
                cell.setCellValue("-");
            }
            cell.setCellStyle(cellStyle);




        }

        for (int i = 0; i < headers.length; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.createFreezePane(1, 1);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return workbook;
}

2 个答案:

答案 0 :(得分:10)

setColumnWidth(int, int)应该有效......是不是因为你在循环中将大小重置为 auto

for (int i = 0; i < headers.length; i++) {
    sheet.autoSizeColumn(i);
}

从1开始循环到headers.length。

答案 1 :(得分:9)

您可以使用setColumnWidth的{​​{1}}方法设置列宽。第一个参数是列号(从零开始),第二个参数是宽度。在这里设置宽度需要一点技巧。要将宽度设置为XSSFWorkbook,我们需要将参数传递为25

25 * 256