Link to home
Start Free TrialLog in
Avatar of chgk
chgk

asked on

Export To Excel

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
Avatar of Mayank S
Mayank S
Flag of India image

You can write to an Excel file using Java. Have a look at:

http://jakarta.apache.org/poi/
Avatar of chgk
chgk

ASKER

I think we can set the content type of an HTML page to EXCEl

any suggesions on this

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?
Avatar of chgk

ASKER

Any help on Setting the contents type
of html to Excel sheet
ASKER CERTIFIED SOLUTION
Avatar of mjzalewski
mjzalewski

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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() );
            }
      }