springMVC jxl数据库导入Excel文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }
    }
    
}

 

以上是关于springMVC jxl数据库导入Excel文件的主要内容,如果未能解决你的问题,请参考以下文章

Java jxl导入excel文件,导入的数字身份证号码手机号变成了科学计数法,解决方案

用SpringMvc实现Excel导出功能

java中如何讲Excel中的数据导入到Mysql

jxl切割excel文件

jxl 导入excel

jxl导入/导出excel