在Primefaces Extensions Custom Exporter工作

时间:2015-05-26 16:10:43

标签: jsf primefaces

我在网页上有一些dataTables,我想将所有数据导出到excel文件,如网页图片:

Web Page and File Excel Exported

我想像页面中显示的那样导出页脚。我尝试使用渲染和可导出,两者都使用布尔属性,但没办法。     这种行为可能是什么原因?

1 个答案:

答案 0 :(得分:2)

我使用customExport,执行链接Steps to Custom Exporter上的步骤   当我调试代码时,我看到了在文档导出器上创建页脚的模式,它不是可导出的标签(isExportable())。进入方法" tableColumnGroup(Sheet sheet,DataTable table,String facetType){",类ExcelCustomExporter,我需要添加一个if:" if(column.isExportable()){& #34;创建一个单元格并为其添加值,以及另一个如果以相同的方式在(i ++)方法中使用增量变量;只有if为true我创建单元格,添加值并增加变量i。

见下面修改的代码:

protected void tableColumnGroup(Sheet sheet, DataTable table, String facetType) {
    ColumnGroup cg = table.getColumnGroup(facetType);
    List<UIComponent> headerComponentList = null;
    if (cg != null) {
        headerComponentList = cg.getChildren();
    }
    if (headerComponentList != null) {
        for (UIComponent component : headerComponentList) { 
            if (component instanceof org.primefaces.component.row.Row) {
                org.primefaces.component.row.Row row = (org.primefaces.component.row.Row) component;
                int sheetRowIndex = sheet.getLastRowNum() + 1;
                Row xlRow = sheet.createRow(sheetRowIndex);
                int i = 0;
                for (UIComponent rowComponent : row.getChildren()) {
                    UIColumn column = (UIColumn) rowComponent;
                    String value = null;
                    if (facetType.equalsIgnoreCase("header")) {
                        value = column.getHeaderText();
                    } else {
                        value = column.getFooterText();

                    }
                    int rowSpan = column.getRowspan();
                    int colSpan = column.getColspan();

                    Cell cell = xlRow.getCell(i);

                    if (rowSpan > 1 || colSpan > 1) {
                        if (rowSpan > 1) {
                            cell = xlRow.createCell((short) i);
                            Boolean rowSpanFlag = false;
                            for (int j = 0; j < sheet.getNumMergedRegions(); j++) {
                                CellRangeAddress merged = sheet.getMergedRegion(j);
                                if (merged.isInRange(sheetRowIndex, i)) {
                                    rowSpanFlag = true;
                                }

                            }
                            if (!rowSpanFlag) {
                                cell.setCellValue(value);
                                cell.setCellStyle(facetStyle);
                                sheet.addMergedRegion(new CellRangeAddress(
                                        sheetRowIndex, //first row (0-based)
                                        sheetRowIndex + (rowSpan - 1), //last row  (0-based)
                                        i, //first column (0-based)
                                        i  //last column  (0-based)
                                ));
                            }
                        }
                        if (colSpan > 1) {
                            cell = xlRow.createCell((short) i);

                            for (int j = 0; j < sheet.getNumMergedRegions(); j++) {
                                CellRangeAddress merged = sheet.getMergedRegion(j);
                                if (merged.isInRange(sheetRowIndex, i)) {
                                    cell = xlRow.createCell((short) ++i);
                                }
                            }
                            cell.setCellValue(value);
                            cell.setCellStyle(facetStyle);
                            sheet.addMergedRegion(new CellRangeAddress(
                                    sheetRowIndex, //first row (0-based)
                                    sheetRowIndex, //last row  (0-based)
                                    i, //first column (0-based)
                                    i + (colSpan - 1)  //last column  (0-based)
                            ));
                            i = i + colSpan - 1;
                        }
                    } else {
                        //TODO TRATAR E VERIRIFICAR SE  O VALUE PODE SER EXIBIDO
                        if (column.isExportable()) {
                            cell = xlRow.createCell((short) i);
                            for (int j = 0; j < sheet.getNumMergedRegions(); j++) {
                                CellRangeAddress merged = sheet.getMergedRegion(j);
                                if (merged.isInRange(sheetRowIndex, i)) {
                                    cell = xlRow.createCell((short) ++i);
                                }
                            }
                            cell.setCellValue(value);
                            cell.setCellStyle(facetStyle);
                        }    
                    }
                    if (column.isExportable()) {
                        i++;
                    }   
                }
            }

        }

    }

Screnshoot,但是JAN到May:

Exporter Customized - Footer ok