JExcelAPI unable to output excel to servlet for user download

import java.io.IOException;

import java.io.OutputStream;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

publicclass Sampleextends HttpServlet

{

publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

OutputStream out =null;

try

{

response.setContentType("application/vnd.ms-excel");

response.setHeader("Content-Disposition","attachment; filename=sampleName.xls");

WritableWorkbook w = Workbook.createWorkbook(response.getOutputStream());

WritableSheet s = w.createSheet("Demo", 0);

s.addCell(new Label(0, 0,"Hello World"));

w.write();

w.close();

}catch (Exception e)

{

thrownew ServletException("Exception in Excel Sample Servlet", e);

}finally

{

if (out !=null)

out.close();

}

}

}

This is the code segment posted on thier FAQ. But I am writing a generic excel export function to be use by multiple servlets. How can I do that?

[2595 byte] By [liangtehza] at [2007-11-27 11:19:40]
# 1

can you show me where the library jxl

so i can get it and try to run the prorgram...

QussayNajjara at 2007-7-29 14:38:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> unable to output excel to servlet for user download

What's happening then? Do you also see any errors in the log?

I know nothing about the jxl package, but your response headers have at least one shortcoming: the content length is not set. Some useragents / applications will refuse to open the file without saving the whole stream to disk then. Maybe this was happening.

BalusCa at 2007-7-29 14:38:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

pls if someone knew any information about this package, just tell me, because soon i'll be needing this too much in my project under construction now...

thx (^_^);

QussayNajjara at 2007-7-29 14:38:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

If I simply google on "jxl excel", I got lots of useful hints. Don't you?

BalusCa at 2007-7-29 14:38:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

I can read or create an excel file using this package but the problem is the main execution code is tied to the ServletOutputStream that does the writing allowing the user to save the file.

I need to create a generic class to create the workbook and return the object to Servlet class for download

liangtehza at 2007-7-29 14:38:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Let me re-phrase the WorkBook object only take in a File object and OutputStream for this I can't make use of it I can't pipe this to a servlet as I am writing in another class

liangtehza at 2007-7-29 14:38:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...