Solved

Export To Excel

Posted on 2004-03-25
8
236 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
javap not working 8 58
mysql jsp example issue 32 48
Why doesn't this text field show up on my Applet frame? 2 19
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

838 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