I'm using this code but only row of table data is entered into file.. .I'm using mysql database. connection is established in connection.jsp. tablename is login. using poi-2.5.1.jar file. this code is working without any error but how to get entire table data into excel sheet
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@page import="java.sql.*"%>
<%@pageimport="java.util.*,java.io.*,javax.servlet.*,javax.servlet.http.*"%>
<%@ page import="java.io.*"%>
<%@include file="connection.jsp" %>
<%@page import=" org.apache.poi.hssf.usermodel.*"%>
<head>
<meta httpequiv="ContentType"content="text/html;charset=UTF-8">
<title>JSP Page</title>
</head>
<% response.setContentType("application/xls");
response.setHeader("ContentDisposition","attachment;filename=File.xls"); %>
<body>
<table>
<tr><th>CG</th><th>CD</th></tr>
<%
Statement stmt = conn.createStatement();
String filename="C:/Users/sangu/Downloads/data.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("sheet");
HSSFRow row= sheet.createRow((short)0);
ResultSet res = stmt.executeQuery("select * from login ");
while (res.next())
{
String cg = res.getString(1);
String cd = res.getString(3);
row.createCell((short) 0).setCellValue(cd);
row.createCell((short) 1).setCellValue(cd);
row.createCell((short) 1).setCellValue(cd);
%>
<tr>
<td align="center"><%=cg%></td>
<td align="center"><%=cd%></td>
</tr>
<%
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
out.println("Your excel file has been generated!");
hwb.write(response.getOutputStream()); }
%>
</table>
</body>
</html>
Don't know for sure but shouldn't be
HSSFRow row= sheet.createRow((short)0);
inside while loop? so it will create new row for every result?
EDIT:
Statement stmt = conn.createStatement();
String filename="C:/Users/sangu/Downloads/data.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("sheet");
ResultSet res = stmt.executeQuery("select * from login ");
while (res.next())
{
HSSFRow row= sheet.createRow((short)0);
String cg = res.getString(1);
String cd = res.getString(3);
row.createCell((short) 0).setCellValue(cd);
row.createCell((short) 1).setCellValue(cd);
row.createCell((short) 1).setCellValue(cd);
%>
<tr>
<td align="center"><%=cg%></td>
<td align="center"><%=cd%></td>
</tr>
<%
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
out.println("Your excel file has been generated!");
hwb.write(response.getOutputStream()); }