Hi, i created a grid panel i have to export it to the excel. please help me by some sample code. thanks in advance. cool day dude.
Here is a jsp application that retrieves database records in a table and export the data to excel file. You need POI api to run the given code.
1)data.jsp:
<%@page import="java.sql.*"%> <form method="post" action="excelFile.jsp"> <table border=1> <tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr> <% Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("Select * from employee"); while(rs.next()){ %> <tr><td><input type="text" name="name" value="<%=rs.getString("name")%>"></td><td><input type="text" name="address" value="<%=rs.getString("address")%>"></td><td><input type="text" name="contact" value="<%=rs.getString("contactNo")%>"></td><td><input type="text" name="email" value="<%=rs.getString("contactNo")%>"></td></tr> <% } %> </table> <input type="submit" value="Export To Excel"> </form>
2)excelFile.jsp:
<%@page import=" java.io.*"%> <%@page import=" org.apache.poi.hssf.usermodel.*"%> <% String name[]=request.getParameterValues("name"); String address[]=request.getParameterValues("address"); String contact[]=request.getParameterValues("contact"); String email[]=request.getParameterValues("email"); try{ String filename="c:/data.xls" ; HSSFWorkbook hwb=new HSSFWorkbook(); HSSFSheet sheet = hwb.createSheet("sheet"); HSSFRow rowhead= sheet.createRow((short)0); rowhead.createCell((short) 0).setCellValue("Name"); rowhead.createCell((short) 1).setCellValue("Address"); rowhead.createCell((short) 2).setCellValue("Contact No"); rowhead.createCell((short) 3).setCellValue("E-mail"); for(int i=0;i<name.length;i++){ int j=i+1; HSSFRow row= sheet.createRow((short)j); row.createCell((short) 0).setCellValue(name[i]); row.createCell((short) 1).setCellValue(address[i]); row.createCell((short) 2).setCellValue(contact[i]); row.createCell((short) 3).setCellValue(email[i]); } FileOutputStream fileOut = new FileOutputStream(filename); hwb.write(fileOut); fileOut.close(); out.println("Your excel file has been generated!"); } catch( Exception ex ) { System.out.println(ex); } %>
hi thanks dude. here you created a table and exported to excel. but i created grid in extjs and have to export to the excel. please help me for that.. thanks dude.