Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Displaying the query results in Excel

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a jsp page where user enters some parameter and then get the results. Now, i want to give him a choice of viewing this in MS-Excel.So, My controller would recieve the request, query the database and then send the results to MS-Excel. I know how to do the query but have no idea how i should send the results to excel. I know how to send the regular data to excel like:



Could anyone please guide me how to get the results from the database to show in Excel program. I would really appreciate it. Thanks a lot!

Aliya
 
author & internet detective
Posts: 40747
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aliya,
To access the database use JDBC.

If you need to display anything more complex in Excel, you can use HSSF/POI an excellent framework for creating spreadsheets.
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aliya,

SInce you know how to query the database, as well as you can display excel with hardcoded values

then there should be no problem in displaying excel content.

I recommend you to use DTO's in your application is not using I am modifying your
code assuming same values are in database.


--------------------------------------------
Create DTO Class
--------------------------------------------
public class myExcelDTO Implements Serializable{

private String q1="";
private String q2="";
private String q3="";
private String q4="";
private String total="";


public String getQ1()
{
retrun this.q1;
}

public void setQ1(String q1)
{
this.q1 = q1;
}
.
.
.
...so on .......

}



-------------------------------------------------------------
At database end
-------------------------------------------------------------
Excecute your query get the resultset (rs) then

myExcelDTO myDTO = null;
ArrayList retList= new ArrayList();

while(rs.next())
{
myDTO = new myExcelDTO();

myDTO.setQ1(rs.getString("q1"));
myDTO.setQ2(rs.getString("q2"));
.
.
so on

retList.add(myDTO);
}


return retList;



--------------------------------------------------------------
At JSP
--------------------------------------------------------------


resp.setContentType("application/vnd.ms-excel");
PrintWriter out = resp.getWriter();

//-- call your method and get the ArrayList retList and then


out.println("\tQ1\tQ2\tQ3\tQ4\tTotal");
for(int i=0; i < retList.size();i++)
{
myExcelDTO jspDTO = (myExcelDTO) retList.get(i);
out.println(jspDTO.getq1()+ "\t" +jspDTO.getq2()+"\t"+jspDTO.getq3()+"\t"+jspDTO.getq4()+"\t"+jspDTO.getTotal());

}
[ October 16, 2004: Message edited by: Shailesh Chandra ]
 
aliya sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Shailesh. I tried it and worked like a charm!..Again, i really appreicate it.

Aliya
 
My, my, aren't you a big fella. Here, have a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic