这是个开源的工具,只需要一行代码就可以搞定数据导入导出,而不用自己去写大量的工具类。 官网教程
pom.xml:
cn.afterturn
easypoi-base
3.0.3
cn.afterturn
easypoi-web
3.0.3
cn.afterturn
easypoi-annotation
3.0.3
DAO类:
package com.lingfei.admin.entity;
import cn.afterturn.easypoi.excel.annotation.Excel;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
import com.gitee.sunchenbin.mybatis.actable.command.BaseModel;
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Timestamp;
import java.util.Date;
/**
* @author 熊义杰
* @date 2019-3-17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "announce")
public class Announce extends BaseModel {
@Excel(name = "序号", orderNum = "0")
@Column(name = "id",type = MySqlTypeConstant.INT, length = 10,isKey = true,isAutoIncrement = true)
private int id;
@Excel(name = "内容", orderNum = "1")
@Column(name = "content",type = MySqlTypeConstant.VARCHAR,length = 400)
private String content;
@Excel(name = "时间", exportFormat = "yyyy-MM-dd hh:mm" ,orderNum = "2")
@Column(name = "date",type = MySqlTypeConstant.DATETIME)
private Timestamp date;
}
我这里使用了大量的注解,自动创建getter和setter的注解,还有自动创建表的注解
工具类:
package com.lingfei.admin.utils;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.sun.deploy.net.URLEncoder;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @author www.xyjz123.xyz
* @date 2019/3/18 18:09
*/
public class ExcelUtils {
public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List list, String title, String sheetName, Class pojoClass,String fileName, HttpServletResponse response){
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List
上面一些工具是用不到的,可以自己选择使用。
control类:
package com.lingfei.admin.control;
import com.lingfei.admin.entity.Announce;
import com.lingfei.admin.service.impl.AnnounceServiceImpl;
import com.lingfei.admin.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* @author www.xyjz123.xyz
* @date 2019/3/18 18:25
*/
@Controller
public class AdminExcelControl {
@Autowired
AnnounceServiceImpl announceService;
@GetMapping("/getExcel")
public void export(HttpServletResponse response){
List announces = announceService.getAllResult();
//导出到Excel
ExcelUtils.exportExcel(announces,"公告记录","jie",Announce.class,"测试.xls",response);
}
}
然后在前端加个连接或者直接浏览器调用接口就可以导出了。