XPage Tutorial-Simple Export to Excel Server Side JavaScript

Open your XPage and copy this code on 'afterRenderResponse' XPage event. To run this code , call this XPage , to find actual implementation of this script please click here

////// Your code Started - Copy from here
var myDatabase:NotesDatabase=session.getDatabase(database.getServer(),"xpagecontrols.nsf");

// Copyright xpagedomino (www.xpagedomino.com)
// Contact admin@xpagedomino.com for any comment
// Trusted code fully tested by 'xpagedomino.com

var myView:NotesView = myDatabase.getView("byNameProfile");
var myViewNav:NotesViewNavigator = myView.createViewNav();
var response = facesContext.getExternalContext().getResponse();
var DataWriter = facesContext.getResponseWriter();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition","attachment; filename='exporttoexcelSSJS.xls'");


var viewEntry:NotesViewEntry = myViewNav.getFirst();

var thead:string = "";
var tr:string = "";


thead="<th>FirstName </th>"
thead=thead+"<th> LastName </th>"
thead=thead+"<th> DOB </th>"
thead=thead+"<th> City </th>"
thead=thead+"<th> Country </th>"


while (viewEntry != null) {
            tr=tr+"<tr>"
            tr=tr+"<td>"+viewEntry.getColumnValues()[0]+"</td>"
            tr=tr+"<td>"+viewEntry.getColumnValues()[1]+"</td>"
            tr=tr+"<td>"+viewEntry.getColumnValues()[2]+"</td>"
            tr=tr+"<td>"+viewEntry.getColumnValues()[3]+"</td>"
            tr=tr+"<td>"+viewEntry.getColumnValues()[4]+"</td>"
            tr=tr+"</tr>"
viewEntry = myViewNav.getNext(viewEntry);
}
DataWriter.write("<table border='1'>"+thead+tr+"</table>");
DataWriter.endDocument();

////// Your code Ends -Copy Code ends here





No comments :

Post a Comment

Leave A Comment...