Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:12:59+00:00 2026-05-16T07:12:59+00:00

I need to display the newly generated excel (from tables using Apache POI) in

  • 0

I need to display the newly generated excel (from tables using Apache POI) in a web browser (whatever it is, Firefox, Opera or IE). I’ve created the JSP file with

contentType="application/vnd.ms-excel"

But I’m not getting it.

Here’s my code snippet :

<%@page session="true" contentType="application/vnd.ms-excel" pageEncoding="UTF-8"%>
<%@page import="org.apache.poi.ss.usermodel.CellStyle"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="org.apache.poi.ss.usermodel.CreationHelper"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>

<html>
    <head>
        <%!
            int r=0;

            HSSFWorkbook book;
            HSSFSheet sheet;
            HSSFRow row;

            CreationHelper createHelper = book.getCreationHelper();

            Connection conn;
            Statement stmt;
            ResultSet rs;
        %>
        <title>Report</title>
        <%
            book = new HSSFWorkbook();
            sheet = book.createSheet("Report");
        %>
    </head>
    <body>
        <%
            try     {

                    // Header of the Excel File
                row = sheet.createRow(r);

                row.createCell(0).setCellValue("Visit ID");
                row.createCell(1).setCellValue("Carrier Name");
                row.createCell(2).setCellValue("Phone Number");
                row.createCell(3).setCellValue("Patient Name");
                row.createCell(4).setCellValue("Subscriber ID");
                row.createCell(5).setCellValue("Subscriber Name");
                row.createCell(6).setCellValue("Chart Number");
                row.createCell(7).setCellValue("Date Of Birth");
                row.createCell(8).setCellValue("Subscriber Employer");
                row.createCell(9).setCellValue("Service Date");
                row.createCell(10).setCellValue("Provider Name");
                row.createCell(11).setCellValue("CPT Code");
                row.createCell(12).setCellValue("Aging Date");
                row.createCell(13).setCellValue("Total");
                row.createCell(14).setCellValue("Follow Up Notes");
                row.createCell(15).setCellValue("Internal Status Code");

                CellStyle cellStyle = book.createCellStyle();
                cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("MM/dd/yyyy"));

                Statement stNotes;
                ResultSet rsNotes;

                Class.forName("com.mysql.jdbc.Driver");

                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/maintain", "root", "root");
                stmt = conn.createStatement();

                stNotes = conn.createStatement();

                rs = stmt.executeQuery("SELECT b.VisitID, b.CarrierName, b.PhoneNum, b.PatientName, "
                        + "b.SubscriberID, b.SubscriberName, b.ChartNum, b.DoB, b.SubscriberEmp, "
                        + "b.ServiceDate, b.ProviderName, b.CPTCode, b.BillingDate, b.BalanceAmt "
                        + "FROM billing b INNER JOIN followup f ON b.VisitID = f.VisitID GROUP BY VisitID");

                while(rs.next())    {
                    r++;

                    row = sheet.createRow(r);

                    row.createCell(0).setCellValue(rs.getString("VisitID"));
                    row.createCell(1).setCellValue(rs.getString("CarrierName"));
                    row.createCell(2).setCellValue(rs.getString("PhoneNum"));
                    row.createCell(3).setCellValue(rs.getString("PatientName"));
                    row.createCell(4).setCellValue(rs.getString("SubscriberID"));
                    row.createCell(5).setCellValue(rs.getString("SubscriberName"));
                    row.createCell(6).setCellValue(rs.getString("ChartNum"));
                    row.createCell(7).setCellValue(rs.getString("DoB"));
                    row.createCell(8).setCellValue(rs.getString("SubscriberEmp"));


                    row.createCell(9).setCellValue(rs.getString("ServiceDate"));
                    row.createCell(9).setCellStyle(cellStyle);

                    row.createCell(10).setCellValue(rs.getString("ProviderName"));
                    row.createCell(11).setCellValue(rs.getString("CPTCode"));

                    row.createCell(12).setCellValue(rs.getString("BillingDate"));
                    row.createCell(12).setCellStyle(cellStyle);

                    row.createCell(13).setCellValue(rs.getString("BalanceAmt"));

                    rsNotes = stNotes.executeQuery("SELECT Date, InternalStatusCode, FollowUpNote "
                            + "FROM followup WHERE VisitID='" + rs.getString("VisitID") + "' ORDER BY Date");

                    while(rsNotes.next())   {
                        row.createCell(14).setCellValue(rsNotes.getString("Date") + " - " + rsNotes.getString("FollowUpNote"));
                        row.createCell(15).setCellValue(rs.getString("VisitID"));
                    }
                }

            }
            catch(ClassNotFoundException cnf) {
                out.print("<br> Error : MySQL Driver not found. <br>");
            }
            catch(Exception ex)  {
                out.print("Error :  <br>" + ex);
            }
        %>
    </body>
</html>

I’m getting this exception with Tomcat 6.0.26 :

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:156)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

root cause

java.lang.NullPointerException
    org.apache.jsp.GetReport_jsp.<init>(GetReport_jsp.java:29)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    java.lang.Class.newInstance0(Class.java:355)
    java.lang.Class.newInstance(Class.java:308)
    org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:145)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

Did I miss something or anything wrong?
Can anybody help me to get rid of this problem?

Thanx in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:13:00+00:00Added an answer on May 16, 2026 at 7:13 am

    The way you are trying to do it doesn’t make any sense. You can’t mix HTML with Excel like that. Better create a servlet instead of a JSP page and let this servlet output only the Excel file and nothing else.

    Something like this:

    import java.io.*;    
    import javax.servlet.http.*;
    import javax.servlet.*;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    // ... plus all the other libs you need
    
    public class ExcelServlet extends HttpServlet {
      public void doGet (HttpServletRequest req,
                         HttpServletResponse res)
        throws ServletException, IOException
      {
        HSSFWorkbook book;
        // ...
        // fill the book
        // ...
        res.setContentType("application/vnd.ms-excel");
        book.write(res.getOutputStream());
        res.getOutputStream().close();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.