您的位置:程序门 -> java -> j2ee / ejb / jms



excel导入问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


excel导入问题
发表于:2007-06-07 15:07:23 楼主
页面调用:
###############
  调用为如下代码   :
  function   doquery1()   {
                  if(confirm( "确定删除该企业信息吗? ")){
                  document.kehuxinxiform.action   =   "kehuxinxiupload.do?method=readexcel ";
                document.kehuxinxiform.submit();
        }                                                                             }

#
###############


strut_config.xml:
------------------------------------
<form-beans>
<form-bean   name= "recexcelform "   type= "com.dsweb.form.recexceltype "   />
</form-beans>

<action   path= "/kehuxinxiupload "   type= "com.dsweb.action.uploadkehuxinxi "   name= "recexcelform "   parameter= "method "   scope= "request ">  
  </action>


uploadkehuxinxi.java
--------------------
package   com.dsweb.action;

import   com.dsweb.form.recexceltype;
import   com.dsweb.util.dbutil;

import   org.apache.poi.hssf.usermodel.hssfcell;
import   org.apache.poi.hssf.usermodel.hssfrow;
import   org.apache.poi.hssf.usermodel.hssfsheet;
import   org.apache.poi.hssf.usermodel.hssfworkbook;

import   java.io.file;
import   java.io.fileinputstream;
import   java.sql.connection;
import   java.sql.preparedstatement;
import   java.sql.sqlexception;
import   java.util.arraylist;
import   java.util.list;
import   java.text.decimalformat;

public   class   uploadkehuxinxi
{
        private   string   restype;

        public   uploadkehuxinxi(string   restype)
        {
                this.restype   =   restype;
        }

        public   void   renamefile()
        {

        }

        /**
          *             *
          *
          *   @return
          *   @throws   exception
          */
        //string   filepath
        public   list   readexcel()   throws   exception
        {       system.out   .println( "##################### ");
                list   list   =   new   arraylist();
                file   file   =   new   file( "d:/aaa.xls ");
                if   (!file.exists())
                {
                        return   null;
                }
                fileinputstream   is   =   null;
                try
                {
                        is   =   new   fileinputstream(file);
                        hssfworkbook   workbook   =   new   hssfworkbook(is);
                        hssfsheet   sheet   =   workbook.getsheetat(0);
                        hssfcell   cell   =   null;
                        int   rowcount   =   sheet.getphysicalnumberofrows();
                        for   (int   i   =   1;   i   <   rowcount;   i++)
                        {
                                recexceltype   ret   =   new   recexceltype();
                                hssfrow   arow   =   sheet.getrow(i);
                                //ret.setcode(restype);//资源类型;

                                cell   =   arow.getcell((short)   0);
                                ret.setcode(casetype(cell));                           //管理码

                                cell   =   arow.getcell((short)   1);
                                ret.setidentifier(casetype(cell));               //纳税识别码

                                cell   =   arow.getcell((short)   2);                 //单位名称
                                ret.setcompany_name(casetype(cell));

                                  cell   =   arow.getcell((short)   2);                 //密码
                                ret.setpassword(casetype(cell));

                                list.add(ret);
                        }
                }
                catch   (exception   e)
                {
                        e.printstacktrace();
                }
                finally
                {
                        if   (is   !=   null)
                        {
                                is.close();
                        }
                }
                return   list;
        }

