@Autowired
private ExcelService excelService;
//跳转到上传文件的页面
@RequestMapping(value = "", method = RequestMethod.GET)
public String goUpload() {
//跳转到 templates 中tools目录下的 upload.html
return "tools/upload";
}
@RequestMapping(value = "/excel",method = RequestMethod.POST)
public String upload(MultipartFile file, Model model) throws Exception {
boolean flag = excelService.getExcel(file);
if(flag){
model.addAttribute("Message", "上传成功");
}else{
model.addAttribute("Message", "上传失败");
}
return "tools/upload";
}
}
Excel实体:
package org.meng.project.entity;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
/**
*
用户实体类
* @ClassName User
* @Author MengMeng
* @Date 2018/10/6
* @Version: 0.1
* @Since JDK 1.80_171
*/
@Entity
@Table(name = "test", schema = "project")
public class Excel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(length=36)
private String id;
@Column(length=45,nullable=false,unique=true)
private String username;
@Column(length=100,nullable=false,unique=true)
private String email;
@Column(length=45,nullable=false)
private String password;
@Column(length=45)
private String role;
public Excel() {
}
public Excel(Excel user){
this.id = user.getId();
this.username = user.getUsername();
this.role = user.getRole();
this.email = user.getEmail();
this.password = user.getPassword();
}
//get 和 set
}
Service:
package org.meng.project.service;
import com.alibaba.fastjson.JSON;
import org.meng.project.entity.*;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
/**
*
Excel的Service类接口
*
Excel的Service类接口,与Excel有关的业务逻辑方法
* @Author MengMeng
* @Date 2018/10/6
* @version: 0.1
* @since JDK 1.80_144
*/
public interface ExcelService {
boolean getExcel(MultipartFile file) throws Exception;
}
ServiceImpl:
package org.meng.project.service;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.meng.project.entity.*;
import org.meng.project.repository.ExcelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.*;
/**
*
Excel的Servic
来源【首席数据官】,更多内容/合作请关注「辉声辉语」公众号,送10G营销资料!
版权声明:本文内容来源互联网整理,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 jkhui22@126.com举报,一经查实,本站将立刻删除。