XPage Export to Excel Using Server Side Java Script


XPage Export to excel Using Server Side Java Script:  In the last article you may have seen the export to excel using Lotus Script on the xpage. Lotus script on server side uses the content type to export data to excel from browser.
Here we have a similar structure to export data to excel but from SSJS.
I.e. side java script.
The Export to excel using server side JavaScript have a few changes, we need to use event afterRenderResponse and facesContext from JSF to get the browser instance handle.

The similarity between Lotus script and Server side Java script export to excel is being demonstrated below on each line of code.

We need to initialize the notes session, notes database, notes view, notes entry, notes navigation and some string variables to hold output data.

Now the question is, where and how to write this code on xpage?

To write this code Open / create a new blank page and open the xPage source and just paste below code to your xPage source.

Or refer below screen-

Open blank xPage and go to xPage Event afterRenderResponse and paste below code.

Remember to modify the view name, database name, server name and view column to work below code.
Refer below screen to add this script:






//xpage afterRenderResponse: event code
//Start Code

var myDatabase:NotesDatabase = session.getDatabase(database.getServer() , "xpagecontrols.nsf");// set your database here LS' set myDatabase=session.getDatabase("","")

// 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");//' set your view LS' set myView=myDatabase.Getview("")
var myViewNav:NotesViewNavigator = myView.createViewNav();//' set navigator LS ' Set myViewNav=myView.createViewNav()

var response = facesContext.getExternalContext().getResponse(); // set document response from the browser

var DataWriter = facesContext.getResponseWriter(); //' Set data writers from JSF to write on the document

response.setContentType("application/vnd.ms-excel");// set content type  LS - Print {Content-Type:application/vnd.ms-excel}
response.setHeader("Cache-Control", "no-cache");// CGI variable type "HTTP_Header"
response.setHeader("Content-Disposition","attachment; filename='exporttoexcelSSJS.xls'");// define this content type for your excel file to set the name and excel file as an attachment for  browsers

var viewEntry:NotesViewEntry = myViewNav.getFirst();// Set notes view in LS- set viewEntry=viewNav.GetFirst()
var thead:string = ""; // Declare thead variable as string LS- Dim thead as a String
var tr:string = ""; // Declare tr variable as string LS - Dim tr as a String

thead="<th>FirstName </th>" // define your table row header
thead=thead+"<th> LastName </th>"
thead=thead+"<th> DOB </th>"
thead=thead+"<th> City </th>"
thead=thead+"<th> Country </th>"

while (viewEntry != null) { // LS- While not ViewEntry is nothing

// Your data row will start from here

            tr=tr+"<tr>" // set your table row data
            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); // next view entry
}//Loop - end of while loop
DataWriter.write("<table border='1'>"+thead+tr+"</table>"); // build and write your table to document using write method
DataWriter.endDocument();
//End Code






Note: you can also paste this script directly on xPage source, before pasting this code add few lines of code on top of the script and bottom of the script

TOP Lines-

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse><![CDATA[#{javascript:

Middle Lines – ‘Export to Excel using Server Side JavaScript’ Code here


BOTTOM Lines-

}]]> // define end of the document
</xp:this.afterRenderResponse> // JSF page event uses to generate after rendering response, i.e. response from one action to another action
</xp:view>

How to call this script?

Go to your viewpanel and add a button control on top and name as you like, here we have added “Export to Excel using Server Side Java Script” to understand better. Refer below screen-








Now go to button control onclick event- paste below line on Server side Script Editor

facesContext.getExternalContext().redirect("./excelExport.xsp"); //This redirect usually used for external URL  

Or...

context.redirectToPage("./excelExport.xsp") // You can use this redirect URL , usually we use this method to move from one page to another page. It’s being used in internal URLs

Refer below screen-



Let’s see the output in excel file-



Thanks for reading this article, hope you like it, if you have any difficulty please contact us on ‘admin@xpagedomino.com’ or click here to contact us.






No comments :

Post a Comment

Leave A Comment...