        public   void   storeresource(list   list,string   cp_id,string   cp_emp_id)   throws   exception
        {
                if   (list   ==   null)
                {
                        return;
                }
                else
                {
                        connection   conn   =   dbutil.getcurrentconnection();
                        preparedstatement   ps   =   null;
//                         resultset   rs   =   null;

                        preparedstatement   psfolder   =   null;
        //             importutil   util   =   new   importutil();

          //               long[]   primarykey   =   util.getresid(list.size());

                        string   sql   =   "insert   into   company_extend(code,idendifier, "   +
                                        "   password,company_name,tel1,mobile1,tel2,mobile2,open_sms1,open_sms2, "   +
                                        "   valid_date,create_time,address,status,postcode,last_mod_time,update_status "   +

                                        "   )   values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

        //             string   sqlfolder   =   "   insert   into   wap_re_folder_res   (folder_id,res_id,order_index)   "   +
        //                                 "   values(?,?,sq_wap_re_folder_res.nextval) ";
                        boolean   isfolder   =   false;
                        try
                        {
                                ps   =   conn.preparestatement(sql);
            //                 psfolder   =   conn.preparestatement(sqlfolder);
                                for   (int   i   =   0;   i   <   list.size();   i++)
                                {
                                        recexceltype   recexceltype1   =   (recexceltype)   list.get(i);
                                      //   ps.setlong(1,   primarykey[i]);
                                        //ps.setint(2,   1);
                                        //ps.setint(1,   integer.parseint(restype));
                                        ps.setstring(1,   recexceltype1.getcode());
                                        ps.setstring(2,   recexceltype1.getidentifier());
                                        ps.setstring(3,   recexceltype1.getcompany_name());
                                        ps.setstring(4,   recexceltype1.getpassword());


                                        //ps.setstring(7,   "1 ");
                                        //ps.setstring(8,   cp_id);
                                        //ps.setstring(9,   cp_emp_id);
                                        ps.addbatch();

//   =============================================================================
                                    //     if   (recexceltype1.getcode()     !=   null   &&   recexceltype1.getcode().trim().length()   >   2)
                                    //     {
                                    //             psfolder.setstring(1,   recexceltype.getfolderid().trim());
                                      //           psfolder.setlong(2,   primarykey[i]);
                                      //           psfolder.addbatch();
                                      //           isfolder   =   true;
                                    //     }
//   =============================================================================

                                }
                                ps.EXECutebatch();
                                if   (isfolder)
                                {
                                        psfolder.EXECutebatch();
                                }
                                conn.commit();
                        }   catch   (exception   e)
                        {
                                try
                                {
                                        conn.rollback();
                                }   catch   (sqlexception   e1)
                                {
                                        e1.printstacktrace();
                                }
                        }
                        finally
                        {
                                if   (psfolder   !=   null)
                                {
                                        try
                                        {
                                                psfolder.close();
                                        }   catch   (sqlexception   e)
                                        {
                                                e.printstacktrace();
                                        }
                                }
                                if   (ps   !=   null)
                                {
                                        try
                                        {
                                                ps.close();
                                        }   catch   (sqlexception   e)
                                        {
                                                e.printstacktrace();
                                        }
                                }
                        }
                }
        }
      public   static   string   casetype(hssfcell   cell)
        {
                string   str   =   " ";
                if   (cell   ==   null)
                {
                        return   str;
                }
                //要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
                if   (cell.getcelltype()   ==   hssfcell.cell_type_numeric)
                {
                        decimalformat   df   =   new   decimalformat( "0.00 ");
                        string   cstr   =   df.format(cell.getnumericcellvalue());
//                         str   =   double.tostring();
                        string   str2   =   " ";
                        if   (cstr.indexof( ". ")   !=   -1)
                        {
                                str2   =   cstr.substring(0,   cstr.indexof( ". "));
                        }
                        double   tt   =   double.parsedouble(str2);
                        double   d   =   cell.getnumericcellvalue();
                        if   (tt   ==   d)
                        {
                                str   =   str2;
                        }
                        else
                        {
                                str   =   cstr;
                        }
                }
                else   if   (cell.getcelltype()   ==   hssfcell.cell_type_blank)
                {
                        str   =   " ";
                }
                else
                {
                        str   =   cell.getstringcellvalue();
                }

                return   str;
        }
}

发表于:2007-06-07 15:08:111楼 得分:0
错误提示如下:
description   the   server   encountered   an   internal   error   ()   that   prevented   it   from   fulfilling   this   request.

exception  

