项目中使用EasyPOI完成监控设备历史数据的导出
生活随笔
收集整理的這篇文章主要介紹了
项目中使用EasyPOI完成监控设备历史数据的导出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1)在頁面定義一個按鈕,當點擊按鈕時,把設備id,時間范圍傳遞到后臺
2)后天接收到請求及參數,進行處理
3)使用easypoi導出數據
/*** 默認查詢最近一個月的數據進行下載* 要按設備類型不同分別下載* 可以根據傳入的時間范圍進行下載* @param map* @param request* @param response* @param pageTableRequest*/ @RequestMapping("load") @ApiOperation(value = "歷史數據下載") public void downloadByPoiBaseView(ModelMap map, HttpServletRequest request,HttpServletResponse response,PageTableRequest pageTableRequest) {FarmDto farmDto=new FarmDto();User user = (User) SecurityUtils.getSubject().getPrincipal();user.setPassword(null);farmDto.setUser(user);//設備信息//基地id默認就是當前擁有的第一個基地,如有多個,在切換的時候動態更新數據//如果用戶沒有基地,那么farmId就為null,數據庫也不會返回數據Long farmId = null;//根據用戶獲取擁有的農場List<Farm> farmList = farmDao.getFarmListByUserId(user.getId());List<DeviceGather> deviceGatherList=null;String createTimeRangeStr ="";if (farmList != null && farmList.size() > 0) {Farm farm = farmList.get(0);//將用戶擁有的第一個農場查詢出來,這里是有問題的,需要優化farmId=farm.getId();List<Device> deviceList = deviceDao.getByFarmId(farmId);//封裝查詢條件createTimeRangeStr = (String) pageTableRequest.getParams().get("createTimeRange");if (StringUtils.isNotEmpty(createTimeRangeStr)) {//2019-01-11 - 2019-02-03String[] createTimeRangeStrS = createTimeRangeStr.split(" - ");pageTableRequest.getParams().put("startTime", createTimeRangeStrS[0]);pageTableRequest.getParams().put("endTime", createTimeRangeStrS[1]);}else {//如果沒有傳時間,就顯示最近一個月的String startTime=DateUtil.dateToString(DateUtil.getBeforeByMonth(1));String endTime=DateUtil.dateToString(new Date());pageTableRequest.getParams().put("startTime",startTime);pageTableRequest.getParams().put("endTime", endTime);createTimeRangeStr=startTime+" - "+endTime;}//將分頁參數設置為空pageTableRequest.setLimit(null);pageTableRequest.setOffset(null);deviceGatherList=deviceService.getHistoryDataByFarmId(pageTableRequest,farmId);}ExportParams params = new ExportParams("采集數據顯示", "概覽", ExcelType.XSSF);params.setFreezeCol(2);map.put(NormalExcelConstants.DATA_LIST, deviceGatherList);map.put(NormalExcelConstants.CLASS, DeviceGather.class);map.put(NormalExcelConstants.PARAMS, params);map.put(NormalExcelConstants.FILE_NAME, createTimeRangeStr);PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);}得到歷史數據的service
/*** 得到歷史采集數據* @param pageTableRequest* @param farmId* @return*/ @Override public List<DeviceGather> getHistoryDataByFarmId(PageTableRequest pageTableRequest,Long farmId) {List<DeviceGather> deviceGathers = new ArrayList<>();//如果設備id直接傳遞過來了,那就按設備id直接查詢Object deviceIdObj = pageTableRequest.getParams().get("deviceId");if (deviceIdObj != null) {Long deviceId= (Long) deviceIdObj;deviceGathers = deviceGatherDao.list(pageTableRequest.getParams(), null, null);return deviceGathers;}//沒有設備id的情況下,根據農場id獲取該農場的所有設備,并全部返回List<Device> deviceList = deviceDao.getByFarmId(farmId);for (Device device : deviceList) {//不需要分頁參數,并且設置設備idpageTableRequest.getParams().put("deviceId",device.getId());List<DeviceGather> deviceGatherList = deviceGatherDao.list(pageTableRequest.getParams(),null,null);if (deviceGatherList!=null&&deviceGatherList.size()>0){//封裝數據返回for (DeviceGather deviceGather : deviceGatherList) {deviceGathers.add(deviceGather);}}}return deviceGathers; }實體類
public class DeviceGather extends BaseEntity<Long> {@Excel(name = "設備id")private Integer deviceId;@Excel(name = "設備序列號",width = 20)private String deviceSn;@Excel(name = "設備名稱",width = 30)private String deviceName;private String deviceType;@Excel(name = "數據")private Float basicData;private Integer measurementUnitId;@Excel(name = "數據單位")private String measurementUnitName;private String measureUnitType;@Excel(name = "采集時間",format = "yyyy-MM-dd HH:mm:ss", width = 35)private Date gatherTime;生成excel的代碼
ExportParams params = new ExportParams("采集數據顯示", "概覽", ExcelType.XSSF); params.setFreezeCol(2); map.put(NormalExcelConstants.DATA_LIST, deviceGatherList); map.put(NormalExcelConstants.CLASS, DeviceGather.class); map.put(NormalExcelConstants.PARAMS, params); map.put(NormalExcelConstants.FILE_NAME, createTimeRangeStr); PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);?
總結
以上是生活随笔為你收集整理的项目中使用EasyPOI完成监控设备历史数据的导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java的Excel导出方案介绍
- 下一篇: 文件上传功能-本地存储、阿里OSS、七牛