Below is the code which can be used to generate dynamic csv file and download the same.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd">
<%@ page import="com.zaax.connections.*
" import="java.util.*"
import="java.sql.*" import="java.io.IOException"
import="java.io.PrintWriter" import="java.io.*" %>
<%@ page import= "oracle.jdbc.*"%>
<%@ page import=" oracle.jdbc.pool. OracleDataSource"%>
<%@ page import=" oracle.jdbc.OracleDriver"%>
<%@ page import="com.csvreader. CsvWriter" %>
<%
int rptno = (Integer.parseInt(request. getParameter("rptno")!=""? request.getParameter("rptno"): "0"));
Connectioncon=null;
ResultSet rs= null;
try{
//WRITE CONNECTION CODE HERE
//CONNECTION CODE END
CallableStatement stmt=con.prepareCall("{ call PKGNAME.PROCNAME(?,?,?,?,?) }");
stmt.setInt(1, 0);
stmt.setInt(2, 2);
stmt.setInt(3, rptno);
stmt.setInt(4, 1);
stmt.registerOutParameter(5, OracleTypes.CURSOR);
stmt.execute();
rsnew = (ResultSet)stmt.getObject(5);
OutputStream os = response.getOutputStream();
CsvWriter csvOutput = new CsvWriter(new OutputStreamWriter(os), ',');
ResultSetMetaData rsMetaData = rsnew.getMetaData();
int numberOfColumns =rsMetaData.getColumnCount();
for (int i = 1; i < numberOfColumns + 1; i++) {
csvOutput.write(rsMetaData. getColumnName(i));
}
csvOutput.endRecord();
while (rsnew.next()) {
for(int i=1;i<=numberOfColumns;i++)
{
csvOutput.write(rsnew. getString(i));
}
csvOutput.endRecord();
}//while
response.setContentType(" application/csv");
response.setHeader("Content- Disposition","attachment; filename=Report"+rptno+".csv") ;
csvOutput.close();
rsnew.close();
out.clear();
out=pageContext.pushBody();
}//try
catch(Exception e)
{
System.out.println("error"+e. getMessage());
}
finally
{
try{
//Connection close
}
catch(Exception e){
System.out.println("error"+e. getMessage());
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/
<%@ page import="com.zaax.connections.*
import="java.sql.*" import="java.io.IOException"
import="java.io.PrintWriter" import="java.io.*" %>
<%@ page import= "oracle.jdbc.*"%>
<%@ page import=" oracle.jdbc.pool.
<%@ page import=" oracle.jdbc.OracleDriver"%>
<%@ page import="com.csvreader.
<%
int rptno = (Integer.parseInt(request.
Connectioncon=null;
ResultSet rs= null;
try{
//WRITE CONNECTION CODE HERE
//CONNECTION CODE END
CallableStatement stmt=con.prepareCall("{ call PKGNAME.PROCNAME(?,?,?,?,?) }");
stmt.setInt(1, 0);
stmt.setInt(2, 2);
stmt.setInt(3, rptno);
stmt.setInt(4, 1);
stmt.registerOutParameter(5, OracleTypes.CURSOR);
stmt.execute();
rsnew = (ResultSet)stmt.getObject(5);
OutputStream os = response.getOutputStream();
CsvWriter csvOutput = new CsvWriter(new OutputStreamWriter(os), ',');
ResultSetMetaData rsMetaData = rsnew.getMetaData();
int numberOfColumns =rsMetaData.getColumnCount();
for (int i = 1; i < numberOfColumns + 1; i++) {
csvOutput.write(rsMetaData.
}
csvOutput.endRecord();
while (rsnew.next()) {
for(int i=1;i<=numberOfColumns;i++)
{
csvOutput.write(rsnew.
}
csvOutput.endRecord();
}//while
response.setContentType("
response.setHeader("Content-
csvOutput.close();
rsnew.close();
out.clear();
out=pageContext.pushBody();
}//try
catch(Exception e)
{
System.out.println("error"+e.
}
finally
{
try{
//Connection close
}
catch(Exception e){
System.out.println("error"+e.
}
}
%>
No comments:
Post a Comment