Datasheet
Patrick c01.tex V3 - 09/18/2009 12:15pm Page 29
Chapter 1: Building Web Applications in WebLogic
{
PrintWriter out = response.getWriter();
response.setContentType(CONTENT_TYPE_EXCEL);
out.print("<table border=1>");
out.print("<tr>");
out.print("<td> </td>"); // empty cell in upper corner
for (int jj = 1; jj <= 10; jj++) {
out.print("<td><b>" + jj + "</b></td>");
}
out.print("</tr>");
for (int ii = 1; ii <= 10; ii++) {
out.print("<tr>");
out.print("<td><b>" + ii + "</b></td>");
for (int jj = 1; jj <= 10; jj++) {
out.print("<td>" + (ii * jj) + "</td>");
}
out.print("</tr>");
}
out.print("</table>");
}
}
You can also use JSP pages to create spreadsheets with one complication: The output of a JSP page often
contains many unintended newline characters caused by extra whitespace around directives and script-
let tags, making it difficult to control the spreadsheet formatting when using simple tab and newline
techniques. HTML formatting similar to the
FancyExcelServlet
works better in JSP pages used to create
spreadsheets. Listing 1-6 presents the JSP equivalent to the
FancyExcelServlet
.
Listing 1-6: FancyExcelPage.jsp.
<% response.setContentType("application/vnd.ms-excel"); %>
<html>
<body>
<table border=1>
<tr>
<td> </td>
<% for (int jj = 1; jj <= 10; jj++) { %>
<td><b><%= jj %></b></td>
<% } %>
</tr>
<% for (int ii = 1; ii <= 10; ii++) { %>
<tr>
<td><b><%= ii %></b></td>
<% for (int jj = 1; jj <= 10; jj++) { %>
<td><%= (ii * jj) %></td>
<% } %>
</tr>
<% } %>
</table>
</body>
</html>
29