【问题标题】:export to excel for dynamic columns with header导出到带有标题的动态列的 excel
【发布时间】:2015-06-30 08:03:01
【问题描述】:

在我的另一个应用程序中,我有一个带有动态列的数据表。我的代码应该将数据表内容导出为 excel 格式。我拥有的代码适用于具有固定列数的数据表。但如果它在动态列中,它不会带来标题行。我浏览了更多网站,他们建议使用带有标签的虚拟列。我也试过了。但无法将标题带入excel。其他excel内容显示正常。

我的代码是:

Xhtml代码:

<p:dataTable id="rosterviewtab" var="data" value="#{dtRosterView.rostertab}" >

    <p:columnGroup type="header">

        <p:row>
            <p:column headerText="" />

    <p:column style="display:none">
            <f:facet name="header"> #{dtRosterView.dates}   </f:facet>
    </p:column>

            <ui:repeat value="#{dtRosterView.dates}" var="dates">
               <p:column headerText="#{dates}" exportable="false" />                
            </ui:repeat>
        </p:row>
    </p:columnGroup>   

      <p:column> 
        <h:outputText value="#{data.slot_noresources}" />
      </p:column>    

     <p:column>
        <h:outputText value="#{data.datecol1}" />
    </p:column>     

     <p:column>
        <h:outputText value="#{data.datecol2}" />
    </p:column> 

    </p:dataTable>

    <h:panelGrid columns="2" cellpadding="5">
    <p:commandButton value="Export To Excel" style="font-size:11px;width:120px;height:40px" immediate="true" ajax="false" >          
    <p:dataExporter type="xls" target="rosterviewtab" fileName="RosterView" postProcessor="#{dtRosterView.postProcessXLS}" update="@form"  />
    </p:commandButton>

在我的 Bean 类中:

public void postProcessXLS(Object document) 
{     HSSFWorkbook wb = (HSSFWorkbook) document;        
      HSSFSheet sheet = wb.getSheetAt(0);  
     for (Row row : sheet) {            
          for (Cell cell : row) {   
       System.out.println("cell value -->"+ cell.getStringCellValue());
       cell.setCellValue(cell.getStringCellValue().toUpperCase());                
        }       
    }
    }

【问题讨论】:

    标签: jsf primefaces datatable


    【解决方案1】:

    我有同样的情况,我使用了 p:fileDownload 并像这样实现它: 在 xhtml 中:

    <p:commandLink ajax="false">
         <p:graphicImage value="#{exportType.icon}" title="#{exportType.firstLangName}"/>
         <p:fileDownload value="#{customStatementBean.makeExportFile(exportType.exportTypeId)}"/>
    </p:commandLink>
    

    在 bean 中:

        public StreamedContent makeExportFile(Long exportTypeId) {
            InputStream stream;
            try {
                stream = customStatementService.getExportFileInputStream(customStatementSO, customStatementResult, exportTypeId, getCurrentLanguage().toLowerCase());
            } catch (SabaBusinessException e) {
                saveError(e);
                return null;
            }
            return new DefaultStreamedContent(stream, getContentType(exportTypeId), customStatementService.makeFileName(customStatementSO, exportTypeId), "UTF-8");
        }
    

    当然,在 customStatementService.getExportFileInputStream 中,我已经根据需要使用 POI 创建了 excel 文件。

    我在 pom.xml 中用于 poi 的依赖是这样的:

    <dependency>
       <groupId>org.apache.poi</groupId>
       <artifactId>poi</artifactId>
       <version>3.9</version>
    </dependency>
    

    【讨论】:

    • 是否有任何依赖项可以使用文件下载..因为在我的应用程序中文件上传功能在那里,我们在其中遇到了很多依赖项问题。我们仍然无法上传。如果可能的话,请您发布完整的源代码。
    • 很抱歉我的回复晚了,我已经编辑了答案以提供您要求的额外信息。当然 Excel 需要 POI,而不是使用 p:fileDownload
    【解决方案2】:

    我使用下面的代码解决了这个问题。

    HSSFWorkbook wb = (HSSFWorkbook) document;        
        HSSFSheet sheet = wb.getSheetAt(0);  
        /*HSSFRow header = sheet.getRow(1);
        HSSFCellStyle cellStyle = wb.createCellStyle();        
        for(int i=0; i < header.getPhysicalNumberOfCells();i++) {  
            HSSFCell cell = header.getCell(i);  
    
            cell.setCellStyle(cellStyle);  
        }  */
        HSSFRow row1 = sheet.createRow(0);
        //HSSFCell r1c1 = row1.createCell(1);
        //r1c1.setCellValue("dates");
        int columval = 1;
        for(int d=0; d<dates.size();d++)
        {
            System.out.println("dates-->"+dates.get(d).toString());
            HSSFCell r1c1 = row1.createCell(columval);
            r1c1.setCellValue(dates.get(d).toString());
            columval +=1;
        }
        for (Row row : sheet) {            
            for (Cell cell : row) {   
                System.out.println("cell value -->"+ cell.getStringCellValue());
                cell.setCellValue(cell.getStringCellValue().toUpperCase());                
    
                }       
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-03
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多