excel 文件倒入数据库

<html:form action="/smsuserexcelimport.do" method="POST"
  enctype="multipart/form-data">
  <bean:define id='formUpload' name='DvDxjgDMTForm'
   type='com.highsoft.sms.DvDxjgDMTForm'></bean:define>
   <%
    String typeCode = request.getParameter("typeCode");
    System.out.println(typeCode);
    formUpload.setReservedProp("typeCode",typeCode);
   %>
 <table width="100%" cellspacing="0" cellpadding="0" align="center">
    <tr>
      <td width="50%"  align="left">请选择文件: </td>
      <td width="30%" align="left">
        <html:file property="myUploadFile"></html:file>
        </td>
    </tr>
    <tr>
      <td height="39">&nbsp;</td>
      <td width="38%" align="left">
        <html:button property="upLoad_Data" οnclick="fConfirm()" value="导入" styleClass="btn" /><input type="reset" name="Clear" class="btn" value="清除文件">
      </td>
      <td width="38%" align="left">&nbsp;</td>
    </tr>
  </table>
  <html:hidden property='action' />
  <html:hidden property="reservedProp(typeCode)" />
  <html:hidden property="reservedProp(MSG)" />
 </html:form>
</body>

public ActionForward upLoadFile(ActionMapping actionMapping,
   ActionForm actionForm, HttpServletRequest httpServletRequest,
   HttpServletResponse httpServletResponse) throws Exception {
  System.err.println("upLoadFile");
  System.err.println("user_type:" + user_type);
  DvDxjgDMTForm form = (DvDxjgDMTForm) actionForm;
  UserTransaction userTransaction = null;

  Workbook wwb = null;
  FormFile myFile = null;
  InputStream streamIn = null;
  DBTool dbtool = new DBTool();
  StringBuffer errMSG = new StringBuffer();
//   事务开始
  userTransaction = ServiceLocator.getInstance()
    .getUserTransaction();
  try {
   
   userTransaction.begin();

   String dir = this.getServlet().getServletContext().getRealPath(
     "/user_indexs");
   log.debug(dir);
   myFile = form.getMyUploadFile();
   if (myFile.getFileName() == null || myFile.getFileName().equals("")) {
    form.setReservedProp("MSG", "请先选择上传文件!");
    return actionMapping.getInputForward();
   } else {
    streamIn = myFile.getInputStream();
    String fname = myFile.getFileName();
    String endName = myFile.getFileName().substring(
      myFile.getFileName().length() - 3,
      myFile.getFileName().length());
    if (!endName.equals("xls")) {
     log.debug("上传文件格式不正确!");
     form.setReservedProp("MSG", "上传文件格式不正确!");
     // this.initForm(actionMapping, actionForm,
     // httpServletRequest, httpServletResponse);
     return actionMapping.getInputForward();
    }
    log.debug("fileName=" + fname);
    WorkbookSettings workbookSettings = new WorkbookSettings();
    workbookSettings.setEncoding("ISO-8859-1"); // 解决中文乱码
    wwb = Workbook.getWorkbook(streamIn, workbookSettings);
    Sheet[] sheets = wwb.getSheets();
    int rows = sheets[0].getRows();
    // ArrayList queryList = new ArrayList();
    //Cell[] cell = sheets[0].getRow(0);
    /*String test = cell[0].getContents().trim();
    // System.out.println(test);
    // 判断格式
    if (!test.equals("序号")) {
     debug("上传内容格式不正确!");
     form.setReservedProp("MSG", "上传内容格式不正确!");
     // this.initForm(actionMapping, actionForm,
     // httpServletRequest, httpServletResponse);
     return actionMapping.getInputForward();
    }*/
    user_type = form.getReservedProp("typeCode");
    if (user_type == null || user_type.equalsIgnoreCase("")
      || user_type == "0" || user_type.equals("0")) {
     log.debug("请选择需导入的短信群组!");
     form.setReservedProp("MSG", "请选择需导入的短信群组!!");
     // this.initForm(actionMapping, actionForm,
     // httpServletRequest, httpServletResponse);
     return actionMapping.getInputForward();
    }
    for (int i = 1; i < rows; i++) {

     Cell[] cells = sheets[0].getRow(i);

     String user_name = cells[0].getContents().trim();
     String mobile = cells[1].getContents().trim();
     String phone = cells[2].getContents().trim();
     String address = cells[3].getContents().trim();
     String sql = "select * from SMS_USER_LIST where USER_NAME='"+user_name+"' and MOBILE ="+mobile;
     Record[] r = dbtool.executeQuery(sql, null);
     if(r.length<1){ //存在的用戶就別導了
     log.debug("用户:" + user_name + "导入了");
     errMSG.append("a");

     // 插入
     ArrayList<Object> list = new ArrayList<Object>();
     String sqlStr = "INSERT INTO SMS_USER_LIST (USER_NO,USER_NAME,MOBILE,PHONE,ADDRESS,USER_TYPE,IS_AUTO)VALUES(?,?,?,?,?,?,'Y')";
     list.clear();
     String userNo = SequenceUtil
       .genSequenceNo("SMS_USER_LIST");
     ; //设置主键
     list.add(userNo);
     list.add(user_name);
     list.add(mobile);
     list.add(phone);
     list.add(address);
     list.add(user_type);

     dbtool.executeUpdate(sqlStr, list);
    }
    }

   }
   wwb.close();
   userTransaction.commit();

  } catch (Exception ex) {
   ex.printStackTrace();
   //System.out.println("ROLLBACK");
   userTransaction.rollback();
   throw new highsoft.framework.exceptions.BaseRunException("", ex);
  } finally {
   if (wwb != null) {
    wwb.close();
   }
   if (myFile != null) {
    myFile = null;
   }
   if (streamIn != null) {
    streamIn.close();
   }
  }
  form.setReservedProp("", "导入成功!");
  return actionMapping.getInputForward();
 }

public class DvDxjgDMTForm extends ClientDataSetForm {
 private static final long serialVersionUID = 1L;

 private String action;


 private FormFile myUploadFile;

 public FormFile getMyUploadFile() {
  return myUploadFile;
 }

 public void setMyUploadFile(FormFile myUploadFile) {
  this.myUploadFile = myUploadFile;
 }

 public String getAction() {
  return action;
 }

 public void setAction(String action) {
  this.action = action;
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值