Solved

Export to Excel or CSV from a JSP application

Posted on 2007-04-04
8
8,831 Views
Last Modified: 2008-01-09
Hi there

I have a simple JSP application that displays reports from data from a SQL database. I want to give the user the option of exporting the results to an excel or csv file by pressing a button. Is there any simple way of doing this?

The sql query is stored in a string variable 'sql' if that would help things a bit.

Cheers
0
Comment
Question by:AndriesKeun
  • 4
  • 3
8 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 18852166
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18864292
You can use an Excel library to export to Excel:

http://api.openoffice.org
0
 

Author Comment

by:AndriesKeun
ID: 18889510
Is there an easy way to find the desktop path of the user using the application?
0
 

Author Comment

by:AndriesKeun
ID: 18904289
I did this, however now i realise that it only saves the file to the pc where the application is running from, and not the user that is using the appplication. Any idea how i can change it?

<%@ page language="java" import="java.sql.*,java.sql.Time,java.util.Date,java.text.SimpleDateFormat" errorPage="error.jsp" %>
<%@ include file="CheckSession.jsp" %>
<%@ include file="conn.jsp" %>
<%@ page import="java.io.*"  %>

<%
SimpleDateFormat sdf_new = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf_new.format(new Date());

String userDir = System.getProperty("user.home");
out.println(userDir);

double r;
int ir = -1;
           
while (ir % 2 != 0) {
    r = Math.random();
    ir = (int) ((9999 - 1000 + r) * r + 1000);
}

String query = (String)session.getAttribute("tempSql");
String nameOfTextFile = "c:\\CallReport_";

nameOfTextFile += date;
nameOfTextFile += "_";
nameOfTextFile += ir;
nameOfTextFile += ".csv";

PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
String header = "Incoming/Outgoing,Call Date,Call Time,Call Length,Incoming Extension,Receiving Extension,Receiving/Calling Name,Number Called/Received";
pw.println(header);

try {
      Driver DriverRecordset = (Driver)Class.forName(_driver).newInstance();
      con = DriverManager.getConnection(_url, _uid,_pwd);
       stmt = con.createStatement();
      rs = stmt.executeQuery(query);

      while (rs.next()){      

            String line = rs.getString("c.inout") + "," + rs.getString("c.calldate") + "," + rs.getString("c.calltime")  + "," + rs.getString("c.calllength") + "," + rs.getString("c.incoming")  + "," + rs.getString("c.receiving")  + "," + rs.getString("e.Name") + "," + rs.getString("c.number");
            pw.println(line);
            }
      
      con.close();
}

catch (SQLException sqle) {
       out.println("SQLException" + sqle.toString());
}

pw.close();

%>

<table>
      <tr><td>The report has been saved on your desktop. Click 'Ok' to close this window.</td></tr>
      <tr><td><input type=button value="Ok" onClick="logout()"></td></tr>
</table>

<script language="Javascript">

function logout(){

      self.close();

}

</script>
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 30

Expert Comment

by:mayankeagle
ID: 18928421
This scriplet <% %> code runs on the server side - so the file is saved there. Instead of C:\ you can save the file within the context root of the web application and give a link <a href/> to the file so that the user can download it.
0
 

Author Comment

by:AndriesKeun
ID: 18930179
Clever, didnt think of that. Will try it, thanks.
0
 

Author Comment

by:AndriesKeun
ID: 18930349
Only one problem... because its a csv it opens up in the browser and does not download it, how do i go about doing that?
0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 500 total points
ID: 18985103
Try:

res.setContentType ( "application/download" ) ;
res.setHeader ( "Content-Disposition", "inline;filename=myFile.txt" ) ;
res.setContentLength ( ( int ) file.length () ) ;

Or try using a Download servlet: http://jspwiki.org/wiki/MakingADownloadServlet

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Stuck in Bootstrap WysiHtml Editor 3 36
JSP Popup page 2 95
parse example 13 86
best way to search/remove a file from an EAR file 3 96
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now