Hello,
I am very new to Java and trying to write Excel sheet from a servlet.
I have downloaded the latest rel. of POI(1.5.1-final) and put all my jars
(log4/commons/jakart-poi etc.) under \WEB-INF\lib. I have my Servlet ready.I
click the button to generate the sheet from a jsp page. The servlet runs but
I do not see the XLS sheet in the directory where it should be I do not get
any errors either.
I have permission to write files and I am running Tomcat Web Server/3.2.4
(JSP 1.1; Servlet 2.2; Java 1.3.1.01-release; HP-UX B.11.00
Please help me resove this problem. Here is the code for Servlet
****************************************************************************
*****************************************
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class SaveExcel extends HttpServlet {
private Connection Conn = null;
private Statement Stmt = null;
private ResultSet myResult=null;
short rownum;
String Var_Pathinfo;
String FilenamewithPath;
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
//servletContext = config.getServletContext();
Var_Pathinfo = config.getServletContext().getRealPath("/");
super.init(config);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
//get the query variables
String var_EMID = (String) request.getParameter("EMID");
String var_StartDate = (String) request.getParameter("StartDate");
String var_EndDate = (String) request.getParameter("EndDate");
String sMessage = new String("");
String sMessage2 = new String("");
String sqlstrExcel = "SELECT REC_ID,To_Char(ATT_DATE,'MM/DD/YYYY')
ATT_DATE,TOTAL_HRS,ATTENDANCE_DESC FROM ATT WHERE"
+ " ATT_DATE BETWEEN TO_DATE(" + "'" + var_StartDate + "','" +
"MM/DD/YYYY" + "')" + " AND TO_DATE('" + var_EndDate + "','" + "MM/DD/YYYY"
+"')"
+ " AND EM_ID = " + "'"+var_EMID+"'";
try {
//create the blank Excel workbook here
FilenamewithPath = (Var_Pathinfo + "Test.xls");
// out.println(FilenamewithPath);
//this prints the whole path /opt/mis/atco/java/atco/
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a new font and alter it.
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream(FilenamewithPath);
wb.write(fileOut);
fileOut.close();
}//end try block
catch (Exception E) {
System.err.println(E.toString());
}//end catch
}//end doPost
}//end Class SaveExcel
I would appreciate your help.
Anwarul M Sufi
MRL Strategic Facilities Planning & Management
Tel: (732)-726-2867
E-mail: [EMAIL PROTECTED]
Mail Stop: WBD-215
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains information of
Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential,
proprietary copyrighted and/or legally privileged, and is intended solely for the use
of the individual or entity named in this message. If you are not the intended
recipient, and have received this message in error, please immediately return this by
e-mail and then delete it.
==============================================================================