介绍步骤
1.首先导入easyexcel依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>1.1.2-beta5</version>
</dependency>
<dependency>
2.然后在untils里面写出方法类!
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import java.io.OutputStream;
import java.util.List;
/**
* easyExcel方法
* @author 19225
*
*/
public class ExcelBuilder {
//这是一个方法类,如果要写多sheet就,多加入几个List就可以解决,但如果太多sheet就需要写入新的方法了,这个方法可能就会不适用。
public static void build(OutputStream out,List list, List list1,Class <? extends BaseRowModel> a,Class <? extends BaseRowModel> b) throws Exception {
ExcelWriter writer = EasyExcelFactory.getWriter(out);
//对应的为第一张表,从第0行开始,载入的类为a
Sheet sheet = new Sheet(1,0, a);
//自定义表名
sheet.setSheetName("统计表");
Sheet sheet1 = new Sheet(2,0,b);
sheet1.setSheetName("测试表");
writer.write(list,sheet);
writer.write(list1,sheet1);
writer.finish();
//一定要关闭输出流
out.close();
}
}
3.接着写对应的实体类
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.security.PrivateKey;
@Data
@AllArgsConstructor
@NoArgsConstructor
//一定要加@Buileder
@Builder
public class MatchingViewEntity extends BaseRowModel {
//字段名
//这里是说excel的头行的属性名,index=0,代表把这个属性值放在第一列
@ExcelProperty(value = "字段名",index = 0)
private String filedName;
//是否匹配
@ExcelProperty(value = "是否匹配",index = 1)
private String match;
//资源项
@ExcelProperty(value = "资源项",index = 2)
private String zyx;
}
另外的实体类与之类似,根据自己的要求来写就可以。
4.接着是前端,首先写一个html标签按钮
//写一个下载报告 点击事件
<input type="button" class="btn btn-danger" id="download" ms-on-click="viewresult"
value="下载报告"/>
5.js写一个函数
viewresult:function(){
//这是自己写的对应数据库查找的id,可以不加
let taskId= sessionStorage.getItem("taskId");
//请求路径
window.location.href="(里面放自己的请求路径)"+taskId;
6.写controller层
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.gson.JsonObject;
import com.zhibeitech.common.utils.HttpContextUtils;
import com.zhibeitech.common.utils.R;
import com.zhibeitech.modules.jtbresource.entity.*;
import com.zhibeitech.modules.api.utils.ExcelBuilder;
import com.zhibeitech.modules.jtbresource.service.ExcludeService;
import com.zhibeitech.modules.jtbresource.service.TaskService;
import com.zhibeitech.modules.jtbresource.service.ZYMLService;
import com.zhibeitech.modules.jtbresource.service.ZYMLXXService;
import org.apache.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static com.baidu.translate.demo.IndexOperate.matchedIndex;
/**
* Excel下载 EasyExcel
* @author 19225
* @email 1922569996@qq.com
* @date 2020年10月14日 上午10:58:10
*/
@RestController
@RequestMapping("请求地址")
@ResponseBody
public class downloadController {
private static Logger logger = Logger.getLogger(downloadController.class);
@Autowired
private TaskService taskService;
@Autowired
private ZYMLService zymlService;
@Autowired
private ExcludeService excludeService;
@Autowired
private ZYMLXXService zymlxxService;
@Value("${projectfilepath.rootpath}")
private String rootpath;
@RequestMapping("test/{taskId}")
//这个还是跟着我的id来的,用来查找数据库里的数据,可以不加
@ResponseBody
//这个也可以直接响应,删掉@PathVariable("taskId") Integer taskId
public String download(@PathVariable("taskId") Integer taskId, HttpServletResponse response) throws IOException {
String fileName = "数据统计表";
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1") + ".xls");
OutputStream out = response.getOutputStream();
//中间写两个list存入自己的实体类,输出为excel格式
//举个例子
for (int i = 0; i <10 ; i++) {
Attributes a = new Attributes();
a.setCount(i);
a.setField("属性");
l1.add(a);
}
List<MatchingViewEntity> l2 = new ArrayList<>();
for (int i = 0; i <10 ; i++) {
MatchingViewEntity m = new MatchingViewEntity();
m.setZyx("1");
m.setMatch("2");
m.setFiledName("3");
l2.add(m);
}
try {
ExcelBuilder.build(out,l1, l2, Attributes.class, MatchingViewEntity.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
7.测试
有了按钮
点击以后,可以下载!!!
任务完成!
PS:心得
本人是实习生,这是公司里要求做的第一个小需求,很简单,我却用了很多天,问了同学,查了很多东西。没报过班,都是自己钻研,时间也没有多久,就来实习了,也算是懂一点点关于java的东西吧,方法什么的可以写出来,但就是不知道如何响应前端,对后台处理的数据也想了很久,不知道流程是怎么来的,不过也算是一点点慢慢懂得很多东西了。
如果你看到我这篇文章,如果你也是个小白,在公司里被leader说,不要怕,撑过去,一点点来,也别太自责,大家都是这么过来的,多找资料,多动脑子,想明白这件事情的流程和前因后果,为什么这么做,别放弃,没有谁是一下子就懂得了的,相信自己,希望这篇文章能帮到你!!