你最愿意做的哪件事,才是你的天赋所在

0%

easypoi export excel

导出exceldemo

SmartlampDTO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.Date;

@Data
@EqualsAndHashCode(callSuper = false)
public class SmartlampDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Excel(name="日期",orderNum = "0",width = 20,databaseFormat = "yyyyMMddHHmmss", format = "yyyy-MM-dd-HH:00")
private Date date;
@Excel(name = "路灯开关",orderNum = "2",width = 15)
private String LED_OnOff;
@Excel(name = "空气质量",orderNum = "3",width = 15)
private String air;
@Excel(name = "路灯亮度",orderNum = "4",width = 15)
private Integer LED_Value;
@Excel(name = "光线强度(%)",orderNum = "5",width = 15)
private Integer sunValue;
@Excel(name = "报警器开关",orderNum = "6",width = 15)
private String alarm_OnOff;
@Excel(name = "湿度(%)",orderNum = "7",width = 15)
private Float hum;
@Excel(name = "温度(°C)",orderNum = "8",width = 15)
private Float tem;
}

searchdataController

控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.lab.wisdom.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.lab.wisdom.DTO.ProductDTO;
import com.lab.wisdom.ExcelDTO.PersonExportVo;
import com.lab.wisdom.ExcelDTO.Smartlamp;
import com.lab.wisdom.mapper.WisdomLampMapper;
import com.lab.wisdom.model.wisdomLamp;
import com.lab.wisdom.model.wisdomLampExample;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class searchdataController {
@Autowired
WisdomLampMapper wisdomLampMapper;
@GetMapping("/searchDate")
public String callback(Model model, HttpServletRequest request){
request.getSession().setAttribute("section","searchData");
return "searchData";
}
@RequestMapping(value = "/ExportExcel",method = {RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public void ExportExcel(HttpServletResponse response){
long start = System.currentTimeMillis();
//这里调用mybatis查找数据
List<wisdomLamp> wisdomLampList = wisdomLampMapper.selectByExample(new wisdomLampExample());
List<Smartlamp> smartlampList = new ArrayList<>();
//把数据放进list中
for (wisdomLamp lamp : wisdomLampList) {
Smartlamp smartlamp = new Smartlamp();
smartlamp.setLED_Value(lamp.getLedValue());
smartlamp.setTem(lamp.getTem());
smartlamp.setHum(lamp.getHum());
smartlamp.setSunValue(lamp.getSunvalue());
smartlamp.setLED_OnOff(lamp.getLedOnoff());
smartlamp.setAlarm_OnOff(lamp.getBuzzerOnoff());
smartlamp.setAir(lamp.getAir());
smartlamp.setDate(new Date());
System.out.println(smartlamp.toString());
smartlampList.add(smartlamp);
}
//调用easypoi来写成excel
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("智慧灯杆", "智慧灯杆"), Smartlamp.class, smartlampList);
//设置头准备回传给用户端
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + System.currentTimeMillis() + ".xls");
response.setCharacterEncoding("UTF-8");
try {
//返回用户端
workbook.write(response.getOutputStream());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

总结

这个地方有坑点,不知为什么我把DTO中的对象名称大写开头后就不能成功的运行,而会报错

1
2
3
2020-04-12 17:41:47.526 ERROR 3788 --- [nio-8889-exec-1] c.a.e.e.export.base.ExportCommonServer   : There is no getter for property named 'Tem' in 'null'

java.lang.RuntimeException: There is no getter for property named 'Tem' in 'null'

解决方法是把变量名开头变成小写即可,注意类型一定要是包装的而不能用原生的。

-------------你最愿意做的哪件事才是你的天赋所在-------------