3
votes

Im using primefaces 3.5 and I have a p:dataTable with some rows and data and i would like to export these data into a xls file or something.

I used the code:

<p:commandButton id="exportBtn" icon="ui-icon-extlink" styleClass="statisticsMenuButton" ajax="false" title="#{msg.general_export_as_xls}">
    <p:dataExporter type="xls" target="cc-eventListTable-eventList" fileName="statistics"/>
</p:commandButton>

This works fine!

My problem is, i would like to have different cell text in the exported file and in the browser dataTable.

e.g In the exported xls file i need other date formatting then in the browser data table. Otherwise the text in the browser cell is too long!

I tried to add a additional column with rendered="false" and exportable="true" only for exported table. But unfortunately it does not work!

Does anybody have an idea how to do that?

2

2 Answers

6
votes

With the rendered tag set to false it doesn´t work at the moment (it´s been reported as bug: https://github.com/primefaces-extensions/primefaces-extensions.github.com/issues/209), but you can make it with css like this:

 <p:column headerText="#{msg['book.coverType']}" style="display:none;">
         <h:outputText value="#{book.coverType}"/>
</p:column>

Another solution is to add a post process method in your managed bean and add the column from there.

public void postProcessXLS(Object document) { log.debug("post processing Excel");

    HSSFWorkbook workbook = (HSSFWorkbook) document;

    addCoverType(workbook,book);

}

you can write the method like:

public void addCoverType(HSSFWorkbook workbook, List<Book> books) {

    HSSFSheet sheet = workbook.getSheetAt(0);
    HSSFCell cell = null;

 //row 0 is the header (not automatically added by primefaces)
 //add a fifth cell to each row
    for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
        sheet.getRow(i).createCell(4);
        cell = sheet.getRow(i).getCell(4);
        cell.setCellValue(book.get(i - 1).getCoverType());
    }
    log.debug("cover type added");
}

If you make sortable column the order will be automatically respected as well :-D

0
votes

I found a workaround solution for my problem...

To make cell text in my dataTabe for browser invisible i putted the css syle "visibility:hidden;" to some h:outputText.

<p:column styleClass="eventFrom" width="125" sortBy="#{event.dateFrom}">
     <f:facet name="header">
         <h:outputText value="#{msg.general_date}"></h:outputText>
     </f:facet>

     <h:outputText value="#{event.dateFrom}" id="dateFrom">
         <f:convertDateTime type="both" timeZone="#{userManagement.userOptions.timeZone}" pattern="#{msg.time_pattern_ymdhmsM_short}" locale="#{msg.local}" />
     </h:outputText>

     <h:outputText value=" - " id="between" style="visibility:hidden;"></h:outputText>

     <h:outputText value="#{event.dateTo}" id="dateTo" style="visibility:hidden;">
          <f:convertDateTime type="both" timeZone="#{userManagement.userOptions.timeZone}"pattern="#{msg.time_pattern_ymdhmsM_short}" locale="#{msg.local}" />
     </h:outputText>

</p:column>

It would be better to create columns just for dataExporter but i didn't found a solution to do that.