dominoGuru.com

Your Development & Design Resource

One-click Microsoft Excel (or Symphony Spreadsheets) via XPages and SSJS

The View - Lotus Developer 2010, Boston: May 12-14, 2010 One of the goals of my View developer2010 session, Using MVC Architecture to Take Your XPage Applications to the Next Level, is to show you that XPages offer User Interface (or View) options for your IBM Lotus Notes Domino Applications far beyond a Lotus Notes Client or Web Browser Client UI component.

One of my examples used in the demo application illustrates this point by giving the user a click-to-export Button on the dashboard-style index.xsp XPage. Pretty slick stuff, especially when the entire XPage and SSJS source is 31 lines of code.

export.xsp XPage Source

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
  3.     <xp:this.afterRenderResponse><![CDATA[#{javascript:var exCon = facesContext.getExternalContext();
  4. var writer = facesContext.getResponseWriter();
  5. var response = exCon.getResponse();
  6. var projects:NotesView = database.getView('projects')
  7. var viewNav:NotesViewNavigator = projects.createViewNav();
  8. var viewEnt:NotesViewEntry = viewNav.getFirst();
  9. var output:string = "";
  10. while (viewEnt != null) {
  11.  output += "<tr>";
  12.  output += "<td>" + viewEnt.getColumnValues()[0]; + "</td>";
  13.  output += "<td>" + viewEnt.getColumnValues()[2] + "</td>";
  14.  output += "<td>" + viewEnt.getColumnValues()[1] + "</td>";
  15.  output += "<td>" + viewEnt.getColumnValues()[3] + "</td>";
  16.  output += "</tr>";
  17. viewEnt = viewNav.getNext(viewEnt);
  18. }
  19. response.setContentType("application/vnd.ms-excel");
  20. response.setHeader("Cache-Control", "no-cache");
  21. writer.write("<table>");
  22. writer.write("<thead><tr>");
  23. writer.write("<td><b>Project</b></td>");
  24. writer.write("<td><b>Developer</b></td>");
  25. writer.write("<td><b>Estimate ( Hours )</b></td>");
  26. writer.write("<td><b>Estimate ( Budget )</b></td>");
  27. writer.write("</tr></thead>");
  28. writer.write(output);
  29. writer.write("</table>");
  30. writer.endDocument();}]]></xp:this.afterRenderResponse>
  31. </xp:view>

A few things you'll notice right away...

  • Non-rendered XPage
    This is an example of the XPage XAgent stuff I've been talking about lately. As seen on Line 2, I set the XPage rendered parameter to false. We'll be using the afterRenderResponse to build our Excel export.

  • Defined NotesView
    On Line 6, I've defined projects as my target NotesView. You'll want to change this, as well as your intended export content (Lines 12-15) to match, well, whatever you're looking to export.

  • Controlling the rendered ContentType
    Line 19, where our magic happens... I simply set the ContentType to application/vnd.ms-excel, and allow the ResponseWriter to build my table.

Yep, a table. and it's supported by Microsoft, too.

Using your export.xsp XPage

Export Button in IBM Lotus Notes Domino Designer XPage Design Pane

It's pretty simple really, since our XPage does all of the heavy lifting for you.

Step 1: Add a button that opens your XPage

The above Export button runs the following event:

<xp:openPage name="/export.xsp"></xp:openPage>

Step 2: Open (or download) the resulting Excel spreadsheet

Microsoft Excel Spreadsheet Export from IBM Lotus Notes Domino XPages

Yeah, that's it.

You can run this in the Web Browser Client as well as the Lotus Notes Client (8.5.1 or greater).

(You will want to rename the export.xsp to something.xls when you run your Export...)

In Closing...

Using this technique, you will be able to rather easily export your NotesData to your productivity suite. You can modify the SSJS to include UI-driven parameters passed via session variables to give you even more control over what is being exported.

And, of course, if you liked this sample... I think you'll love my session at developer2010. Let me know if I'll see you there!


About the author: Chris Toohey

Thought Leadership, Web & Mobile Application Development, Solutions Integration, Technical Writing & Mentoring

A published developer and webmaster of dominoGuru.com, Chris Toohey specializes in platform application development, solutions integration, and evangelism of platform capabilities and best practices.



More from dominoGuru.com


dominoGuru.com is powered by IBM Notes Domino XPages & hosted by Prominic.NET

Contact Us

Use our Contact / Feedback form or one of these email addresses:

Creative Commons License

Except where otherwise noted, dominoGuru.com by Chris Toohey is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.