使用Primefaces将可编辑数据表导出到Excel仅显示标题

时间:2013-10-04 16:05:13

标签: jsf-2 primefaces

我正在尝试使用Primefaces的dataExporter组件将dataTable中的值导出到Excel。

  

我正在使用primefaces 3.5和jsf 2.2;

我的dataTable和commandlink如下:

<p:dataTable id="tableTemp" binding="#{ledgerComplMB.tableTemp}" value="#{ledgerComplMB.listaTemp}"
         var="q" emptyMessage="No Registry" paginator="true" rows="10" 
         rowsPerPageTemplate="10,50,200,500"
         paginatorTemplate=" {CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
         scrollable="true" scrollWidth="100%" scrollRows="10"
         filteredValue="#{ledgerComplMB.listaTempFilter}"
         filterDelay="1500" resizableColumns="true" editable="true">

<f:facet name="header">
    <p:commandButton id="btGravar" icon="ui-icon-disk"
                     value="Write data" actionListener="#{ledgerComplMB.gravar}"
                     disabled="${empty ledgerComplMB.listaTemp}" style="width:200px;"  />

    <p:commandButton id="btLimpar" value="Clean data"
                     actionListener="#{ledgerComplMB.limpar}" icon="ui-icon-trash"
                     disabled="${empty ledgerComplMB.listaTemp}"
                     style="width:200px;font-weight: bold;margin-left: 20px"/>

    <br />
    <h:outputText
        value="Total: #{fn:length(ledgerComplMB.listaTemp)}"
        rendered="#{not empty ledgerComplMB.listaTemp}" />
</f:facet>

<p:ajax event="rowEdit"
        oncomplete="dlgConfirma.show()" update=":form:dialogConfirmacao" />

<p:column exportable="false" style="width:30px">
    <p:rowEditor />
      </p:column>

<p:column filterBy="#{q.ledger.accountNumber}" filterMatchMode="exact"  style="width:100px">
    <f:facet name="header">AccountNumber</f:facet>
    <h:outputText value="#{q.ledger.accountNumber}" />
</p:column>

<p:column filterBy="#{q.ledger.vendor}" filterMatchMode="exact" style="width:100px">
    <f:facet name="header">Vendor</f:facet>                                      
    <p:cellEditor>
        <f:facet name="output">
            <h:outputText value="#{q.ledger.vendor}" />
        </f:facet>
        <f:facet name="input">
            <p:inputText value="#{q.ledger.vendor}" />
        </f:facet>
    </p:cellEditor>
</p:column>

</p:dataTable>


<p:commandLink id="exp1" ajax="false">
      <p:graphicImage library="img" name="pdf.png" />
<p:dataExporter type="xlsx" target="tableTemp" fileName="#{ledgerComplMB.retornaDataExport}"  />  

当我请求导出时,只显示标题值。

1 个答案:

答案 0 :(得分:1)

我发现的唯一解决方案是创建自己的类,扩展Exporter并覆盖方法 protected String exportValue(FacesContext context, UIComponent component)

您应该只添加以下行中的现有代码:

else if (component instanceof CellEditor) {
 return exportValue(context, ((CellEditor) component).getFacet("output")); 
}

这是因为CellEditor不可导出。我还没有检查它是否仍然是PrimeFaces 4.0中的一个问题。 issue4013已将此问题视为问题BalusC 如果导出到Excel文件,这里是我写的和它工作(这是在扩展ExcelExporter的类):

@Override
protected String exportValue(FacesContext context, UIComponent component) {
        if (component instanceof HtmlCommandLink) { // support for PrimeFaces
                                                    // and standard
                                                    // HtmlCommandLink
            HtmlCommandLink link = (HtmlCommandLink) component;
            Object value = link.getValue();

            if (value != null) {
                return String.valueOf(value);
            } else {
                // export first value holder
                for (UIComponent child : link.getChildren()) {
                    //if (child instanceof ValueHolder) {
                        return exportValue(context, child);
                    //}
                }

                return "";
            }
        } else if (component instanceof CellEditor) { // Handle in-cell editable datatables
            return exportValue(context,
                    ((CellEditor) component).getFacet("output"));
        } else if (component instanceof ValueHolder) {

            if (component instanceof EditableValueHolder) {
                Object submittedValue = ((EditableValueHolder) component)
                        .getSubmittedValue();
                if (submittedValue != null) {
                    return submittedValue.toString();
                }
            }

            ValueHolder valueHolder = (ValueHolder) component;
            Object value = valueHolder.getValue();
            if (value == null)
                return "";

            // first ask the converter
            if (valueHolder.getConverter() != null) {
                return valueHolder.getConverter().getAsString(context,
                        component, value);
            }
            // Try to guess
            else {
                ValueExpression expr = component.getValueExpression("value");
                if (expr != null) {
                    Class<?> valueType = expr.getType(context.getELContext());
                    if (valueType != null) {
                        Converter converterForType = context.getApplication()
                                .createConverter(valueType);

                        if (converterForType != null)
                            return converterForType.getAsString(context,
                                    component, value);
                    }
                }
            }

            // No converter found just return the value as string
            return value.toString();
        } else {
            // This would get the plain texts on UIInstructions when using
            // Facelets
            String value = component.toString();

            if (value != null)
                return value.trim();
            else
                return "";
        }

}