Question

I have a button that exports a view to Excel from an Xpage. It works fairly well, but when the user opens up Excel there are no grid lines and I know they are going to freak out. I have looked for an hour on the internet for how to turn these one but I can't find how.

Does anyone know how to do this?

Here is my code"

    var output:string = "";
    for(i=0;i < sessionScope.searchDocIDArray.length; i++)
    {
    var docId=sessionScope.searchDocIDArray[i];
    var doc=database.getDocumentByID(docId);

    output += "<tr>";
    output += "<td>" + doc.getItemValueString("user") + "</td>";
    output += "<td>" + doc.getItemValueString("loc") + "</td>";
    output += "<td>" + doc.getItemValueString("date") + "</td>";
    output += "<td>" + doc.getItemValueString("workCategory") + "</td>";
    output += "<td>" + doc.getItemValueString("state") + "</td>";
    output += "<td>" + doc.getItemValueString("timeSpent") + "</td>";
    output += "<td>" + doc.getItemValueString("notes") + "</td>";

    output += "</tr>";
    }

    facesContext.getExternalContext().getResponse().setHeader("Content-disposition", "attachment; filename=TSC Time Spent.xlxs");
    facesContext.getExternalContext().getResponse().setHeader("Cache-Control", "no-cache");

    facesContext.getResponseWriter().write("<x:WorksheetOptions>")
    facesContext.getResponseWriter().write("<x:Panes>");
    facesContext.getResponseWriter().write("</x:Panes>");
    facesContext.getResponseWriter().write("<x:WorksheetOptions>")

    facesContext.getResponseWriter().write("<table><thead><tr><td><b>User</b></td><td><b>Loc</b></td><td><b>Date</b></td><td><b>Work Category</b></td><td><b>Time Spent</b></td><td><b>Notes</tr></thead>"+output+"</table>");
    facesContext.getResponseWriter().endDocument();
Was it helpful?

Solution

One way to make sure grid lines are intact is to export to csv instead of using html tags to write out your content.

You also can assign css to your to td elements but the borders will not look like they do natively in excel.

Here is an example of using csv. This is a simplified version of something I used in afterRenderResponse:

var exCon = facesContext.getExternalContext();
var writer = facesContext.getResponseWriter();
var response = exCon.getResponse();
var output = "";
var colHeaders = "col1,col2,col3,col4,col5";
// Loop through data set
while (doc != null) {

output+="\"" + val1 + "\",";
output+="\"" + val2 + "\",";
output+="\"" + val3 + "\",";
output+="\"" + val4 + "\",";
output+="\"" + val5 + "\",";
output += @Char(13)+@Char(10);  // start a new row
}
response.setContentType("application/csv-tab-delimited-table;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Disposition", "attachment;filename=actionExport.csv");
writer.write(colHeaders+@Char(13)+@Char(10)); // Add the first row as column titles and add line feed
writer.write(output+@Char(13)+@Char(10));  //
writer.endDocument();

OTHER TIPS

It's going to take a bit more investigation, but I would recommend looking at using Apache POI for writing to Excel. It won't throw security alerts with Excel 2007+ like the table approach does. It has more specific APIs for setting styling. The HSSF classes are the ones you want (standing for Horrible SpreadSheet Format!).

Try adding this :-)

<x:WorksheetOptions>
  <x:DisplayGridlines/>
</x:WorksheetOptions>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top