Solved

Export To Excel

Posted on 2004-03-25
8
233 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:mayankeagle
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:mayankeagle
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

13 Experts available now in Live!

Get 1:1 Help Now