santosh_tamse
03-13-2009, 11:44 AM
hi all,
i hav written a program in which i hav to read excel file.the problem is that after reading hav printed the values now i dont hav to print values.after reading the control should automatically get transferred to other. i m writing this program using apache poi in jsp
code:
<%@page language="Java"%>
<%@page import="java.io.*,java.lang.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@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.poifs.filesystem.POIFSFileSystem"%>
<%@page contentType="application/vnd.ms-excel" %>
<%
HSSFRow row;
HSSFCell cell;
Connection con=null;
PreparedStatement stmt;
String parea="",esgrp="",egroup="",personelsub="",halfname="",esubgroup="",costctr="",psubarea="",designation="",position="",name="";
int bc=0,tno=0,eegrp=0,flag=0;
String name1="",name2="";
String arr[][];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:deployment","","");
}
catch(Exception e)
{
out.println("insewrted");
out.println(e.getMessage());
}
try
{
String nam=request.getParameter("sun1.xls");
int i=0;
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(nam));
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rows;
rows = sheet.getPhysicalNumberOfRows();
out.println("Rows are"+rows);
int cols = 0;
int tmp = 0;
for(int i2 = 0;i2 < rows; i2++)
{
row = sheet.getRow(i2);
if(row != null)
{
tmp = sheet.getRow(i2).getPhysicalNumberOfCells();
if(tmp > cols)
cols = tmp;
}
}
out.println("Columns are="+cols);
arr=new String[rows][cols];
int m=0,n=0;
for(int r = 0; r < rows/2; r++)
{
row = sheet.getRow(r);
if(row != null)
{
for(int c = 0; c < 15; c++)
{
m=c;
n=m+1;
cell = row.getCell((short)c);
if(cell != null)
{
if(m==1||m==5||m==9)
{
arr[r][c]=Double.toString(cell.getNumericCellValue());
}
else
{
arr[r][c]=cell.getStringCellValue();
}
}//if
else
{
if(m==11)
{
arr[r][c]="";
}
}
}//for
}//if
}//for
for(int r1 = rows/2; r1 < rows; r1++)
{
row = sheet.getRow(r1);
if(row != null)
{
for(int c1= 0; c1 < 15; c1++)
{
m=c1;
n=m+1;
cell = row.getCell((short)c1);
if(cell != null)
{
if(m==1||m==5||m==9)
{
arr[r1][c1]=Double.toString(cell.getNumericCellValue());
}
else
{
arr[r1][c1]=cell.getStringCellValue();
}
}//if
else
{
if(m==11)
{
arr[r1][c1]="";
}
}
}//for
}//if
}//for
int xy=0;
for(int jj=0;jj<rows;jj++)
{
stmt=con.prepareStatement("insert into Acemain values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
out.println("<br>");
for(int kk=0;kk<cols;kk++)
{
xy=kk+1;
out.print(arr[jj][kk]);
stmt.setString(xy,arr[jj][kk]);
}
stmt.executeUpdate();
}
response.sendRedirect("Ace1.jsp");
}
catch(Exception e)
{
out.println("inserted2");
out.println(e.getMessage());
}
%>
i hav written a program in which i hav to read excel file.the problem is that after reading hav printed the values now i dont hav to print values.after reading the control should automatically get transferred to other. i m writing this program using apache poi in jsp
code:
<%@page language="Java"%>
<%@page import="java.io.*,java.lang.*" %>
<%@page import="java.sql.*" %>
<%@page import="java.util.*" %>
<%@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.poifs.filesystem.POIFSFileSystem"%>
<%@page contentType="application/vnd.ms-excel" %>
<%
HSSFRow row;
HSSFCell cell;
Connection con=null;
PreparedStatement stmt;
String parea="",esgrp="",egroup="",personelsub="",halfname="",esubgroup="",costctr="",psubarea="",designation="",position="",name="";
int bc=0,tno=0,eegrp=0,flag=0;
String name1="",name2="";
String arr[][];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:deployment","","");
}
catch(Exception e)
{
out.println("insewrted");
out.println(e.getMessage());
}
try
{
String nam=request.getParameter("sun1.xls");
int i=0;
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(nam));
HSSFWorkbook wb=new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int rows;
rows = sheet.getPhysicalNumberOfRows();
out.println("Rows are"+rows);
int cols = 0;
int tmp = 0;
for(int i2 = 0;i2 < rows; i2++)
{
row = sheet.getRow(i2);
if(row != null)
{
tmp = sheet.getRow(i2).getPhysicalNumberOfCells();
if(tmp > cols)
cols = tmp;
}
}
out.println("Columns are="+cols);
arr=new String[rows][cols];
int m=0,n=0;
for(int r = 0; r < rows/2; r++)
{
row = sheet.getRow(r);
if(row != null)
{
for(int c = 0; c < 15; c++)
{
m=c;
n=m+1;
cell = row.getCell((short)c);
if(cell != null)
{
if(m==1||m==5||m==9)
{
arr[r][c]=Double.toString(cell.getNumericCellValue());
}
else
{
arr[r][c]=cell.getStringCellValue();
}
}//if
else
{
if(m==11)
{
arr[r][c]="";
}
}
}//for
}//if
}//for
for(int r1 = rows/2; r1 < rows; r1++)
{
row = sheet.getRow(r1);
if(row != null)
{
for(int c1= 0; c1 < 15; c1++)
{
m=c1;
n=m+1;
cell = row.getCell((short)c1);
if(cell != null)
{
if(m==1||m==5||m==9)
{
arr[r1][c1]=Double.toString(cell.getNumericCellValue());
}
else
{
arr[r1][c1]=cell.getStringCellValue();
}
}//if
else
{
if(m==11)
{
arr[r1][c1]="";
}
}
}//for
}//if
}//for
int xy=0;
for(int jj=0;jj<rows;jj++)
{
stmt=con.prepareStatement("insert into Acemain values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
out.println("<br>");
for(int kk=0;kk<cols;kk++)
{
xy=kk+1;
out.print(arr[jj][kk]);
stmt.setString(xy,arr[jj][kk]);
}
stmt.executeUpdate();
}
response.sendRedirect("Ace1.jsp");
}
catch(Exception e)
{
out.println("inserted2");
out.println(e.getMessage());
}
%>