Note that there are some explanatory texts on larger screens.

plurals
  1. POPrimeFaces Export data from a Lazyloading DataTable
    text
    copied!<p>I want to export a dataTable (with Pagination) having LazyLoad DataModel during Report Generation. </p> <p><strong>Problem :</strong> When I export, the report is getting generated page by page from Database and then getting exported to Excel/PDF that consumes more time. I would like to get it in a single database access by skipping the page by page generation of dataset. </p> <p>I'm producing my code snippet as follows:</p> <p><strong>JSF:</strong> </p> <pre><code>&lt;p:dataTable id="dTable" var="dev" value="#{ReportAction.lazyModel}" styleClass ="table_paginator" rowKey="#{device.macAddress}" paginatorPosition="bottom" paginator="true" rowsPerPageTemplate="10,20,30" rows="10" lazy="true" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" emptyMessage="Select appropriate conditions and click 'Generate Report'"&gt; &lt;f:facet name="header"&gt; &lt;h:commandLink actionListener="#{ReportAction.doExport}"&gt; &lt;p:graphicImage value="../../../resources/images/excel.png" alt="XLS" style="float:right;width:32px;height:32px" /&gt; &lt;p:dataExporter type="xls" target="dTable" fileName="#{ReportAction.fileName}" preProcessor="#{ReportAction.preProcess}" postProcessor="#{ReportAction.postProcessXLS}" /&gt; &lt;/h:commandLink&gt; &lt;/f:facet&gt; &lt;!-- All the columns in Data Table --&gt; &lt;/p:dataTable&gt; </code></pre> <p><strong>Managed Bean:</strong></p> <pre><code>public class ReportAction { private ConfigurationReportDataModel mediumConfigModel; private List&lt;FieldReportModel&gt; configModelList; private String fileName; private LazyDataModel&lt;ConfigurationReportModel&gt; lazyModel; private boolean export; public ReportAction() { configModelList = new ArrayList&lt;ConfigurationReportModel&gt;(); export = false; mediumConfigModel = new ReportDataModel(); } public void generateFieldReport() { lazyFieldModel = new ConfigurationReportDataModel(day, fromDate, location,store,engineer, status, toDate, export); } public void preProcess(Object document) { export = true; log.info("preProcess::Lazy model : Page Sizing"); if(lazyFieldModel != null) { lazyFieldModel.setPageSize(1000000); } log.info("preProcess::Export All Details"); mediumConfigModel.setExport(true); } public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0); HSSFRow header = sheet.getRow(0); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.GREEN.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); for (int i = 0; i &lt; header.getPhysicalNumberOfCells(); i++) { HSSFCell cell = header.getCell(i); cell.setCellValue(cell.getStringCellValue().toUpperCase()); cell.setCellStyle(cellStyle); sheet.autoSizeColumn(i); } export = false; mediumConfigModel.setExport(false); } public List&lt;ConfigurationReportModel&gt; getConfigModelList() { return configModelList; } public void setConfigModelList( ArrayList&lt;ConfigurationReportModel&gt; configModelList) { this.configModelList = configModelList; } public String getFileName() { SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy_HHmmss"); fileName = "Config_Report_".concat(formatter.format(new Date())); return fileName; } public void setMediumConfigModel( ConfigurationReportDataModel mediumConfigModel) { this.mediumConfigModel = mediumConfigModel; } public void setConfigModelList( List&lt;ConfigurationReportModel&gt; configModelList) { this.configModelList = configModelList; } public LazyDataModel&lt;ConfigurationReportModel&gt; getLazyFieldModel() { log.info("##########getLazyFieldModel###########"); if(export) { log.info("getLazyFieldModel::Will get Exported........"); lazyFieldModel = new ConfigurationReportDataModel(day, fromDate, location, store, engineer, status, toDate, true); lazyFieldModel.load(1, 1000000000, null, null, null); } return lazyFieldModel; } public void setLazyFieldModel( LazyDataModel&lt;ConfigurationReportModel&gt; lazyFieldModel) { this.lazyFieldModel = lazyFieldModel; } } </code></pre> <p><strong>DataModel:</strong></p> <pre><code>public class ConfigurationReportDataModel extends LazyDataModel&lt;ConfigurationReportModel&gt; { private List&lt;ConfigurationReportModel&gt; configReport; private boolean export; public ConfigurationReportDataModel() { this.export = false; } public List&lt;ConfigurationReportModel&gt; load(int first, int pageSize, String sortField, SortOrder sortOrder, Map&lt;String, String&gt; filters) { UIClient client = new UIClient(); ReportData data = null; // /////////////////// if(export) { log.info("Do Export...."); first = 1; pageSize = 1000000000; } deviceList = new ArrayList&lt;DeviceGlobal&gt;(); // Retrieves data from Database with the number of record (page size) data = client.generateFieldReport(first, pageSize, Integer.parseInt(location), Integer.parseInt(store), engineer, getTimeToBeginningOfDay(), getTimeToEndofDay(), status); log.info("Obtained data : " + data); if (data != null) { log.info("Got devices : " + data.getRecords().size()); deviceList = (ArrayList&lt;DeviceGlobal&gt;) data.getRecords(); // /////////////////// int record = first + 1; ConfigurationReportModel storeModel = null; DeviceGlobal deviceGlobal = null; configReport = new ArrayList&lt;ConfigurationReportModel&gt;(); for (Iterator&lt;DeviceGlobal&gt; iterator = deviceList.iterator(); iterator .hasNext();) { deviceGlobal = (DeviceGlobal) iterator.next(); storeModel = new ConfigurationReportModel(deviceGlobal, record++); configReport.add(storeModel); } log.info("Total Config Report : " + configReport.size()); // rowCount int dataSize = data.getReportCount(); this.setRowCount(dataSize); log.info("Report Count: " + data.getReportCount()); if(export) { return configReport; } else { // paginate if (dataSize &gt; pageSize) { try { return configReport; } catch (IndexOutOfBoundsException e) { return configReport; } } else { return configReport; } } } else { log.info("Got no devices"); deviceList = new ArrayList&lt;DeviceGlobal&gt;(); configReport = new ArrayList&lt;ConfigurationReportModel&gt;(); this.setRowCount(0); return configReport; } } } </code></pre> <p><strong>Note:</strong> </p> <ul> <li>There's no syntax error. </li> <li>All the custom type classes are defined.</li> </ul> <p>How do I get the Excel report by skipping the page by page generation of records?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload