
following is my jsp code it is working correct.. i want to save excel file throgh jsp bt not by hard coded path as i have written in my prog(File file=new File("c:\"+fname+".xls");) user should have freedom to save the file in any location so plz give me suggestions
<%@ page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@ page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%@ page import="org.apache.poi.hssf.util.HSSFColor"%>
<%@ page import="java.io.* " %>
<%@page import="java.sql.* "%>
<html>
<body bgcolor="lightgrey">
<%
String fname=request.getParameter("fname");
if(fname.equals(""))
{
%><h1><b><i><% out.println("PLZ Enter filename!");%></i></b></h1>
<%
}else
{
File file=new File("c:\\"+fname+".xls");
session.setAttribute("fname",fname);
boolean exists = file.exists();
//boolean exists = file.exists();
if (exists)
{
%>
<h1><b><i>
<%out.println("File or Directory exist.do u want to replace it? or PLZ enter another filename");%></i></b></h1>
<h3><a href="replace.jsp">Replace the existing one</a></h3><br><br>
<h3><a href="save2.jsp">Enter another filename</a></h3>
<%
}else
{
String s1=null;
try
{
String MyDate=(String)session.getAttribute( "MyDate" );
session.setAttribute( "MyDate", MyDate );
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
HSSFSheet sheet1 = hwb.createSheet("new sheet1");
HSSFRow rowhead= sheet.createRow((short)0);
rowhead.createCell((short) 0).setCellValue("INSTRUMENT");
rowhead.createCell((short) 1).setCellValue("SYMBOL");
rowhead.createCell((short) 2).setCellValue("EXP_DATE");
rowhead.createCell((short) 3).setCellValue("STR_PRICE");
rowhead.createCell((short) 4).setCellValue("OPT_TYPE");
rowhead.createCell((short) 5).setCellValue("OPEN_PRICE");
rowhead.createCell((short) 6).setCellValue("HI_PRICE");
rowhead.createCell((short) 7).setCellValue("LO_PRICE");
rowhead.createCell((short) 8).setCellValue("CLOSE_PRICE");
rowhead.createCell((short) 9).setCellValue("OPEN_INT*");
rowhead.createCell((short) 10).setCellValue("TRD_QTY");
rowhead.createCell((short) 11).setCellValue("NO_OF_CONT");
rowhead.createCell((short) 12).setCellValue("NO_OF_TRADE");
rowhead.createCell((short) 13).setCellValue("NATIONAL_VAL");
rowhead.createCell((short) 14).setCellValue("PR_VAL");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://";
Connection con = DriverManager.getConnection(url,"**","*******");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from CO ");
int i=1;
while(rs.next())
{
HSSFRow row= sheet.createRow((short)i);
s1=rs.getString("EXP_DATE");
if(s1.equals((String)session.getAttribute("MyDate")))
{
row.createCell((short) 0).setCellValue(rs.getString("INSTRUMENT"));
row.createCell((short) 1).setCellValue(rs.getString("SYMBOL"));
row.createCell((short) 2).setCellValue(rs.getString("EXP_DATE"));
row.createCell((short) 3).setCellValue(rs.getString("STR_PRICE"));
row.createCell((short) 4).setCellValue(rs.getString("OPT_TYPE"));
row.createCell((short) 5).setCellValue(rs.getString("OPEN_PRICE"));
row.createCell((short) 6).setCellValue(rs.getString("HI_PRICE"));
row.createCell((short) 7).setCellValue(rs.getString("LO_PRICE"));
row.createCell((short) 8).setCellValue(rs.getString("CLOSE_PRICE"));
row.createCell((short) 9).setCellValue(rs.getString("OPEN_INT*"));
row.createCell((short) 10).setCellValue(rs.getString("TRD_QTY"));
row.createCell((short) 12).setCellValue(rs.getString("NO_OF_TRADE"));
row.createCell((short) 13).setCellValue(rs.getString("NATIONAL_VAL"));
row.createCell((short) 14).setCellValue(rs.getString("PR_VAL"));
i++;
}
}
FileOutputStream fileOut = new FileOutputStream(file);
hwb.write(fileOut);
fileOut.close();
%><h1><b><i><% out.println("Your excel file has been generated!");%>
</i></b></h1>;
</form>
</body>
</html>
<%
}catch ( Exception ex )
{
System.out.println(ex);
}
}
}
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.