springboot实现从数据库导出数据到Excel表格中(完整代码)

1、pom.xml,除了mybatis数据库依赖之外,要加上以下的依赖。


        
            org.apache.poi
            poi
            3.17
        
        
            org.apache.poi
            poi-ooxml
            3.17
        
        
            org.xmlunit
            xmlunit-core
        

2.前端页面




    
    Title
    


数据导出

3.配置文件

springboot实现从数据库导出数据到Excel表格中(完整代码)_第1张图片

4.建立数据库

springboot实现从数据库导出数据到Excel表格中(完整代码)_第2张图片

5.实体类

package com.wei.domain;

public class Teacher {
    private Integer tno;
    private String tName;
    private String type;
    private String tPassword;
    private String phone;
    private String e_mail;

    public Teacher() {
    }

    public Teacher(Integer tno, String tName, String type, String tPassword, String phone, String e_mail) {
        this.tno = tno;
        this.tName = tName;
        this.type = type;
        this.tPassword = tPassword;
        this.phone = phone;
        this.e_mail = e_mail;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tno=" + tno +
                ", tName='" + tName + '\'' +
                ", type='" + type + '\'' +
                ", tPassword='" + tPassword + '\'' +
                ", phone='" + phone + '\'' +
                ", e_mail='" + e_mail + '\'' +
                '}';
    }

    public Integer getTno() {
        return tno;
    }

    public void setTno(Integer tno) {
        this.tno = tno;
    }

    public String gettName() {
        return tName;
    }

    public void settName(String tName) {
        this.tName = tName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String gettPassword() {
        return tPassword;
    }

    public void settPassword(String tPassword) {
        this.tPassword = tPassword;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getE_mail() {
        return e_mail;
    }

    public void setE_mail(String e_mail) {
        this.e_mail = e_mail;
    }
}

6.mapper接口

package com.wei.mapper;

import com.wei.domain.Teacher;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface teacherMapper {
    @Select("select * from teacher")
    public List teacherinfor();


}

7.service层

package com.wei.service;

import com.wei.domain.Teacher;
import com.wei.mapper.teacherMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class teacherService {
    @Resource
    public teacherMapper teachermapper;
    public List teacherinfor(){
        return teachermapper.teacherinfor();
}

}

8.controller层

package com.wei.controller;

import com.wei.domain.Teacher;
import com.wei.service.teacherService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/")
public class teacherController {
    @Resource
    public teacherService teacherservice;

    @RequestMapping(value = "UserExcelDownloads", method = RequestMethod.GET)
    public void downloadAllClassmate(HttpServletResponse response) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();//创建HSSFWorkbook对象,  excel的文档对象
        HSSFSheet sheet = workbook.createSheet("信息表"); //excel的表单

        List classmateList = teacherservice.teacherinfor();

        String fileName = "userinf"  + ".xls";//设置要导出的文件的名字
        //新增数据行,并且设置单元格数据
        int rowNum = 1;
        String[] headers = { "学号", "姓名", "身份类型", "登录密码"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for(int i=0;ispringboot实现从数据库导出数据到Excel表格中(完整代码)_第3张图片

springboot实现从数据库导出数据到Excel表格中(完整代码)_第4张图片

springboot实现从数据库导出数据到Excel表格中(完整代码)_第5张图片

希望对你有所帮助!!!!!

你可能感兴趣的:(springboot,excel,mybatis,springboot,实现数据导出到excel)