【问题标题】:primefaces datatable exporterprimefaces 数据表导出器
【发布时间】:2025-12-11 00:35:01
【问题描述】:

在 XHTML 页面中支持多种语言。我使用数据表显示数据,例如:

<p:column style="text-align:center; width:7%;" exportPosition="center">
      <f:facet name="header">
           <h:outputText value="#{msg['common']['identityIssuePlace']}" />
      </f:facet>
      <h:outputText value="#{element.getLocalizedName(bean.locale)}" />
</p:column>

它显示正常。但我使用dataExporter 导出数据会引发异常:

avax.el.PropertyNotFoundException: /backend/gms/account-manage.xhtml at line 170 and column 109 value="#{element.getLocalizedName(bean.locale)}": Property 'getLocalizedName' not found on type safp.acms.common.domain.IdentityIssuePlace

dataExporter不能绑定方法?谢谢高级。

【问题讨论】:

    标签: primefaces datatable export


    【解决方案1】:

    我用这个解决了

    Xhtml代码:

    <p:commandButton image="ui-icon-xls" styleClass="botonExcel" value="mylabel" ajax="false">                              
       <p:dataExporter type="xls" target=":formEditar:tablaEditar" fileName="myFilename"  postProcessor="#{parametrosMB.postProcessXLS}" />  
    </p:commandButton> 
    

    Java 代码:

      public void postProcessXLS(Object document) {
            HSSFWorkbook wb = (HSSFWorkbook) document;
            HSSFSheet sheet = wb.getSheetAt(0);
    
            //Loop for all datafiles 
            //(sheet.getLastRowNum() + 1) => because file 0 is a header
            for (int i = 0; i < sheet.getLastRowNum() + 1; i++) {
                HSSFRow header = sheet.getRow(i);              
    
                //Loop for each cell
                for (int j = 0; j < header.getPhysicalNumberOfCells(); j++) {
                    HSSFCell cell = header.getCell(j);                    
    
                    //Here you need verify the cell to change value and assign new value
                    cell.setCellValue("the new value!!");
                }
            }
        }
    

    灵感来自:http://www.primefaces.org/showcase/ui/data/dataexporter/customizedDocuments.xhtml

    对不起我的英语不好

    【讨论】:

      【解决方案2】:

      解决了!!!!

      有了这个p:dataExporter does not recognize p:cellEditor

      我的解决方案太相似了,只改变了一个函数“exportValue”和扩展类。

      Xhtml:

       <h:commandLink value="Excel" action="#{myBean.exportExcel(table, 'filename')}" />
      
      <p:dataTable id="tablaDatos" binding="#{table}" etc...> 
      <!-- data -->
      </p:dataTable>
      

      bean 上的方法:

      public void exportExcel(DataTable table, String filename) throws IOException {
          FacesContext context = FacesContext.getCurrentInstance();
          MyExporterXls exporter = new MyExporterXls();
          exporter.export(context, table, filename, false, false, "UTF-8", null, null);
          context.responseComplete();
      }
      

      我的解决方案

      public class MyExporterXls extends ExcelExporter {
      
          @Override
          protected String exportValue(FacesContext fc, UIComponent uic) {
              String valor = "";
              System.out.println("uic = " + uic);
      
              if (uic instanceof HtmlOutputText) {
                  valor = parseValorCelda(((HtmlOutputText) uic).getValue());
              }
              return valor;           
          }
      
          private String parseValorCelda(Object valor) {
              String retorno = "";
      
              //Cadenas
              if (valor instanceof String) {
                  retorno = (String) valor;
              }
      
              //Numeros ENTEROS
              if (valor instanceof Short) {
                  retorno = ((Short) valor).toString();
              }
      
              if (valor instanceof Integer) {
                  retorno = ((Integer) valor).toString();
              }
      
              if (valor instanceof BigInteger) {
                  retorno = ((BigInteger) valor).toString();
              }
      
              if (valor instanceof Long) {
                  retorno = ((Long) valor).toString();
              }
      
              //Numeros decimales
              if (valor instanceof Double) {
                  retorno = ((Double) valor).toString();
              }
      
              if (valor instanceof Float) {
                  retorno = ((Float) valor).toString();
              }
      
              if (valor instanceof BigDecimal) {
                  retorno = ((BigDecimal) valor).toString();
              }
      
              return retorno;
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        很久没有问这个问题了,@dmnoguera 给出的唯一答案对我来说并不满意。

        实际上,primefaces 的dataExporter 似乎不支持绑定方法。但是我是如何解决这个问题的:

        这是我在 dataTable 中可导出的列:

        <p:column>
            <f:facet name="header">
                <h:outputText value="#{messages['portfolioOfUser']}" />
            </f:facet>
            <h:outputText id="portfoliosOfUser"
                value="#{editPortfolio.portfoliosOfUser(listCustomerTableRow)}"
                escape="false" />
        </p:column>
        

        这是我的 Bean:

        public class EditPortfolioManagedBean extends EnhancedManagedBean {
        
            private String portfoliosOfUser ;
        
            public String getPortfoliosOfUser() {
                return "Coucou";
            }
        
            public void setPortfoliosOfUser(String portfoliosOfUser) {
                this.portfoliosOfUser = portfoliosOfUser;
            }
        
        //Cool stuf - Start
        ...
        //Cool stuf - End
        
        public String portfoliosOfUser(Customer customer)
            {
                String listOfPortfolios = new String("");
        
                //Really sexy computation and high level algorithms
        
                return listOfPortfolios;
        
            }
        }
        

        这样我欺骗了dataExporter,当我导出表格时,我得到了portfoliosOfUser(Customer customer) 给出的String,而不是“Coucou”。希望对其他人有用!

        【讨论】: