write Excel through servlet

F

finecur

Here is my servlet:

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{

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

response.addHeader("Pragma", "no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader ("Expires", 0);


PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<table border=1>");
out.println("<tr>");
out.println("<th>A</th>");
out.println("<th>B</th>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}

When I call it from http://www.mydomain.com/myservlet.do?mypara=1 in
IE 7, it says

"Do you want to open or save this file?
Name:myservlet_do?mypara=1
Type:Microsoft Ecel Worksheet, 733
Form:www.mydomain.com
"

Then I click "Open",

but it says" Can not open the file", and the IE page went black.

Does anyone know why?

Thanks,

ff
 
L

LuckyBoy

This is a Bug in IE Header. Search Sun discussion forums and you will
get the answer. Did you try in Firefox or Other browser ? What does it
say then ?
 
S

Sanjay

finecur said:
Here is my servlet:

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{

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

response.addHeader("Pragma", "no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader ("Expires", 0);


PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<table border=1>");
out.println("<tr>");
out.println("<th>A</th>");
out.println("<th>B</th>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}

When I call it from http://www.mydomain.com/myservlet.do?mypara=1 in
IE 7, it says

"Do you want to open or save this file?
Name:myservlet_do?mypara=1
Type:Microsoft Ecel Worksheet, 733
Form:www.mydomain.com
"

Then I click "Open",

but it says" Can not open the file", and the IE page went black.

Does anyone know why?

Thanks,

ff
I had exact same problem couple of years ago. I guess you code works
fine with Firefox or mozilla. Because your cache expires in 0 seconds,
what IE does in my opinion is downloads the file and because it expires
in 0 seconds deletes it and now can not open it.

Try setting "expires" header to some value like 2 minutes etc. That
should work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top