导出exceldemo
SmartlampDTO
1 |
|
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
74package 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;
public class searchdataController {
WisdomLampMapper wisdomLampMapper;
"/searchDate") (
public String callback(Model model, HttpServletRequest request){
request.getSession().setAttribute("section","searchData");
return "searchData";
}
"/ExportExcel",method = {RequestMethod.POST,RequestMethod.GET}) (value =
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
32020-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'
解决方法是把变量名开头变成小写即可,注意类型一定要是包装的而不能用原生的。