AndriesKeun
asked on
Export to Excel or CSV from a JSP application
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
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
ASKER
Is there an easy way to find the desktop path of the user using the application?
ASKER
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.sq l.Time,jav a.util.Dat e,java.tex t.SimpleDa teFormat" errorPage="error.jsp" %>
<%@ include file="CheckSession.jsp" %>
<%@ include file="conn.jsp" %>
<%@ page import="java.io.*" %>
<%
SimpleDateFormat sdf_new = new SimpleDateFormat("dd-MM-yy yy");
String date = sdf_new.format(new Date());
String userDir = System.getProperty("user.h ome");
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.getAttribu te("tempSq l");
String nameOfTextFile = "c:\\CallReport_";
nameOfTextFile += date;
nameOfTextFile += "_";
nameOfTextFile += ir;
nameOfTextFile += ".csv";
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTex tFile));
String header = "Incoming/Outgoing,Call Date,Call Time,Call Length,Incoming Extension,Receiving Extension,Receiving/Callin g Name,Number Called/Received";
pw.println(header);
try {
Driver DriverRecordset = (Driver)Class.forName(_dri ver).newIn stance();
con = DriverManager.getConnectio n(_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>
<%@ page language="java" import="java.sql.*,java.sq
<%@ include file="CheckSession.jsp" %>
<%@ include file="conn.jsp" %>
<%@ page import="java.io.*" %>
<%
SimpleDateFormat sdf_new = new SimpleDateFormat("dd-MM-yy
String date = sdf_new.format(new Date());
String userDir = System.getProperty("user.h
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.getAttribu
String nameOfTextFile = "c:\\CallReport_";
nameOfTextFile += date;
nameOfTextFile += "_";
nameOfTextFile += ir;
nameOfTextFile += ".csv";
PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTex
String header = "Incoming/Outgoing,Call Date,Call Time,Call Length,Incoming Extension,Receiving Extension,Receiving/Callin
pw.println(header);
try {
Driver DriverRecordset = (Driver)Class.forName(_dri
con = DriverManager.getConnectio
stmt = con.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()){
String line = rs.getString("c.inout") + "," + rs.getString("c.calldate")
pw.println(line);
}
con.close();
}
catch (SQLException sqle) {
out.println("SQLException"
}
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></
</table>
<script language="Javascript">
function logout(){
self.close();
}
</script>
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.
ASKER
Clever, didnt think of that. Will try it, thanks.
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://www.experts-exchange.com/questions/21876018/creating-csv-file.html