Richards,

Though new to the topic, I just tried out the example given by you. I'm
using IE. The servlet generates the output on to the browser but not to an
excel sheet.

Also, could u please be elaborate on

 res.setContentType("application/vnd.ms-excel");

Thanks,
Mani

-----Original Message-----
From: Ryan Richards [ mailto:[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> ]
Sent: Wednesday, April 05, 2000 10:18 PM
To: [EMAIL PROTECTED]
Subject: servlet using Excel example code


OK guys, here a little servlet I wrote to place a query returned from a
database into an excel spreadsheet. I modified the code to make it simpler,
hopefully I didnt make any mistakes in editing. Feel free to email me if you
have any questions.

If you are using Netscape it prompts you to open or save. If you open the
Excel is laucnhed and your spreadsheet contains the data. If you are using
IE then it automatically opens an excel spreadsheet.

HTH
Ryan
--------------------------------------------------------
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.StringTokenizer;

public class dbServlet extends HttpServlet
                                    implements SingleThreadModel {
  Connection conn = null;

  public void init( ServletConfig config ) throws ServletException {
    super.init(config);
    ...
    ...
    snip
  } // init

  public void doGet( HttpServletRequest req,
                      HttpServletResponse res )
    throws ServletException, IOException {

        // Setup the stream
        PrintWriter out = res.getWriter();
        res.setContentType("application/vnd.ms-excel");

    try { // Create a Statement
       Statement stmt = conn.createStatement ();
       ResultSet rset = null;

       rset = stmt.executeQuery(
                        "select name,"+
                        "salary,"+
                        "dept,"+
                        "from employee_table"+
                //
                // Send the information to Excel.
                // The tabs (\t) tell the code to place
                // each rset item into it's own column.
                //

                out.println("Name\tSalary\tDept");
                while ( rset.next () )
                {
                out.println(rset.getString(1)+"\t"+
                            rset.getString(2)+"\t"+
                            rset.getString(3));
                }

    } catch ( SQLException e ) {
       System.err.println("A database error occurred.");
    }
        out.close();
  } // doGet
} // dbServlet

Ryan Richards
Technical Consultant
Database Consultants, Inc.
Oklahoma City, OK
-----------------------------------------------
FREE! The World's Best Email Address @email.com
Reserve your name now at http://www.email.com <http://www.email.com>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
<http://archives.java.sun.com/archives/servlet-interest.html>
Resources: http://java.sun.com/products/servlet/external-resources.html
<http://java.sun.com/products/servlet/external-resources.html>
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
<http://www.lsoft.com/manuals/user/user.html>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to