| 发表于: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; } } |
|
|
|
|