aspose file tools*
The moose likes JSP and the fly likes Open CSV file in Excel not IE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Open CSV file in Excel not IE" Watch "Open CSV file in Excel not IE" New topic
Author

Open CSV file in Excel not IE

Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Hi

I am generating a report using jasper report. When the report gets generated and then user clicks the link then for pdf and html things are fine but for csv file I want the report to be opened in Excel rather than IE but it always opens it in IE with all commas and everything. How can I make the file open in excel not IE. I have tried the method of folder options.
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

You need to properly set the content type and disposition headers when you send the report response to the browser. Search this forum, and perhaps the Servlets forum, as well as google, for terms like "content-disposition" and you will find examples of setting these headers.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is an example of this in the JSP faq
http://faq.javaranch.com/view?JspAndExcel


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Hi Bear

Thanks for your reply. What is happening right now is it is opening the jsp page contents but not displaying the report contents.

Here is my code that does this

<HEAD>
</HEAD>
<BODY>

<% if(( (GeneratingReport)request.getSession().getAttribute("Generating")).getGenerating() == true){
response.setHeader("Refresh","3; URL=/r911/report/rptPreview.jsp");
JspWriter output = pageContext.getOut();
output.write("Processing Report...");
}
else
{
if( ((Boolean)request.getSession().getAttribute("EmptyReport")).booleanValue() == false)
{
String fileName = (String)session.getAttribute("fileName");

StringBuffer path = new StringBuffer("/r911/report/tmp/");
path.append(fileName);
if ( fileName.contains ( "csv" ) )
{
response.setContentType ( "application/vnd.ms-excel" ) ;
response.setHeader ( "Content-Disposition", "attachment;filename=" + path );
}
%>
<h3>Processing Complete</h3>
<a href=<%=path.toString()%> target='_self' >
View the report </a>



<% }
else
{
%>
<h4> Empty Report </h4>
Requested Report has no Data
<%
}
}
%>
</BODY>

I really appreciate your help.
Thanks
Imad
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
I am sorry I mentioned to tell that it is opening the jsp page "reportpreview.jsp" when but not the report in csv format. but if report type is pdf then when i click "View Report" then report is displayed in pdf format.
Thanks
Imad
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Hi Bear

What I am dping is generating a report and storing it in /r911/report/tmp/myReport.csv

Now what I want is either the report opens automatically or when the user clicks View Report link the report should open in excel not in IE. right now it opens in IE and displays all commas and everything.

By adding content disposition header and seContent type the jsp page I am using itself opens in excel which i dont want.
Your help is really appreciated.
Thanks
Imad
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

That's a different matter. When hitting a direct link, the settings in the browser will determine how it is handled. There is not much you can do from the server end of things to affect that behavior.
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
can someone please solve this problem
Thanks
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Hi Bear

Is there a way using javascript penDoc or something like that?

i m talking about a builtin function openDoc.
Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

One option (server side) is to stream the PDF yourself.
If you do this, you'll be able to set whatever headers you like.
You'll want to do this from a servlet (not a JSP).

If you would like an example, I have one on my site:
http://simple.souther.us
Look for SimpleStream.

In this app, I'm streaming images but the code would be the same for PDFs or XSL files.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Ben is correct. If you just provide a link to the file you are at the mercy of the browser MIME settings, which you cannot affect from the server. If you want complete control over the process, you need to stream it yourself.
Muhammad Imad Qureshi
Ranch Hand

Joined: Sep 13, 2005
Posts: 238
Thanks a lot. With the help of you guys I have solved the problem.
Here is what I did in my code (java code)

public void generateCSVReport ( HttpSession session, HttpServletRequest request,
HttpServletResponse response)
{
try
{
JasperReportDTO jasperReportDTO = (JasperReportDTO) session
.getAttribute("jasperDTO");
ReportDelegate reportDelegate = new ReportDelegate();
ReportDTO reportDTO = reportDelegate.createReport(jasperReportDTO);

FileInputStream fIn = new FileInputStream(reportDTO.getReportFile());

ServletOutputStream servletOut = response.getOutputStream() ;

response.setContentType("application/csv");
response.setHeader("Content-Disposition",
"attachment; filename=\"r911.csv\"");
response.setContentLength(fIn.available());

int ch;
while ((ch = fIn.read()) != -1) {
servletOut.write((char) ch);
}
servletOut.flush();
servletOut.close();
fIn.close();

reportDTO.getReportFile().delete();
}

catch (Exception exception)
{
exception.printStackTrace() ;
}
}

I am pasting this code so just in case it might help someone else in future.
Thank you so much Bear and Ben both.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Open CSV file in Excel not IE