zoukankan      html  css  js  c++  java
  • springMVC jxl数据库导入Excel文件

    1.新建一个jsp表单 用到multipart

      <form id="form2" method="post" enctype="multipart/form-data"     action="<%=basePath%>uploadftp.shtml">
            <input type="file" name="file">
            <input type="submit" value="ftp提交">
        </form>

    2.Controller代码 ftpserver判断 然后是username判断

    如果没有ftpserver添加 添加mapper中设置主键添加

    useGeneratedKeys="true"和主键id对应 Conroller下面的红色代码获得主键
    <insert id="addFtpServer" useGeneratedKeys="true" parameterType="ftpServerBean" keyProperty="Id">
    @Controller
    @RequestMapping("/")
    public class UploadFtpController {
        @Autowired
        private FtpService ftpService;
        
        @Autowired
        private FtpServerService ftpServerService;
        
        @Autowired
        private CustomerService customerService;
        
        @RequestMapping("uploadftppage")
        public String uploadcustomerpage()
        {
            return "help/uploadcustomer";
        }
        /**
         * @param request
         * @param file
         */
        /**
         * @param request
         * @param file
         */
        @RequestMapping("uploadftp")
        public void uploadcustomer(HttpServletRequest request,MultipartFile file)
        {
            System.out.println("-----------"+file);
            UserBean userBean = (UserBean) request.getAttribute("userBean");
            int i;
            Sheet sheet;
            Workbook book;
            Cell cell1,cell2;
            List<CustomerBean> agents = new ArrayList<CustomerBean>();
            try {
                CommonsMultipartFile cf = (CommonsMultipartFile) file;
                DiskFileItem fi = (DiskFileItem) cf.getFileItem();
                
                File f = fi.getStoreLocation();
                // t.xls为要读取的excel文件名
                book = Workbook.getWorkbook(f);
    
                // 获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
                sheet = book.getSheet(0);
    
                i = 1;
                FtpBean ftpBean =null;
                FtpServerBean ftpServerBean = null;
                CustomerBean customerBean = null;
                while (true) {
                    // 获取每一行的单元格
                    ftpServerBean = new FtpServerBean();
                    customerBean = new CustomerBean();
                    ftpBean = new FtpBean();
                    cell1 = sheet.getCell(0, i);// (列,行)
                    if ("".equals(cell1.getContents()) == true)
                        break;    // 如果读取的数据为空
                    ftpServerBean.setServerName(cell1.getContents());
                    cell2 = sheet.getCell(2,i);
                    ftpServerBean.setServerIp(cell2.getContents());
                    //serverid  begin    
                    ftpServerBean = ftpServerService.getFtpServerByServerIP(ftpServerBean);
                    if(ftpServerBean!=null && ftpServerBean.getId()>0)
                    {
                        ftpBean.setServerId(ftpServerBean.getId());//存在
                    }
                    else
                    {    
                        ftpServerBean = new FtpServerBean();
                        ftpServerBean.setServerName(PinYin.addPinYinHeadChar(cell1.getContents()));
                        ftpServerBean.setServerIp(cell2.getContents());
                        ftpServerBean.setCreateUser(userBean.getId());
                        int count =  ftpServerService.addFtpServer(ftpServerBean);
                        ftpBean.setServerId(ftpServerBean.getId());
                    }
                    //serverid  end    
                    
                    //customer
                        cell1 = sheet.getCell(1, i);//客户名
                        customerBean.setName(PinYin.addPinYinHeadChar(cell1.getContents()));
                        customerBean = customerService.selectCustomerByName(customerBean);
                        if (customerBean!=null && customerBean.getId()>0) {
                            ftpBean.setCustomerName(customerBean.getName());
                            ftpBean.setCustomerId(customerBean.getId());
                            ftpBean.setIpAddr(ftpServerBean.getServerIp());
                            ftpBean.setCreateUser(ftpServerBean.getCreateUser());
                        }
                        else {
                            customerBean = new CustomerBean();
                            customerBean.setName(PinYin.addPinYinHeadChar(cell1.getContents()));
                            customerBean.setAddUser(userBean.getId());
                            customerBean.setContacts("**");
                            customerBean.setContactTel("13156878391");
                            customerBean.setSigningTime(new Date());
                            customerBean.setCompletetime(new Date());
                            int count = customerService.insertCustomer(customerBean);
                            ftpBean.setCustomerId(customerBean.getId());
                            ftpBean.setCustomerName(PinYin.addPinYinHeadChar(cell1.getContents()));
                            ftpBean.setIpAddr(ftpServerBean.getServerIp());
                            ftpBean.setCreateUser(ftpServerBean.getCreateUser());
                        }
                        //customer
                            cell1 = sheet.getCell(3, i);
                            ftpBean.setUserName(cell1.getContents());//用户名
                            cell1 = sheet.getCell(4, i);
                            ftpBean.setPassword(cell1.getContents());//密码
                            cell1 = sheet.getCell(5, i);
                            ftpBean.setDomainName(cell1.getContents());//绑定域名
                            cell1 = sheet.getCell(6, i);
                            ftpBean.setDeveLanguage(cell1.getContents());//开发语言
                            cell1 = sheet.getCell(7, i);
                            ftpBean.setSpaceSize(cell1.getContents());//空间大小
                            try {
                                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                cell1 = sheet.getCell(8, i);
                                Date date = dateFormat.parse(cell1.getContents().replace("/", "-"));
                                ftpBean.setBeginTime(date);//开始时间
                                
                            } catch (Exception e) {
                                System.out.println(e);
                                ftpBean.setBeginTime(new Date());//开始时间
                            }
                            try {
                                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                                cell1 = sheet.getCell(9, i);
                                Date date = dateFormat.parse(cell1.getContents().replace("/", "-"));
                                ftpBean.setEndTime(date);//到期时间
                                
                            } catch (Exception e) {
                                System.out.println(e);                            
                                ftpBean.setBeginTime(new Date(new Date().getTime()+365*24*3600));//开始时间
                            }
                            
                            cell1 = sheet.getCell(10, i);
                            ftpBean.setSqlType(cell1.getContents());//数据库
                            cell1 = sheet.getCell(11, i);
                            ftpBean.setSqlName(cell1.getContents());//数据库名
                            cell1 = sheet.getCell(12, i);
                            ftpBean.setSqlUserName(cell1.getContents());//用户名
                            cell1 = sheet.getCell(13, i);
                            ftpBean.setSqlPassword(cell1.getContents());//数据库密码
                            cell1 = sheet.getCell(14, i);
                            ftpBean.setInfo(cell1.getContents());//备注
                            ftpService.addFtp(ftpBean);
                            
                            i++;
                        }
                
                book.close();
            } catch (Exception e) {
    
                e.printStackTrace();
            }
        }
        
    }
  • 相关阅读:
    [转] 美股评论:远离波动的噪音
    [转] GDB 下 watch的使用
    [转] Web性能压力测试工具之ApacheBench(ab)详解
    [转] Ubuntu 12.04下LAMP安装配置 (Linux+Apache+Mysql+PHP)
    [转] 在 Linux 中怎样使用cp命令合并目录树
    [转] postgresql常用命令
    [转] 跟着美联储投资
    [转] 智能指针(三):unique_ptr使用简介
    关于前端开发
    [转] 美股评论:美国散户血泪辛酸
  • 原文地址:https://www.cnblogs.com/zhengteng/p/5193040.html
Copyright © 2011-2022 走看看