Ver Mensaje Individual
  #3 (permalink)  
Antiguo 25/11/2005, 08:08
kerman1980
 
Fecha de Ingreso: marzo-2004
Ubicación: bilbo
Mensajes: 54
Antigüedad: 20 años, 2 meses
Puntos: 0
este es el codigo para leer el excel!!

public class HSSF1
{
private HSSFWorkbook workbook=new HSSFWorkbook();
private FileInputStream inputfile;
private FileOutputStream outputfile;

public HSSF1(){}

/*

Creamos el workbook que sirve para leer el excel, le pasamos un parametro que es la path completo de donde se encuentra en excel

*/
public HSSFWorkbook getWorkbook(String s){

try{
inputfile=new FileInputStream(s);
workbook=new HSSFWorkbook(inputfile);
}catch(Exception e ){
System.out.println("error!!!!!!!!!!!!!!!!"+e.getMe ssage());
}
return workbook;
}

}

/*

Clase que lee el excel

*/

public class comActionLeerExcel extends Action
{
private static Logger logger = Logger.getLogger(comActionCrearExcel.class.getName ());

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
try{

HSSF1 example = new HSSF1();

/* creamos un HSSFWorkbook con nuestro excel */
HSSFWorkbook xls=example.getWorkbook("C://kerman.xls");

/* Elegimos la hoja que queremos leer del excel */
HSSFSheet sheet=xls.getSheetAt(0);

/* Nos recorremos las filas */
for (int j=9; j<17;j++){

/* Cogemos una fila HSSFRow row=sheet.getRow(fila);*/
HSSFRow row=sheet.getRow(j);

HSSFCell cell;
int tipo;

String s="";

/* cogemos la celda que deseamos cell=row.getCell((short)celda);
cell=row.getCell((short)1);

switch(cell.getCellType()){

case cell.CELL_TYPE_FORMULA:
s =s+ cell.getNumericCellValue();
Double amount = new Double(s);
NumberFormat numberFormatter;
numberFormatter = NumberFormat.getNumberInstance();
s = numberFormatter.format(amount);
break;

case cell.CELL_TYPE_NUMERIC:
s=s+cell.getNumericCellValue();
Double amounts = new Double(s);
NumberFormat numberFormatters;
numberFormatters = NumberFormat.getNumberInstance();
s = numberFormatters.format(amounts);
break;

}
System.out.println("s"+s);
s="";
}


HSSFRow row=sheet.getRow(17);
Date p=null;
HSSFCell cell=row.getCell((short)1);
p=cell.getDateCellValue();
SimpleDateFormat formatDate = new SimpleDateFormat("dd-MM-yyyy");
String fecha = formatDate.format(p);
System.out.println("p"+ fecha);

return (mapping.findForward("exito"));

}catch(Exception e){
request.setAttribute("exception",e);
return(mapping.findForward("error"));
}
}
}o la implementacion de la lectura del excel