java.lang.illegalargumentexception:   resources   cannot   be   null.
org.apache.commons.validator.validator. <init> (validator.java:188)
org.apache.struts.validator.resources.initvalidator(resources.java:475)
org.apache.struts.validator.validatorform.validate(validatorform.java:104)
org.apache.struts.action.requestprocessor.processvalidate(requestprocessor.java:927)
org.apache.struts.action.requestprocessor.process(requestprocessor.java:204)
org.apache.struts.action.actionservlet.process(actionservlet.java:1196)
org.apache.struts.action.actionservlet.dopost(actionservlet.java:432)
javax.servlet.http.httpservlet.service(httpservlet.java:709)
javax.servlet.http.httpservlet.service(httpservlet.java:802)
com.dsweb.filter.encodefilter.dofilter(encodefilter.java:54)

发表于:2007-06-07 15:10:272楼 得分:0
uploadkehuxinxi.java可能太长了
修改如下:

uploadkehuxinxi.java
--------------------
package   com.dsweb.action;

import   com.dsweb.form.recexceltype;
import   com.dsweb.util.dbutil;

import   org.apache.poi.hssf.usermodel.hssfcell;
import   org.apache.poi.hssf.usermodel.hssfrow;
import   org.apache.poi.hssf.usermodel.hssfsheet;
import   org.apache.poi.hssf.usermodel.hssfworkbook;

import   java.io.file;
import   java.io.fileinputstream;
import   java.sql.connection;
import   java.sql.preparedstatement;
import   java.sql.sqlexception;
import   java.util.arraylist;
import   java.util.list;
import   java.text.decimalformat;

public   class   uploadkehuxinxi
{
        private   string   restype;

        public   uploadkehuxinxi(string   restype)
        {
                this.restype   =   restype;
        }

        public   void   renamefile()
        {

        }

          //string   filepath
        public   list   readexcel()   throws   exception
        {       system.out   .println( "##################### ");
                list   list   =   new   arraylist();
                file   file   =   new   file( "d:/aaa.xls ");
                if   (!file.exists())
                {
                        return   null;
                }
                fileinputstream   is   =   null;
                try
                {
                        is   =   new   fileinputstream(file);
                        hssfworkbook   workbook   =   new   hssfworkbook(is);
                        hssfsheet   sheet   =   workbook.getsheetat(0);
                        hssfcell   cell   =   null;
                        int   rowcount   =   sheet.getphysicalnumberofrows();
                        for   (int   i   =   1;   i   <   rowcount;   i++)
                        {
                                recexceltype   ret   =   new   recexceltype();
                                hssfrow   arow   =   sheet.getrow(i);
                                //ret.setcode(restype);//资源类型;

                                cell   =   arow.getcell((short)   0);
                                ret.setcode(casetype(cell));                           //管理码

                                cell   =   arow.getcell((short)   1);
                                ret.setidentifier(casetype(cell));               //纳税识别码

                                cell   =   arow.getcell((short)   2);                 //单位名称
                                ret.setcompany_name(casetype(cell));

                                  cell   =   arow.getcell((short)   2);                 //密码
                                ret.setpassword(casetype(cell));

                                list.add(ret);
                        }
                }
                catch   (exception   e)
                {
                        e.printstacktrace();
                }
                finally
                {
                        if   (is   !=   null)
                        {
                                is.close();
                        }
                }
                return   list;
        }

