Hola tengo un problema. Mi aplicación tiene que leer un excel y guardar los datos en la base de datos. La cuestion es que eso ya lo he conseguido. El problema viene cuando el excel tiene macros que me da un error. Si el excel no tiene macrois me funciona perfectamente, el error es el siguiente:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:274)
at org.apache.poi.hssf.record.RecordFactory.createRec ord(RecordFactory.java:224)
at org.apache.poi.hssf.record.RecordFactory.createRec ords(RecordFactory.java:160)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>( HSSFWorkbook.java:163)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>( HSSFWorkbook.java:210)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>( HSSFWorkbook.java:191)
at eroski.scin.pmis.struts.controller.HSSF1.getWorkbo ok(HSSF1.java:24)
at eroski.scin.pmis.struts.controller.comActionLeerEx cel.execute(comActionLeerExcel.java:55)
at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(Actio nServlet.java:397)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:853)
at com.evermind.server.http.ServletRequestDispatcher. invoke(ServletRequestDispatcher.java:795)
at com.evermind.server.http.ServletRequestDispatcher. forwardInternal(ServletRequestDispatcher.java:330)
at com.evermind.server.http.HttpRequestHandler.proces sRequest(HttpRequestHandler.java:794)
at com.evermind.server.http.HttpRequestHandler.run(Ht tpRequestHandler.java:270)
at com.evermind.server.http.HttpRequestHandler.run(Ht tpRequestHandler.java:112)
at com.evermind.util.ReleasableResourcePooledExecutor $MyWorker.run(ReleasableResourcePooledExecutor.jav a:192)
at java.lang.Thread.run(Thread.java:534)
at java.lang.System.arraycopy(Native Method)
at org.apache.poi.hssf.record.UnknownRecord.<init>(Un knownRecord.java:62)
at org.apache.poi.hssf.record.SubRecord.createSubReco rd(SubRecord.java:57)error!!!!!!!!!!!!!!!!Unable to construct record instance, the following exception occured: null
at org.apache.poi.hssf.record.ObjRecord.fillFields(Ob jRecord.java:99)
at org.apache.poi.hssf.record.Record.fillFields(Recor d.java:90)
at org.apache.poi.hssf.record.Record.<init>(Record.ja va:55)
at org.apache.poi.hssf.record.ObjRecord.<init>(ObjRec ord.java:61)
Y este es el codigo:
public class HSSF1
{
private HSSFWorkbook workbook=new HSSFWorkbook();
private FileInputStream inputfile;
private FileOutputStream outputfile;
public HSSF1(){}
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;
}
}
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();
HSSFWorkbook xls=example.getWorkbook("C://kerman.xls");
HSSFSheet sheet=xls.getSheetAt(0);
for (int j=9; j<17;j++){
HSSFRow row=sheet.getRow(j);
HSSFCell cell;
int tipo;
String s="";
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"));
}
}
}