【问题标题】:Invalid sheet name error exporting datatable to excel将数据表导出到 Excel 时工作表名称错误
【发布时间】:2017-01-18 05:18:38
【问题描述】:

当我点击 “export as excel” 链接时,我想将 jsf 数据表导出为 excelsheet。我正在使用“Apache poi 库”和“PrimeFaces”库来完成这项任务。

我正在使用 PrimeFaces 成功显示我的数据表。当我点击 "export as excel" 链接时,出现异常

javax.faces.FacesException: sheetName '' 无效 - 字符数 必须大于或等于 1 且小于或等于 31。

请帮我解决我的问题。

谢谢。

这是我的 jsf 文件。

     <p:dataTable id = "dataTableId" var = "pr" value = "#{dashboard.beanList}"  >

        <p:panel header="Export All Data">

        <h:commandLink value="export as excel sheet">

            <p:dataExporter type="xls"  target="dataTableId" fileName="myexcel" />

        </h:commandLink>
        </p:panel>

这是我的堆栈跟踪文件。

javax.faces.FacesException: sheetName '' is invalid - character count MUST be greater than or equal to 1 and less than or equal to 31
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:85)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:334)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
          Caused by: java.lang.IllegalArgumentException: sheetName '' is invalid - character count MUST be greater than or equal to 1 and less than or equal to 31 at org.apache.poi.ss.util.WorkbookUtil.validateSheetName(WorkbookUtil.java:108)
at org.apache.poi.hssf.record.BoundSheetRecord.setSheetname(BoundSheetRecord.java:94)
at org.apache.poi.hssf.model.InternalWorkbook.setSheetName(InternalWorkbook.java:588)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.createSheet(HSSFWorkbook.java:798)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.createSheet(HSSFWorkbook.java:78)
at org.primefaces.component.export.ExcelExporter.export(ExcelExporter.java:52)
at org.primefaces.component.export.DataExporter.processAction(DataExporter.java:112)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
at javax.faces.component.UICommand.broadcast(UICommand.java:296)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1255)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
... 22 more

这是我完整的 Xhtml 文件。

    <p:dataTable id = "dataTableId" var = "pr" value = "#{dashboard.beanList}"  >

    <f:facet name="header">
<!-- edit button -->    
    <p:commandButton id="toggler1" type="button" value="Customize Columns" style="float:right" icon="ui-icon-calculator" />
    <p:columnToggler datasource="dataTableId" trigger="toggler1" />
</f:facet>


            <p:column>
                <f:facet name="header"><h:selectBooleanCheckbox /> </f:facet>
                <h:selectBooleanCheckbox />
            </p:column>


            <p:column>

                <f:facet name="header"> UPC </f:facet>
                <div class="help-tip">
                    <h:outputLabel value="#{pr.lUpc}" id="l1" />
                </div>
            </p:column>


            <p:column>
                <f:facet name="header"> Cost </f:facet>
                <h:outputLabel value="#{pr.dCost}" />
            </p:column>



            <p:column>
                <f:facet name="header"> SKU </f:facet>
                <h:outputLabel value="#{pr.sSku}" />
            </p:column>


            <p:column>
                <f:facet name="header"> ProductName </f:facet>
                <h:outputLabel value="#{pr.sProductName}" id="l2" />
            </p:column>




            <p:column>

                <p:commandButton id="btn" value="Edit" onclick = "getupc(#{pr.lUpc})" >

                </p:commandButton>
            </p:column>

        </p:dataTable>




    <p:panel header="Export All Data">

    <h:commandLink value="export as excel sheet">

        <p:dataExporter type="xls"  target="dataTableId" fileName="myexcel" />

    </h:commandLink>
    </p:panel>

【问题讨论】:

  • 发布完整的数据表。你没有 p:column 元素。
  • 您可以设置断点并调试为什么 sheetName 为空
  • 请注意您错误地使用了h:outputLabel。这里应该是h:outputText
  • 我可以知道如何在jsf中设置断点
  • 异常明确表示工作表名称无效。检查工作表名称是什么。

标签: jsf primefaces apache-poi


【解决方案1】:

尝试评论这一行: 编辑按钮
-->

【讨论】:

  • 为什么你认为这会有所帮助?
【解决方案2】:

如果您将 primefaces 更新到 6.0 或更高版本,那么它将检查 excel 的 sheetName。它将检查 f:facet's 内部,如果它包含任何值,它将在 sheet0 中打印该名称。如果没有值,则工作表将为空。然后它会返回异常。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题。

    正如“Anand”在之前的答案中所说,primefaces 将名称搜索为构面。

    我有这个代码:

     <p:dataTable id="xxxxxxx" value="#{bean.xxxxxxxx}" var="row" scrollable="true" scrollHeight="800" style="margin-top:10px; " >
             <f:facet name="header">
                 <p:outputPanel style="display:flex;justify-content:space-between; width:100%;">
                            <h:commandLink>
                                <p:graphicImage
                                    style="width:21px;height: 21px;margin:2px"
                                    name="images/excel.png"/>
                                   <p:dataExporter type="xls" target="xxxxxxx" fileName="Detail" postProcessor="#{bean.postProcessXLS}" />
                            </h:commandLink> 
                 </p:outputPanel>
                </f:facet>
              
              ...........

    同样的异常发生了,但是像这样在 facet 中添加了一个标签

    <p:dataTable id="xxxxxxx" value="#{bean.xxxxxxxx}" var="row" scrollable="true" scrollHeight="800" style="margin-top:10px; " >
             <f:facet name="header">
                 <p:outputPanel style="display:flex;justify-content:space-between; width:100%;">
                     <p:outputLabel value="xxxxxxxxx" escape="false" style="line-height: 2; font-size: 13px;"></p:outputLabel>
                            <h:commandLink>
                                <p:graphicImage
                                    style="width:21px;height: 21px;margin:2px"
                                    name="images/excel.png"/>
                                   <p:dataExporter type="xls" target="xxxxxxx" fileName="Detail" postProcessor="#{bean.postProcessXLS}" />
                            </h:commandLink> 
                 </p:outputPanel>
                </f:facet>
              
              ...........

    它有效...不要问我为什么..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      相关资源
      最近更新 更多