[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

JSP: RecordSet 2 Excel

Within a JSP, I'm pushing a resultset to Excel. That works fine with the code below.
Except that after the rows have been printed in Excel, the output IN EXCEL continues with:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">                                    
<html lang="en">                                     
<head>                                    

What's the cause?


            response.setContentType("application/vnd.ms-excel");
            PrintWriter outCSV = response.getWriter();
             // Get the result set using a method called 'doQuery'
            ResultSet rsCSV = rs_browse;

            try {
                 ResultSetMetaData metaData = rsCSV.getMetaData();
                 int numberOfColumns =  metaData.getColumnCount();
                 // Print column names
                 for(int column = 0; column < numberOfColumns; column++) {
                      outCSV.print(metaData.getColumnLabel(column+1));
                      if (column < (numberOfColumns - 1)) outCSV.print("\t");
                 }
                 outCSV.println();
                 // Get all rows and print
                 while (rsCSV.next()) {
                      for (int i = 1; i <= numberOfColumns; i++) {
                           outCSV.print(rsCSV.getObject(i).toString());
                           if (i < numberOfColumns) outCSV.print("\t");

                      }
                      outCSV.println();
                 }
            }
            catch(Exception e) {
                 e.printStackTrace();
            }
0
go4java
Asked:
go4java
  • 3
  • 2
1 Solution
 
KuldeepchaturvediCommented:
on this last part do the following changes


     // Get all rows and print
                 while (rsCSV.next()) {
                      for (int i = 1; i <= numberOfColumns; i++) {
                           outCSV.print(rsCSV.getObject(i).toString());
                           if (i < numberOfColumns) outCSV.print("\t");

                      }
outCSV.println();
                       
     }
outCSV.flush();
outCSV.close();
            }
            catch(Exception e) {
                 e.printStackTrace();
            }

0
 
go4javaAuthor Commented:
still the same?
0
 
KuldeepchaturvediCommented:
ohh my mistake..
you are not creating any HTML here right..??
if thats the case then take out every line before first
then your jsp should not be having anything out side<%%> tags because it will be considered as HTML to be outputed to the client and will mess up your outout..
0
 
go4javaAuthor Commented:
what do you mean with 'take out every line before first'
0
 
KuldeepchaturvediCommented:
like if your jsp has got some HTML tags...

<HTML>
<BODY>..

<%
 response.setContentType("application/vnd.ms-excel");
            PrintWriter outCSV = response.getWriter();
             // Get the result set using a method called 'doQuery'
            ResultSet rsCSV = rs_browse;

            try {
                 ResultSetMetaData metaData = rsCSV.getMetaData();
                 int numberOfColumns =  metaData.getColumnCount();
                 // Print column names
                 for(int column = 0; column < numberOfColumns; column++) {
                      outCSV.print(metaData.getColumnLabel(column+1));
                      if (column < (numberOfColumns - 1)) outCSV.print("\t");
                 }
                 outCSV.println();
                 // Get all rows and print
                 while (rsCSV.next()) {
                      for (int i = 1; i <= numberOfColumns; i++) {
                           outCSV.print(rsCSV.getObject(i).toString());
                           if (i < numberOfColumns) outCSV.print("\t");

                      }
                      outCSV.println();
                 }
            }
            catch(Exception e) {
                 e.printStackTrace();
            }
%>
</HTML>
then your system will print <HTML><BODY> then your CSV file and then </HTML>
thats where problem is
it should be written as
 

<%
response.setContentType("application/vnd.ms-excel");
            PrintWriter outCSV = response.getWriter();
             // Get the result set using a method called 'doQuery'
            ResultSet rsCSV = rs_browse;

            try {
                 ResultSetMetaData metaData = rsCSV.getMetaData();
                 int numberOfColumns =  metaData.getColumnCount();
                 // Print column names
                 for(int column = 0; column < numberOfColumns; column++) {
                      outCSV.print(metaData.getColumnLabel(column+1));
                      if (column < (numberOfColumns - 1)) outCSV.print("\t");
                 }
                 outCSV.println();
                 // Get all rows and print
                 while (rsCSV.next()) {
                      for (int i = 1; i <= numberOfColumns; i++) {
                           outCSV.print(rsCSV.getObject(i).toString());
                           if (i < numberOfColumns) outCSV.print("\t");

                      }
                      outCSV.println();
                 }
            }
            catch(Exception e) {
                 e.printStackTrace();
            }
%>

i.e. no HTML tags anywhere
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now