java导出excel并实现下载功能
程序员文章站
2022-03-15 10:15:15
...
java导出excel并实现下载功能
- 这里我们使用alibaba的依赖包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
- entity
- 这里如果不希望某个字段显示,就在该字段上加上@ExcelIgnore注解
package com.infoearth.modules.survey.entity;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 实体类
*
* @author BladeX
* @since 2021-02-24
*/
@Data
@TableName("SURVEYJMRECORD")
//@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "Surveyjmrecord对象", description = "机民井统测记录表")
@ColumnWidth(value = 13)
public class Surveyjmrecord implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键 (机民井统测记录表)
*/
@ExcelIgnore
@ApiModelProperty(value = "主键 (机民井统测记录表)")
@TableId("ID")
private String id;
/**
* 统测点号,外键,与水文地质点调查表ID关联
*/
@ExcelIgnore
@ApiModelProperty(value = "统测点号,外键,与水文地质点调查表ID关联")
@TableField("POINTID")
private String pointid;
/**
* 野外编号
*/
@ExcelProperty(value = "野外编号")
@ApiModelProperty(value = "野外编号")
@TableField("GCEABC")
private String gceabc;
/**
* 统测期次
*/
@ExcelProperty(value = "统测期次")
@ApiModelProperty(value = "统测期次")
@TableField("OBSERVE_PERIOD")
private String observePeriod;
/**
* 测点高程
*/
@ExcelProperty(value = "测点高程")
@ApiModelProperty(value = "测点高程")
@TableField("MEAPOINTELEV")
private Float meapointelev;
/**
* 测点距地面高度
*/
@ExcelProperty(value = "测点距地面高度")
@ApiModelProperty(value = "测点距地面高度")
@TableField("MEAGROHEIGHT")
private Float meagroheight;
}
- serviceimpl
- 我这里是多个sheet导出,如果小伙伴要单sheet就写一个就可以了
public String exportExcelByCode(String code) throws IOException {
//这里的path是上传的服务器地址+编码(为了区别)
String path = baseFilePath + code + "survey.xlsx";
FileOutputStream outputStream = new FileOutputStream(path);
//导出多个sheet,getjmList(code)是查出来List数据集合
ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
excelWriter.write(getjmList(code),EasyExcel.writerSheet(0,"机民井统测记录表").head(Surveyjmrecord.class).build());
excelWriter.write(getqdList(code),EasyExcel.writerSheet(1,"泉点统测记录表").head(Surveysprrecord.class).build());
excelWriter.write(getAnhList(code),EasyExcel.writerSheet(2,"地下暗河统测记录表").head(Surveyriverrecord.class).build());
excelWriter.write(gethkList(code),EasyExcel.writerSheet(3,"湖库统测记录表").head(Surveylakeresrecord.class).build());
excelWriter.write(gethlList(code),EasyExcel.writerSheet(4,"河流统测记录表").head(Surveyrivernairerecord.class).build());
excelWriter.finish();
return path;
}
- controller层下载
public void exportExcel(@RequestParam String code,HttpServletResponse response) throws IOException {
String path = basicinfoService.exportExcelByCode(code);
File file = new File(path);
String fileName = file.getName();
InputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[is.available()];
is.read(bytes);
is.close();
//清空response
response.reset();
// 设置response的Header,设置浏览器
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(bytes);
toClient.flush();
toClient.close();
}
- 好了,excel导出功能到这里就结束了,小伙伴们直接可以拿来即用
- 此文章主要记录遇到的问题,方便以后的查看
上一篇: 使用node创建一个简单的api服务器
下一篇: 前后端分离下载Excel文件