简体   繁体   English

从Xpages视图导出到Excel并保留网格线

[英]Export from Xpages view to Excel and keep grid lines

I have a button that exports a view to Excel from an Xpage. 我有一个按钮,可以从Xpage导出视图到Excel。 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. 它运行得相当好,但是当用户打开Excel时,没有网格线,我知道它们会吓坏。 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();

One way to make sure grid lines are intact is to export to csv instead of using html tags to write out your content. 确保网格线完整的一种方法是导出到csv而不是使用html标记来写出您的内容。

You also can assign css to your to td elements but the borders will not look like they do natively in excel. 您也可以将css分配给您的td元素,但边框看起来不像它们在excel中原生的那样。

Here is an example of using csv. 以下是使用csv的示例。 This is a simplified version of something I used in afterRenderResponse: 这是我在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();

It's going to take a bit more investigation, but I would recommend looking at using Apache POI for writing to Excel. 这将需要更多的调查,但我建议使用Apache POI写入Excel。 It won't throw security alerts with Excel 2007+ like the table approach does. 它不像表格方法那样使用Excel 2007+抛出安全警报。 It has more specific APIs for setting styling. 它有更多用于设置样式的特定API。 The HSSF classes are the ones you want (standing for Horrible SpreadSheet Format!). HSSF类是你想要的(代表可怕的SpreadSheet格式!)。

Try adding this :-) 试着添加这个:-)

<x:WorksheetOptions>
  <x:DisplayGridlines/>
</x:WorksheetOptions>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM