Export to Excel from JSF Page
801557Nov 10 2010 — edited Nov 17 2010Hi,
I want to export the data from the jsf page to a excel sheet.
In this method by using view iterator i am trying to export it to the excel .
I have a pannel tabbed in my jsf page.
NsEventDetailsView1Iterator is present in one tab and Participation_List_staffdetails1Iterator is in another tab so when the second tab view iterator data is not fetched properly so the exorted file comes in HTML format.
But if i use only for first tab iterator the data is exported properly in excel format.
I have attached the following code:
public void exportToExcel() throws IOException {
DCBindingContainer dcBindings =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding EventDetailContent=(DCIteratorBinding)dcBindings.get("NsEventDetailsView1Iterator");
DCIteratorBinding EventParticipationList=(DCIteratorBinding)dcBindings.get("Participation_List_staffdetails1Iterator");
Date dt = new Date();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
String filename = fmt.format(dt) + ".csv";
//Setup the output
String contentType = "application/vnd.ms-excel";
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletResponse response;
response = (HttpServletResponse)fc.getExternalContext().getResponse(); ;
response.setHeader("Content-disposition",
"attachment; filename=" + filename);
response.setContentType(contentType);
PrintWriter out = response.getWriter();
RowSetIterator rsi = EventDetailContent.getRowSetIterator();
String[] attNames = rsi.getRowAtRangeIndex(0).getAttributeNames();
for (int i = 0; i < attNames.length; i++) {
if (i > 0)
out.print(",");
out.print(attNames);
}
out.println();
for (int i = 0; i < rsi.getRowCount(); i++) {
Row currentRow = rsi.getRowAtRangeIndex(i);
Object[] attValues = currentRow.getAttributeValues();
for (int j = 0; j < attValues.length; j++) {
if (j > 0)
out.print(",");
if (attValues[j] != null)
out.print("\"" + attValues[j] + "\"");
}
out.println();
}
RowSetIterator rsiParticipationList = EventParticipationList.getRowSetIterator();
String[] attNamesParticipationList = rsiParticipationList.getRowAtRangeIndex(0).getAttributeNames();
for (int i = 0; i < attNamesParticipationList.length; i++) {
if (i > 0)
out.print(",");
out.print(attNamesParticipationList[i]);
}
out.println();
for (int i = 0; i < rsiParticipationList.getRowCount(); i++) {
Row currentRow = rsiParticipationList.getRowAtRangeIndex(i);
Object[] attValuesParticipationList = currentRow.getAttributeValues();
for (int j = 0; j < attValuesParticipationList.length; j++) {
if (j > 0)
out.print(",");
if (attValuesParticipationList[j] != null)
out.print("\"" +attValuesParticipationList[j] + "\"");
}
out.println();
}
out.close();
fc.responseComplete();
}
Please help me
Thanks