Search code examples
xpagesexport-to-excel

XPages Create Excel in SSJS


I would likte to create an excel file in SSJS. It will be the same in LotusScript (You can find the code sample at the bottom of this question.) I started this but i could not finish it yet :(

var response = facesContext.getExternalContext().getResponse(); 
var DataWriter = facesContext.getResponseWriter(); 
response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition","attachment; filename='ExportedExcel1.xls'");

DataWriter.write("Denenee");
DataWriter.endDocument();



xlsFile =Environ("Temp") & "\firstExcel.xls"
Call object.ExtractFile( xlsFile )
Set excelappl = Nothing
Set excelappl = CreateObject("Excel.Application")
ver$ = excelappl.Version    
Select Case Left(ver$,2)
Case "9.":
Set excelappl = CreateObject( "Excel.Application.9" )       
Case "10":
Set excelappl = CreateObject( "Excel.Application.10" )      
Case "11":
Set excelappl = CreateObject( "Excel.Application.11" )      
Case "12":
Set excelappl = CreateObject( "Excel.Application.12" )  
Case "13":
Set excelappl = CreateObject( "Excel.Application.13" )  
End Select
excelappl.visible = False
Set wb = excelappl.Workbooks.Open(xlsFile)
If wb Is Nothing Then
Error 10003, "Hata: Dosya bulunamadı."
End If
Set excelSheet = excelappl.workbooks(1).Worksheets(1)
Call excelsheet.Activate
row = 1

Solution

  • XPages' SSJS runs on server. It's not a good idea to use OLE objects like LotusScript's CreateObject("Excel.Application") on server.

    Use POI 4 XPages instead. It allows you to create Excel files in an approved way.