Solved

Export To Excel

Posted on 2004-03-25
8
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
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.

729 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