Simple (and limited) Xpages export to Excel with no external dependencies


//THE JAVA BEAN

package com.data101.managed;

import java.io.Serializable;
import java.util.Vector;

public class ExcelDataExport implements Serializable {

	private final String NEW_LINE	=	 "NL";
	
	private static final long serialVersionUID = 1L;
	
	Vector<String> data;
	
	String fileName;
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName= fileName;
	}

	public ExcelDataExport(){
		this.data = new Vector<String>();
	}
	
	public void clear(){
		this.data = new Vector<String>();
	}
	
	public void addData(String cellData){
		this.data.add(cellData);
	}
	
	public void newLine(){
		this.data.add(this.NEW_LINE);
	}
	
	
	public String getData(){
		StringBuilder builder = new StringBuilder();
		
		boolean firstLine= true;
		
		builder.append("<table><tr>");
		
		for (Integer i = 0; i < this.data.size(); i++)
        {
			if(this.data.get(i).equals(this.NEW_LINE)){
				if(firstLine){
					firstLine= false;					
				}
				if(this.data.size()>i+1){
					builder.append("</tr>");
				}else{
					builder.append("</tr><tr>");
				}
				
			}else{
				if(firstLine){
					builder.append("<th>");
		                        builder.append(this.data.get(i));
		                        builder.append("</th>");
				}else{
					builder.append("<td>");
		                        builder.append(this.data.get(i));
		                        builder.append("</td>");
				}
				if(this.data.size() == i+1){
					builder.append("</tr>");
				}
			}
            
            
        }
		
		builder.append("</table>");
		
		return builder.toString();
	}
}


// THE XAGENT( Xpage )

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false" viewState="nostate">

	<xp:this.afterRenderResponse><![CDATA[#{javascript:var exCon = facesContext.getExternalContext();

var writer = facesContext.getResponseWriter();

var response = exCon.getResponse();

response.setHeader("Content-Disposition","attachment; filename=" + excelExportBean.getNombre());
response.setContentType("application/x-excel");
response.setHeader("Cache-Control", "no-cache");


writer.write('<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>' + excelExportBean.getFileName() + '</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>' + excelExportBean.getData() + '</body></html>');

writer.endDocument();}]]></xp:this.afterRenderResponse>
</xp:view>

//Facesconfig file

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
  <managed-bean>
    <managed-bean-name>excelExportBean</managed-bean-name>
    <managed-bean-class>com.data101.managed.ExcelDataExport</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>
  <!--AUTOGEN-START-BUILDER: Generado automáticamente por HCL Domino Designer. No lo modifique.-->
  <!--AUTOGEN-END-BUILDER: Fin de la sección generada automáticamente-->
</faces-config>


//EXAMPLE SSJS /JAVA

ExcelExport.clear();
ExcelExport.addData("Cab1");
ExcelExport.addData("Cab2");
ExcelExport.addData("Cab3");
ExcelExport.newLine();
ExcelExport.addData("Dato1");
ExcelExport.addData("Dato2");
ExcelExport.addData("Dato3");

//Just open new window or redirect to the defined xpage.
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...