|  |  |  | 
|---|
|  |  |  | import cn.hutool.poi.word.WordUtil; | 
|---|
|  |  |  | import com.deepoove.poi.XWPFTemplate; | 
|---|
|  |  |  | import com.deepoove.poi.config.Configure; | 
|---|
|  |  |  | import com.deepoove.poi.data.PictureRenderData; | 
|---|
|  |  |  | import com.deepoove.poi.data.RowRenderData; | 
|---|
|  |  |  | import com.deepoove.poi.data.TextRenderData; | 
|---|
|  |  |  | import com.deepoove.poi.policy.HackLoopTableRenderPolicy; | 
|---|
|  |  |  | import com.moral.api.entity.Dustld; | 
|---|
|  |  |  | import com.moral.api.exception.BusinessException; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.moral.api.service.SpecialDeviceService; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import com.moral.constant.ResultMessage; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  | import com.moral.util.WebUtils; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | import io.swagger.annotations.ApiImplicitParam; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.apache.commons.io.FileUtils; | 
|---|
|  |  |  | import org.apache.poi.xwpf.usermodel.XWPFDocument; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.boot.system.ApplicationHome; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.util.ResourceUtils; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  | import org.springframework.web.multipart.MultipartFile; | 
|---|
|  |  |  | import org.springframework.web.multipart.MultipartHttpServletRequest; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.io.*; | 
|---|
|  |  |  | import java.net.URL; | 
|---|
|  |  |  | import java.net.URLEncoder; | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | 
|---|
|  |  |  | return ObjectUtils.isEmpty(daily) ? ResultMessage.ok() : ResultMessage.ok(daily); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 下载走航车日报 | 
|---|
|  |  |  | * @param id | 
|---|
|  |  |  | * @param params | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("loadDaily") | 
|---|
|  |  |  | public ResultMessage loadDaily(Integer id){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | DailyVo dailyVo = specialDeviceService.loadDaily(id); | 
|---|
|  |  |  | if (dailyVo==null){ | 
|---|
|  |  |  | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(), ResponseCodeEnum.SUCCESS.getMsg(),dailyVo); | 
|---|
|  |  |  | @PostMapping("loadDaily") | 
|---|
|  |  |  | public void loadDaily( @RequestBody Map<String,Object> params,HttpServletResponse response) throws IOException { | 
|---|
|  |  |  | List<Integer> ids = (List<Integer>) params.get("ids"); | 
|---|
|  |  |  | List<DailyVo> dailyVos = specialDeviceService.loadDaily(ids); | 
|---|
|  |  |  | dailyDocx(dailyVos,response); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public  void dailyDocx( List<DailyVo> dailyVos,HttpServletResponse response) throws IOException { | 
|---|
|  |  |  | //        String path = getPath(fileName); | 
|---|
|  |  |  | //获取jar包所在目录 | 
|---|
|  |  |  | ApplicationHome applicationHome = new ApplicationHome(getClass()); | 
|---|
|  |  |  | //在jar包所在目录下生成一个upload文件夹用来存储上传的图片 | 
|---|
|  |  |  | String path = applicationHome.getSource().getParentFile().toString() + "/static/img"; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //InputStream  inputStream = WordUtil.class.getClassLoader().getResourceAsStream("word/尘负荷监测报告.docx"); | 
|---|
|  |  |  | InputStream stream = WordUtil.class.getClassLoader().getResourceAsStream("word/盐城市走航监测报告.docx"); | 
|---|
|  |  |  | //获取临时文件 | 
|---|
|  |  |  | File file = new File("word/盐城市走航监测报告.docx"); | 
|---|
|  |  |  | //将读取到的类容存储到临时文件中,后面就可以用这个临时文件访问了 | 
|---|
|  |  |  | FileUtils.copyInputStreamToFile(stream, file); | 
|---|
|  |  |  | //这个时候再去获取证书的文件路径 就可以正常获取了 | 
|---|
|  |  |  | String filePath = file.getAbsolutePath(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //第二步:创建数据,用于把我们模板中的{{}}包裹的变量替换掉,map中保存的key一定要和模板中的变量保持一致 | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | //设置单元格,表头 | 
|---|
|  |  |  | //RowRenderData header1 = RowRenderData.build(new TextRenderData("6495ED", "序号"), new TextRenderData("6495ED", "道路"),new TextRenderData("6495ED", "平均尘负荷(克/平方米)")); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //设置表内容,如果在实际应用中,传入一个list集合或者数组,方可用foreach进行循环填充 | 
|---|
|  |  |  | List<Map> detailList1 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList2 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList3 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList4 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList5 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList6 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList7 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> detailList8 = new ArrayList<>(); | 
|---|
|  |  |  | StringBuilder builder = new StringBuilder(); | 
|---|
|  |  |  | List<Map> pic1 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic2 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic3 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic4 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic5 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic6 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic7 = new ArrayList<>(); | 
|---|
|  |  |  | List<Map> pic8 = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //            picMap.put("urlImg", Pictures.ofUrl(str, PictureType.JPEG).size(width, height).create()); | 
|---|
|  |  |  | //            picMap.put("urlImg",new PictureRenderData(500, 500, "G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg")); | 
|---|
|  |  |  | //            picMap.put("urlImg1",new PictureRenderData(500, 500, "G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg")); | 
|---|
|  |  |  | //            picMap.put("urlImg", Pictures.ofUrl("G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg", PictureType.JPEG).size(500, 500).create()); | 
|---|
|  |  |  | //            picMap.put("urlImg1", Pictures.ofUrl("G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg", PictureType.JPEG).size(500, 500).create()); | 
|---|
|  |  |  | int i =1; | 
|---|
|  |  |  | for (DailyVo dailyVo : dailyVos) { | 
|---|
|  |  |  | Map picMap = new HashMap(); | 
|---|
|  |  |  | List<String> images = dailyVo.getImages(); | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(images)){ | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(0))); | 
|---|
|  |  |  | pic1.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(1))); | 
|---|
|  |  |  | pic2.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(2))); | 
|---|
|  |  |  | pic3.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(3))); | 
|---|
|  |  |  | pic4.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(4))); | 
|---|
|  |  |  | pic5.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(5))); | 
|---|
|  |  |  | pic6.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(6))); | 
|---|
|  |  |  | pic7.add(picMap); | 
|---|
|  |  |  | picMap.put("urlImg"+i,new PictureRenderData(500, 500, path+"\\"+images.get(7))); | 
|---|
|  |  |  | pic8.add(picMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | detailList1.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList2.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList3.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList4.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList5.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList6.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList7.add(dailyVo.getCode()); | 
|---|
|  |  |  | detailList8.add(dailyVo.getCode()); | 
|---|
|  |  |  | builder.append("("+dailyVo.getCode().get("mac")+")白天对市重点区域(宝龙广场、盐城电厂为主)进行走航监测:常规7参数走航过程中PM2.5均值浓度为"+dailyVo.getCode().get("avgPm25")+"ug/m3;PM10均值浓度为"+dailyVo.getCode().get("avgPm10")+"ug/m3;NO2均值浓度为" | 
|---|
|  |  |  | +dailyVo.getCode().get("avgNO2")+"ug/m3;CO均值浓度为"+dailyVo.getCode().get("avgCO")+"mg/m3;SO2均值浓度为"+dailyVo.getCode().get("avgSO2")+"ug/m3;O3均值浓度为"+dailyVo.getCode().get("avgO3")+"ug/m3,VOCs均值浓度"+dailyVo.getCode().get("avgVOC")+"mg/m3。"+"\n"+"    "); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /*            for (HashMap<String, Object> list : lists) { | 
|---|
|  |  |  | detailList1.add(list); | 
|---|
|  |  |  | detailList2.add(list); | 
|---|
|  |  |  | detailList3.add(list); | 
|---|
|  |  |  | detailList4.add(list); | 
|---|
|  |  |  | detailList5.add(list); | 
|---|
|  |  |  | detailList6.add(list); | 
|---|
|  |  |  | detailList7.add(list); | 
|---|
|  |  |  | detailList8.add(list); | 
|---|
|  |  |  | builder.append("("+list.get("mac")+")白天对市重点区域(宝龙广场、盐城电厂为主)进行走航监测:常规7参数走航过程中PM2.5均值浓度为"+list.get("avgPm25")+"ug/m3;PM10均值浓度为"+list.get("avgPm10")+"ug/m3;NO2均值浓度为" | 
|---|
|  |  |  | +list.get("avgNO2")+"ug/m3;CO均值浓度为"+list.get("avgCO")+"mg/m3;SO2均值浓度为"+list.get("avgSO2")+"ug/m3;O3均值浓度为"+list.get("avgO3")+"ug/m3,VOCs均值浓度"+list.get("avgVOC")+"mg/m3。"+"\n"+"    "); | 
|---|
|  |  |  | }*/ | 
|---|
|  |  |  | /*            List<Map> pic = new ArrayList<>(); | 
|---|
|  |  |  | for (MultipartFile multipartFile : files) { | 
|---|
|  |  |  | Map picMap = new HashMap(); | 
|---|
|  |  |  | InputStream inputStream = multipartFile.getInputStream(); | 
|---|
|  |  |  | picMap.put("urlImg", Pictures.ofStream(inputStream, PictureType.JPEG).size(500, 500).create()); | 
|---|
|  |  |  | pic.add(picMap); | 
|---|
|  |  |  | }*/ | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | map.put("code",builder); | 
|---|
|  |  |  | map.put("time1",DateUtils.getCurCNDate()); | 
|---|
|  |  |  | //            map.put("pho",new PictureRenderData(100, 100, "G:\\home\\3d423e3cb05d7edc35c38e3173af2a0d.jpg")); | 
|---|
|  |  |  | //            Pictures.ofLocal(null); | 
|---|
|  |  |  | //            map.put("pho1",new PictureRenderData(100, 100, path+"\\0a4ab467-304d-4871-948f-55aa7e820975.png")); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy(); | 
|---|
|  |  |  | Configure config = Configure.newBuilder().bind("detailList1", policy).bind("detailList2", policy).bind("detailList3", policy).bind("detailList4", policy) | 
|---|
|  |  |  | .bind("detailList5", policy).bind("detailList6", policy).bind("detailList7", policy).bind("detailList8", policy).build(); | 
|---|
|  |  |  | XWPFTemplate compile = XWPFTemplate.compile(filePath,config).render(new HashMap<String, Object>(){{ | 
|---|
|  |  |  | put("code",map.get("code")); | 
|---|
|  |  |  | put("time1",map.get("time1")); | 
|---|
|  |  |  | //                put("pho", map.get("pho")); | 
|---|
|  |  |  | //                put("pho1", map.get("pho1")); | 
|---|
|  |  |  | put("detailList1", detailList1); | 
|---|
|  |  |  | put("detailList2", detailList2); | 
|---|
|  |  |  | put("detailList3", detailList3); | 
|---|
|  |  |  | put("detailList4", detailList4); | 
|---|
|  |  |  | put("detailList5", detailList5); | 
|---|
|  |  |  | put("detailList6", detailList6); | 
|---|
|  |  |  | put("detailList7", detailList7); | 
|---|
|  |  |  | put("detailList8", detailList8); | 
|---|
|  |  |  | put("picList1", pic1); | 
|---|
|  |  |  | put("picList2", pic2); | 
|---|
|  |  |  | put("picList3", pic3); | 
|---|
|  |  |  | put("picList4", pic4); | 
|---|
|  |  |  | put("picList5", pic5); | 
|---|
|  |  |  | put("picList6", pic6); | 
|---|
|  |  |  | put("picList7", pic7); | 
|---|
|  |  |  | put("picList8", pic8); | 
|---|
|  |  |  | }}); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | response.setContentType("multipart/form-data"); | 
|---|
|  |  |  | // 设置文件名 | 
|---|
|  |  |  | response.addHeader("Content-Disposition", "attachment;fileName=ce.docx" ); | 
|---|
|  |  |  | response.setCharacterEncoding("UTF-8"); | 
|---|
|  |  |  | //保存到本地 | 
|---|
|  |  |  | //            File file1 = new File("G:\\test\\test2.docx"); | 
|---|
|  |  |  | //            FileOutputStream out = new FileOutputStream(file1); | 
|---|
|  |  |  | //            compile.write(out); | 
|---|
|  |  |  | //下载到线上 | 
|---|
|  |  |  | OutputStream out = response.getOutputStream(); | 
|---|
|  |  |  | compile.write(out); | 
|---|
|  |  |  | out.flush(); | 
|---|
|  |  |  | out.close(); | 
|---|
|  |  |  | compile.close(); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | log.error ("导出失败,请联系网站管理员!", e); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | throw new RuntimeException(e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|