Xpages,将视图保存到excel文件中

时间:2014-08-25 05:23:00

标签: excel view xpages

我想将搜索报告保存到excel文件中,但保存时.xsp扩展名不是excel。

我使用以下代码:

<?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>

我从pdf中使用这段代码......这不是我自己的创作,也许是错误的。

谢谢,

3 个答案:

答案 0 :(得分:6)

添加一行

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

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

使用&#34; inline&#34;,浏览器将尝试在浏览器中打开文件,而&#34;附件&#34;将强行下载。

答案 1 :(得分:1)

这有什么问题?

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()

即使我使用:     myView.FTSearch(&#34;杰夫&#34); 它不会只显示杰夫的文件...为什么?

答案 2 :(得分:0)

不确定您是否想要一步到位地执行搜索和导出,但您始终可以修改视图选择公式并刷新视图。然后为用户提供导出按钮,以按照Knut所说的修改当前代码的步骤。我在之前的项目中完成了这项工作,我在Excel中打开导出并简单地保存为逗号分隔文件。在另一个项目中,我调用了一个Xagent,它在beforeRenderResponse中有代码,在单击按钮后在Excel中打开。只是其他选择。