Charanjeet Singh
Charanjeet Singh

Reputation: 11

Ambiguous rows while exporting data from Xpages to Excel

When I am exporting the data to excel in Xpages from a view, first few rows of the excel shows just angle brackets and then the real data.. Below is the code snippet.

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

response.setContentType("application/vnd.ms-excel");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition","attachment; filename=export.xls");

writer.write("<html>");
writer.write("<head>");
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>")
writer.write("</head>");
writer.write("<body>");
// set-up the table header
writer.write("<table>");
writer.write("<thead><tr>");

//Loop over view columns
var totalColumns = vwCurrent.getColumnCount();
for(var x=1;x<=totalColumns;x++) {
var column:NotesViewColumn = vwCurrent.getColumn(x);
writer.write("<td><b>" + column.getTitle() + "</b></td>");
}
writer.write("</tr></thead>");

// loop returned docs
var docCurrent:NotesDocument = vwCurrent.getFirstDocument();

//Variables for Multivalued Columns
var multiValueColSize = 0;
var multiValueColIndex = 1;  //Initialized to 1 as first value is just an indicator

while( null != docCurrent){
    var docNext:NotesDocument = vwCurrent.getNextDocument( docCurrent);

    // output data...
    writer.write("<tr>");
    // get columns, use view to format data ;-)
    var vectColumns:java.util.Vector = docCurrent.getColumnValues();
    for( intIndex = 0; intIndex < vectColumns.size(); intIndex++){
        var colValue = vectColumns.get( intIndex);
        /*Calculated columns*/
        switch(@Left(colValue,4)) {
        case "$PO$":
            var data = colValue.split(";");
            colValue = getStatusDate(data[1], data[2]);
            break;

        case "$MO$":
            var data = colValue.split(";");
            // Condition to ensure that, multivalued column with maximum size is set as multiValueColSize 
            if(multiValueColSize < data.length)
                multiValueColSize = data.length;
            //condition to ensure we do not get arrayindexOutofBond exception
            if (multiValueColIndex < data.length){  
                colValue = data[multiValueColIndex].split("##")[0];
            } else {
                colValue = "";
            }
            break; 
        }
        writer.write("<td>" + colValue + "</td>");
    }   
    writer.write("</tr>");

    if (multiValueColSize == 0 || ((multiValueColSize-1) == multiValueColIndex)){
        // re-assign next
        docCurrent.recycle();
        docCurrent = docNext;
        // Re-initialize variables for Multivalued Columns
        multiValueColSize = 0;
        multiValueColIndex = 1;
    } else {
        //increment the index by 1 to populate next values in the list
        multiValueColIndex = multiValueColIndex + 1;
    }
}

// close the table and the document
writer.write("</table>");

writer.write("<body>");
writer.write("</html>");
writer.endDocument()

Snipped of Excel

Where am I doing wrong ? Any clues ?

Upvotes: 1

Views: 77

Answers (2)

Charanjeet Singh
Charanjeet Singh

Reputation: 11

It is the multi value field which was causing an issue. When seen in a view it was coming out to be comma separated properly but when the data was written to excel, the same was causing an issue. Hence I used @Implode in the view and the error got solved.

Upvotes: 0

Fredrik Norling
Fredrik Norling

Reputation: 3484

I would use the poi4xpages project on openntf istead of trying to print out html excel file. Some versions of Excel will give errors if this is used.

https://poi4xpages.openntf.org/

Upvotes: 1

Related Questions