        public   void   storeresource(list   list,string   cp_id,string   cp_emp_id)   throws   exception
        {
                if   (list   ==   null)
                {
                        return;
                }
                else
                {
                        connection   conn   =   dbutil.getcurrentconnection();
                        preparedstatement   ps   =   null;

                        preparedstatement   psfolder   =   null;


                        string   sql   =   "insert   into   company_extend(code,idendifier, "   +
                                        "   password,company_name,tel1,mobile1,tel2,mobile2,open_sms1,open_sms2, "   +
                                        "   valid_date,create_time,address,status,postcode,last_mod_time,update_status "   +

                                        "   )   values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

                        boolean   isfolder   =   false;
                        try
                        {
                                ps   =   conn.preparestatement(sql);
 
                                for   (int   i   =   0;   i   <   list.size();   i++)
                                {
                                        recexceltype   recexceltype1   =   (recexceltype)   list.get(i);
                                                         
                                        ps.setstring(1,   recexceltype1.getcode());
                                        ps.setstring(2,   recexceltype1.getidentifier());
                                        ps.setstring(3,   recexceltype1.getcompany_name());
                                        ps.setstring(4,   recexceltype1.getpassword());


                                                                              ps.addbatch();


                                }
                                ps.EXECutebatch();
                                if   (isfolder)
                                {
                                        psfolder.EXECutebatch();
                                }
                                conn.commit();
                        }   catch   (exception   e)
                        {
                                try
                                {
                                        conn.rollback();
                                }   catch   (sqlexception   e1)
                                {
                                        e1.printstacktrace();
                                }
                        }
                        finally
                        {
                                if   (psfolder   !=   null)
                                {
                                        try
                                        {
                                                psfolder.close();
                                        }   catch   (sqlexception   e)
                                        {
                                                e.printstacktrace();
                                        }
                                }
                                if   (ps   !=   null)
                                {
                                        try
                                        {
                                                ps.close();
                                        }   catch   (sqlexception   e)
                                        {
                                                e.printstacktrace();
                                        }
                                }
                        }
                }
        }
      public   static   string   casetype(hssfcell   cell)
        {
                string   str   =   " ";
                if   (cell   ==   null)
                {
                        return   str;
                }
                //要根据单元格的类型分别做处理,否则格式化过的内容可能会不正确
                if   (cell.getcelltype()   ==   hssfcell.cell_type_numeric)
                {
                        decimalformat   df   =   new   decimalformat( "0.00 ");
                        string   cstr   =   df.format(cell.getnumericcellvalue());
                        string   str2   =   " ";
                        if   (cstr.indexof( ". ")   !=   -1)
                        {
                                str2   =   cstr.substring(0,   cstr.indexof( ". "));
                        }
                        double   tt   =   double.parsedouble(str2);
                        double   d   =   cell.getnumericcellvalue();
                        if   (tt   ==   d)
                        {
                                str   =   str2;
                        }
                        else
                        {
                                str   =   cstr;
                        }
                }
                else   if   (cell.getcelltype()   ==   hssfcell.cell_type_blank)
                {
                        str   =   " ";
                }
                else
                {
                        str   =   cell.getstringcellvalue();
                }

                return   str;
        }
}
发表于:2007-06-07 15:13:253楼 得分:0
java.lang.illegalargumentexception:   resources   cannot   be   null.

传递的参数中有为null的对象.   调用前应检测一下相关参数的值.
发表于:2007-06-07 15:25:384楼 得分:0
纠正一个小错误
cell   =   arow.getcell((short)   2);                 //密码       ----》改为(short)3
ret.setpassword(casetype(cell));
发表于:2007-06-07 15:33:195楼 得分:0
theforever(碧海情天)  
----------------

我己经将参数凭闭了我是用定值来处理的
如:
public   list   readexcel()   throws   exception
        {       system.out   .println( "##################### ");
                list   list   =   new   arraylist();
                file   file   =   new   file( "d:/aaa.xls ");
                if   (!file.exists())


我应该如何改
发表于:2007-06-07 16:03:496楼 得分:0
怎么人回呀分不够可能再加
发表于:2007-06-07 16:45:257楼 得分:0
ps   =   conn.preparestatement(sql);
 
for   (int   i   =   0;   i   <   list.size();   i++)
{
      recexceltype   recexceltype1   =   (recexceltype)   list.get(i);
                                                         
      ps.setstring(1,   recexceltype1.getcode());
      ps.setstring(2,   recexceltype1.getidentifier());
      ps.setstring(3,   recexceltype1.getcompany_name());
      ps.setstring(4,   recexceltype1.getpassword());

      ps.addbatch();
}

¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦
估计是有的格子里的值是null
ps设置sql参数时就出异常了
跟一下这段代码看看
发表于:2007-06-07 17:54:548楼 得分:0
关键是下面的都没有出来不可能往下走的  
system.out   .println( "##################### ");
发表于:2007-06-07 21:26:229楼 得分:0
关键是我根本没有进入我的uploadkehuxinxi.java
为什么
发表于:2007-06-07 23:12:4010楼 得分:0
现在己进去了,但到了
  cell   =   arow.getcell((short)   0);
  ret.setcode(casetype(cell));                           //管理码
tomcat就出错了

提示:
java.lang.nullpointerexception
                at   com.dsweb.action.uploadkehuxinxi.readexcel(uploadkehuxinxi.java:59)
                at   sun.reflect.nativemethodaccessorimpl.invoke0(native   method)
                at   sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.
java:39)
                at   sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodacces
