Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpages, save a view into excel file

Tags:

excel

view

xpages

I want to save a search report into an excel file but it is saving with the .xsp extension not excel.

I use the following code:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse><![CDATA[#{javascript: 
var exCon = facesContext.getExternalContext(); 
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
var myView:NotesView = database.getView('vwSearch');
var viewNav:NotesViewNavigator = myView.createViewNav();
var viewEnt:NotesViewEntry = viewNav.getFirst();
print("2")
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("<td><b>Column4Header</b></td>");
writer.write("<td><b>Column5Header</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("<td>" + viewEnt.getColumnValues()[3] + "</td>");
writer.write("<td>" + viewEnt.getColumnValues()[4] + "</td>");
writer.write("</tr>");
viewEnt = viewNav.getNext(viewEnt);
}
writer.write("</table>");
writer.endDocument()}]]></xp:this.afterRenderResponse>
</xp:view>

I use this code from a pdf... It is not my own creation, maybe something is wrong.

Thanks,

Florin

like image 864
Florin Pop Avatar asked Mar 18 '23 16:03

Florin Pop


2 Answers

Add a line

response.setHeader("Content-Disposition", "attachment; filename=yourfile.xls");

or

response.setHeader("Content-Disposition", "inline; filename=yourfile.xls");

With "inline", the browser will try to open the file within the browser, whereas "attachment" will force it to download.

like image 103
Knut Herrmann Avatar answered Mar 23 '23 12:03

Knut Herrmann


What is wrong with this?

var exCon = facesContext.getExternalContext(); 
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
var myView:NotesView = database.getView('vwSearch');
myView.FTSearch("sessionScope.search");
var viewNav:NotesViewNavigator = myView.createViewNav();
var viewEnt:NotesViewEntry = viewNav.getFirst();

response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=raport.xls");
writer.write("<table>");
writer.write("<thead><tr>");
writer.write("<td><b>Nume</b></td>");
writer.write("<td><b>Ora Intrare</b></td>");
writer.write("<td><b>Ora Iesire</b></td>");
writer.write("<td><b>Timp</b></td>");
writer.write("<td><b>Timp 2</b></td>");
writer.write("<td><b>Data Creare</b></td>");
writer.write("<td><b>Email</b></td>");
writer.write("<td><b>Telefon</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].toJavaDate().getHours()+":"+viewEnt.getColumnValues()[1].toJavaDate().getMinutes() + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[2].toJavaDate().getHours()+":"+viewEnt.getColumnValues()[2].toJavaDate().getMinutes() + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[3] + "</td>");
 writer.write("<td>" + "08:00" + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[4].toJavaDate() + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[5] + "</td>");
 writer.write("<td>" + viewEnt.getColumnValues()[6] + "</td>");
 writer.write("</tr>");
 viewEnt = viewNav.getNext(viewEnt);
}
writer.write("</table>");
writer.endDocument()

Even if I use: myView.FTSearch("Jeff"); it will not show only documents with Jeff... Why?

like image 26
Florin Pop Avatar answered Mar 23 '23 13:03

Florin Pop