SSH+Mysql实现的excel文件上传下载并解析数据到数据库的实例(功能包含excel上传下载)

servlet+Mysql实现的校园论坛管理系统

本系统是一个简单的校园论坛系统,学生可以在线发帖并进行帖子评论回复,同同时管理员可以对用户进行管理。
(文末查看完整源码)

实现功能截图

excel上传
在这里插入图片描述
在这里插入图片描述
上传成功excel下载
在这里插入图片描述

系统功能

本系统实现了以下功能:
1、excel上传
2、excel下载

使用技术

数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:SSH

项目结构
在这里插入图片描述

代码

java端
实体类
User.java

package com.spark.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_user")
public class User implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -9042615274714038279L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(name="number")
    private Integer number;
    
    @Column(name = "name")
    private String name;

    @Column(name = "password")
    private String password;

    @Column(name = "run")
    private double run;

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the number
	 */
	public Integer getNumber() {
		return number;
	}

	/**
	 * @param number the number to set
	 */
	public void setNumber(Integer number) {
		this.number = number;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the run
	 */
	public double getRun() {
		return run;
	}

	/**
	 * @param run the run to set
	 */
	public void setRun(double run) {
		this.run = run;
	}

	

    
   
}

FileUploadDaoImpl.java

package com.spark.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.spark.dao.FileUploadDao;

@Repository
public class FileUploadDaoImpl implements FileUploadDao{

	@Autowired
	private SessionFactory sessionFactory;

	@Transactional
	public void save(Object object) {
		sessionFactory.getCurrentSession().save(object);
	}

}

UploadController.java

package com.spark.controller;

import java.util.List;
import java.awt.Menu;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.spark.bean.User;
import com.spark.service.FileUploadService;


@Controller
public class UploadController {
	
	@Resource(name="service")
	private FileUploadService fileUploadService1;
		
	@RequestMapping("/uploadfile")
	public String toUpload(){
		return "upload";
	}
	@RequestMapping("/form")
	public String upload(@RequestParam(value="file",required=false) MultipartFile file,HttpServletRequest request,ModelMap model,Model mod) throws Exception{
		String path=request.getSession().getServletContext().getRealPath("upload");
		//文本路径
		System.out.println(path);
		String fileName=file.getOriginalFilename();
		File targetFile=new File(path,fileName);
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
	    // 获得上传文件的文件扩展名
	    String subname = fileName.substring(fileName.lastIndexOf(".")+1);
	    System.out.println("文件的扩展名:"+subname);
		//保存
		try{
			file.transferTo(targetFile);
			model.addAttribute("fileUrl",request.getSession().getServletContext().getRealPath("upload")+"/"+fileName);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		String rootpath = path + File.separator + fileName;
		System.out.println(rootpath);
		List<User> excelList = fileUploadService1.readExcel(rootpath);
		for(User user : excelList){
			fileUploadService1.save(user);
		}
		int len = excelList.size();
		System.out.println("集合的大小为:"+len);
		
		return "result";
	}

}

完整源码

觉得有用,记得一键三连哦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anmu4200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值