sorimpl.java:25)
                at   java.lang.reflect.method.invoke(method.java:324)
                at   org.apache.struts.actions.dispatchaction.dispatchmethod(dispatchactio
n.java:274)
                at   org.apache.struts.actions.dispatchaction.EXECute(dispatchaction.java:
194)
                at   org.apache.struts.action.requestprocessor.processactionperform(reques
tprocessor.java:419)
                at   org.apache.struts.action.requestprocessor.process(requestprocessor.ja
va:224)
                at   org.apache.struts.action.actionservlet.process(actionservlet.java:119
6)
                at   org.apache.struts.action.actionservlet.dopost(actionservlet.java:432)

                at   javax.servlet.http.httpservlet.service(httpservlet.java:709)
                at   javax.servlet.http.httpservlet.service(httpservlet.java:802)
                at   org.apache.catalina.core.applicationfilterchain.internaldofilter(appl
icationfilterchain.java:237)
                at   org.apache.catalina.core.applicationfilterchain.dofilter(applicationf
ilterchain.java:157)
                at   com.dsweb.filter.encodefilter.dofilter(encodefilter.java:54)
                at   org.apache.catalina.core.applicationfilterchain.internaldofilter(appl
icationfilterchain.java:186)
                at   org.apache.catalina.core.applicationfilterchain.dofilter(applicationf
ilterchain.java:157)
                at   org.apache.catalina.core.standardwrappervalve.invoke(standardwrapperv
alve.java:214)
                at   org.apache.catalina.core.standardvalvecontext.invokenext(standardvalv
econtext.java:104)
                at   org.apache.catalina.core.standardpipeline.invoke(standardpipeline.jav
a:520)
                at   org.apache.catalina.core.standardcontextvalve.invokeinternal(standard
contextvalve.java:198)
                at   org.apache.catalina.core.standardcontextvalve.invoke(standardcontextv
alve.java:152)
                at   org.apache.catalina.core.standardvalvecontext.invokenext(standardvalv
econtext.java:104)
                at   org.apache.catalina.core.standardpipeline.invoke(standardpipeline.jav
a:520)
                at   org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.j
ava:137)
                at   org.apache.catalina.core.standardvalvecontext.invokenext(standardvalv
econtext.java:104)
                at   org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.j
ava:118)
                at   org.apache.catalina.core.standardvalvecontext.invokenext(standardvalv
econtext.java:102)
                at   org.apache.catalina.core.standardpipeline.invoke(standardpipeline.jav
a:520)
                at   org.apache.catalina.core.standardenginevalve.invoke(standardengineval
ve.java:109)
                at   org.apache.catalina.core.standardvalvecontext.invokenext(standardvalv
econtext.java:104)
                at   org.apache.catalina.core.standardpipeline.invoke(standardpipeline.jav
a:520)
                at   org.apache.catalina.core.containerbase.invoke(containerbase.java:929)

                at   org.apache.coyote.tomcat5.coyoteadapter.service(coyoteadapter.java:16
0)
                at   org.apache.coyote.http11.http11processor.process(http11processor.java
:799)
                at   org.apache.coyote.http11.http11protocol$http11connectionhandler.proce
ssconnection(http11protocol.java:705)
                at   org.apache.tomcat.util.net.tcpworkerthread.runit(pooltcpendpoint.java
:577)
                at   org.apache.tomcat.util.threads.threadpool$controlrunnable.run(threadp
ool.java:683)
                at   java.lang.thread.run(thread.java:534)


快速检索

最新资讯
热门点击