• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Open CSV file in Excel not IE

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone please solve this problem
Thanks
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic