0

I read this great help 'tool' from David Leedy regarding Export to excel.:

http://notesin9.com/wp-content/uploads/2011/01/XPagesCheatSheet-85x11-v1.pdf

My scenario: I have a ftsearch modulo which offers its results into a viewpanel. I want ONLY those results to be exported into excel. I follow the steps from the .pdf but in that case it will export all the documents listed in the respective view, not the search results.

Is there any possibility to have only the search results into the excel file, after the search is submited?

var myView:NotesView = database.getView('MyView');

Here I get the view which holds the search results, but I want to get the view contents/those docs. after the Search button was submitted. The problem is I don't get the 'updated' view only with the search results, but all the DOCS contained in that view. ( Before the Search button was clicked ... )

Thanks for your time!

Florin M.
  • 2,159
  • 4
  • 39
  • 97

3 Answers3

4

You say that you already have a view panel that shows the search results. I'm assuming that you're using the search attribute on the view panel object to do that. Another assumption is that you're computing the full text query in the search attribute based on an input field that's bound to a scoped variable (possibly the sessionScope). So your datasource on the search XPage would look something like this:

<xp:this.data>
  <xp:dominoView
    var="view1"
    viewName="yourSearchView">
    <xp:this.search><![CDATA[#{javascript:"[subject]=\"" + sessionScope.searchField + "*\"";}]]></xp:this.search>
  </xp:dominoView>
</xp:this.data>

In the Export XPage you can restrict the entries that are exported by applying the same query to the view before exporting the results. To do that you can use the 'FTSearch()' function on the view:

myView.FTSearch("[subject]=\"" + sessionScope.searchField + "*\");

Based on the code on David's cheatsheet I tested this and it turns out that for some reasons if you add this row, the NotesViewNavigator that is constructed based on the (filtered) view isn't limited to the search results, but still contains all entries. The solution to that is to remove the NotesViewNavigator object and use a NotesViewEntryCollection:

var exCon = facesContext.getExternalContext(); 
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();

var myView:NotesView = database.getView('vwKlanten');
var query = "[subject]=\"em*\"";
myView.FTSearch(query);

var vec:NotesViewEntryCollection = myView.getAllEntries()

var viewEnt:NotesViewEntry = vec.getFirstEntry();

response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "no-cache");

writer.write("<table>");
writer.write("<thead><tr>");
writer.write("<td><b>Column1Header</b></td>");
writer.write("<td><b>Column2Header</b></td>");
writer.write("<td><b>Column3Header</b></td>");
writer.write("</tr></thead>");

while (viewEnt != null) {

 writer.write("<tr>");
 writer.write("<td>" + viewEnt.getColumnValues()[0] + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[1] + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[2] + "</td>");
 writer.write("</tr>");

 var tmp = vec.getNextEntry(viewEnt);
 viewEnt.recycle();
 viewEnt = tmp;
}

writer.write("</table>");
writer.endDocument();

(BTW: I added a 'recycle()' call to deal with possible memory errors)

Mark Leusink
  • 3,747
  • 15
  • 23
  • Thanks for input. My query in the search attribute is computed based on some input fields bounded to session variables. The problem is I don't know how to copy the view data source from the search XPage to the Export xpage. An example of this would help me a lot. – Florin M. May 19 '14 at 07:57
  • I've updated my answer with some sample code on how your view data source would probably look on your search XPage. That's the piece of code that you need to copy to the Export XPage. – Mark Leusink May 19 '14 at 08:02
  • I've already have the search code for the view. I don't know how to copy to the the Export xpage – Florin M. May 19 '14 at 08:12
  • Did some tests with it. You don't need to copy the data source to the Export page: after retrieving the view you can apply the same query to the entries as on the search XPage. See the code sample I've added. – Mark Leusink May 19 '14 at 08:33
  • Regarding the query variable: If in the search attribute I create the quering with having as input 10 fields, how can I declare it in the Export xpage? I copied the exactly code from the search attribute to obtain the qstring but in the excel i get ONLY the first document. – Florin M. May 19 '14 at 11:04
  • I just corrected an error in my code sample: the line that starts with `var tmp =` was incorrect. That could explain that you only get 1 result in the Excel file. – Mark Leusink May 19 '14 at 12:09
1

Have You already tried http://poi4xpages.openntf.org/ ? A custom control for exporting data to excel or word

umeli
  • 1,068
  • 8
  • 14
1

For me, rather then trying to get ahold of the search results from the code that produces the report, I'd just have that code run the same search again. At least to get it working. Then I'd deal with trying to cut it back to 1 search call. You might find that running 2 searches just isn't a big deal.

David Leedy
  • 3,583
  • 18
  • 38
  • I just saw this blog post. Came out today. Sounds like it might be interesting to you. http://lostinxpages.com/2014/05/15/exporting-to-excel-using-queries-in-xpages/ – David Leedy May 15 '14 at 14:20
  • Thanks for your response. So in my 'Excel' button I would do another submit for the view and then open the excel xpage with the code from the cheatseet? unfortunately, i already tried this, and all documents appears in the excel document – Florin M. May 19 '14 at 05:07