When you want to generate excel file in jsp java then you have lot of options,
for ex: jexcelapi, Apache POI,other paid library.
The problem is that the file; first will be created on hard disk then after read it and download to the user’s system.
We all know that the .csv files open in excel software, so we can generate .csv file without any third party library.
It is very simple, quickly, fast and easy to use.
Use listed below code.
showdata.jsp
============================================================
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.StringTokenizer" %>
<%
// made cdb connection here
PreparedStatement psTestUser = conn.prepareStatement("SELECT *from emp");
ResultSet rsTestUser=psTestUser.executeQuery();
String test_login_id,skills_text,desig_text;
ArrayList<String> l1 = new ArrayList<String>();
l1.add("Login,Skills,Design.");
%>
<body>
<a href="download_excel.jsp">download_excel</a>
<table width="100%" height="600" align="left" border="0">
<tr>
<td><strong>User Name</strong></td>
<td><strong>Designation</strong></td>
<td><strong>Skills</strong></td>
</tr>
<%
while (rsTestUser.next()){
%>
<tr>
<td><%=rsTestUser.getString("login") %></td>
<td><%=decryptA(rsTestUser.getString("skills")) %></td>
<td><%=rsTestUser.getString("design") %></td>
</tr>
<%
StringBuilder sb=new StringBuilder();
sb.append(rsTestUser.getString("login")+","+rsTestUser.getString("skills")+",");
sb.append(decryptA(rsTestUser.getString("design")));
l1.add(sb.toString());
}
session.setAttribute("listResult",l1);
%>
</table>
</body>
</html>
<%
// close database connection here
%>
download_excel.jsp
=========================
<%@ page import="java.util.ArrayList"%>
<%
response.setContentType("application/csv");
response.setHeader("content-disposition","filename=test.csv");
ArrayList<String> list = (ArrayList<String>)session.getAttribute("listResult");
for(int i = 0; i < list.size(); i++) {
String myString = (String)list.get(i);
out.println(myString);
}
out.flush();
out.close();
%>
for ex: jexcelapi, Apache POI,other paid library.
The problem is that the file; first will be created on hard disk then after read it and download to the user’s system.
We all know that the .csv files open in excel software, so we can generate .csv file without any third party library.
It is very simple, quickly, fast and easy to use.
Use listed below code.
showdata.jsp
============================================================
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.text.*" %>
<%@ page import="java.util.StringTokenizer" %>
<%
// made cdb connection here
PreparedStatement psTestUser = conn.prepareStatement("SELECT *from emp");
ResultSet rsTestUser=psTestUser.executeQuery();
String test_login_id,skills_text,desig_text;
ArrayList<String> l1 = new ArrayList<String>();
l1.add("Login,Skills,Design.");
%>
<body>
<a href="download_excel.jsp">download_excel</a>
<table width="100%" height="600" align="left" border="0">
<tr>
<td><strong>User Name</strong></td>
<td><strong>Designation</strong></td>
<td><strong>Skills</strong></td>
</tr>
<%
while (rsTestUser.next()){
%>
<tr>
<td><%=rsTestUser.getString("login") %></td>
<td><%=decryptA(rsTestUser.getString("skills")) %></td>
<td><%=rsTestUser.getString("design") %></td>
</tr>
<%
StringBuilder sb=new StringBuilder();
sb.append(rsTestUser.getString("login")+","+rsTestUser.getString("skills")+",");
sb.append(decryptA(rsTestUser.getString("design")));
l1.add(sb.toString());
}
session.setAttribute("listResult",l1);
%>
</table>
</body>
</html>
<%
// close database connection here
%>
download_excel.jsp
=========================
<%@ page import="java.util.ArrayList"%>
<%
response.setContentType("application/csv");
response.setHeader("content-disposition","filename=test.csv");
ArrayList<String> list = (ArrayList<String>)session.getAttribute("listResult");
for(int i = 0; i < list.size(); i++) {
String myString = (String)list.get(i);
out.println(myString);
}
out.flush();
out.close();
%>
0 comments:
Post a Comment