Solved

Export To Excel

Posted on 2004-03-25
8
235 Views
Last Modified: 2010-03-31
I have an Html page with a button and also some data in table format

On click of button I have to call some servlet or any way. I have to create and Excel sheet and export all the data to that sheet. Any suggestions??


Can I give the HTML content type to Excel format

Thanks
0
Comment
Question by:chgk
8 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675317
You can write to an Excel file using Java. Have a look at:

http://jakarta.apache.org/poi/
0
 

Author Comment

by:chgk
ID: 10675679
I think we can set the content type of an HTML page to EXCEl

any suggesions on this

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10675706
Perhaps you can, but I haven't worked with that. Anyway, you said:

>> On click of button I have to call some servlet or any way

So in the Servlet, you can put your Java code to write to the excel-sheet?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:chgk
ID: 10675910
Any help on Setting the contents type
of html to Excel sheet
0
 
LVL 3

Accepted Solution

by:
mjzalewski earned 20 total points
ID: 10677930
All you have to do is

1) Make sure your HTML is reasonably simple and correct. It works best if your page contains only a single TABLE element. You may apply formats such as fonts, font sizes, colors, background colors, etc. to the TD elements. You can specify the width and height of cells, and your settings will be reflected in the spread sheet. Bear in mind that Excel has a limited color pallette, so all of the font colors and background colors may not translate exactly.

2) Make sure that the TABLE element is properly closed, and make sure that there is a line feed after the last closing </html> element.

3) Set the MIME type (before you send any of the HTML page). In JSP, you should do this:

<%
   response.setContentType( "application/vnd.ms-excel");
%>

Put that as close to the top of the page as you can get it.
0
 

Expert Comment

by:pradeepbkumar
ID: 10684627
Try out something like this

public synchronized void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            Log.skip();
            Log.write("Servlet posting...");

            try
            {
                  String filename = request.getParameter("DownLoad");
                  File file = new File(filename);
                  
                  String lsContentDisposition = "attachment; filename=\"" + filename + "\";";

                  response.setContentType("application/vnd.ms-excel");
                  response.setContentLength((int)file.length());
                  response.setHeader("Content-Disposition", lsContentDisposition);
                  
                  java.io.FileInputStream fis = new java.io.FileInputStream(file);
                  int i = 0;
                  javax.servlet.ServletOutputStream out = response.getOutputStream();
                  while((i=fis.read()) != -1)
                  {
                        out.write(i);
                  }
                  out.flush();
                  out.close();
                  fis.close();
                  response.flushBuffer();
            }
            catch(Throwable t)
            {
                  //System.out.println("---DownLoadFile--:" + e1.getMessage() );
                  System.out.println("---DownLoadFile--:" + t.getMessage() );
            }
      }
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 36
fibonacci ten numbers 4 38
varialbe initialization 11 32
jdbc error in jsp application 20 44
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now