?
Solved

Export To Excel

Posted on 2004-03-25
8
Medium Priority
?
250 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
6 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 80 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 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