How can I export numbers in the correct type using p:dataExporter?

user2231498 Source

I am using PrimeFaces dataExporter for exporting my datatable to an Excel file. The problem is that numbers are exported as text.

How can I export my numbers as integer and double?

    <p:column headerText="net_usd_sale">
        <h:outputText value="#{dizgi.net_usd_sale}" />
    </p:column>

    <p:column style="text-align: center" exportable="false">
   <f:facet name="header">
     <h:outputText value="Details" />
   </f:facet>
   <h:graphicImage value="/resources/images/details.png"  alt="details image" />

    </p:column>

</p:dataTable>
<h3>Export Page Data Only</h3>
    <h:commandLink>
        <p:graphicImage library="images" name="excel.png" width="24" />
        <p:dataExporter type="xls" target="tbl" fileName="dizgi" pageOnly="true"/>
    </h:commandLink>

    </h:form>

</html>   
primefacesdatatablepf-datatable

Answers

answered 3 years ago vels4j #1

There is an option to customize your dataExporter

   <h:commandLink id="excel">
    <p:graphicImage name="/demo/images/excel.png" />
    <p:dataExporter type="xls" target="tbl" fileName="cars" 
             postProcessor="#{customizedDocumentsView.postProcessXLS}" />
</h:commandLink>

In post process event you can manipulate your work book. Check this link Primefaces DataExporter - Customized Documents

answered 3 years ago user2231498 #2

 public void postProcessXLS(Object document) {
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFSheet sheet = wb.getSheetAt(0);
        HSSFRow header;



        HSSFCellStyle cellStyle = wb.createCellStyle();  
        cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        int ind=0;
        for(int j=0;j<dizgi.size()+1;j++) {
            header = sheet.getRow(j);
            for(int i=0; i < header.getPhysicalNumberOfCells();i++) {
                if(j==0){
                    HSSFCell cell = header.getCell(i);
                    cell.setCellStyle(cellStyle);
                }
                else{
                    if(i==2){
                        HSSFCell cell = header.getCell(i);
                        cell.setCellValue(dizgi.get(ind).getTicket_sold());
                    }
                    if(i==3 ){
                        HSSFCell cell = header.getCell(i);
                        cell.setCellValue(dizgi.get(ind).getNet_usd_sale());
                        ind++;
                    }
                }
            }
        }
    }   

> Ok I solved it,thanks.

comments powered by Disqus