|  |  |  | 
|---|
|  |  |  | import com.deepoove.poi.data.TextRenderData; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cn.afterturn.easypoi.exception.word.WordExportException; | 
|---|
|  |  |  | import cn.afterturn.easypoi.word.WordExportUtil; | 
|---|
|  |  |  | import io.swagger.annotations.Api; | 
|---|
|  |  |  | import io.swagger.annotations.ApiImplicitParam; | 
|---|
|  |  |  | import io.swagger.annotations.ApiImplicitParams; | 
|---|
|  |  |  | import io.swagger.annotations.ApiOperation; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.poi.ss.formula.functions.Rows; | 
|---|
|  |  |  | import org.apache.poi.xwpf.usermodel.XWPFDocument; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.net.URLEncoder; | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.ServletOutputStream; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | public void test( Map<String, Object> params,HttpServletResponse response) throws IOException { | 
|---|
|  |  |  | //        String path="D:\\尘负荷监测报告.docx"; | 
|---|
|  |  |  | String path = getPath("尘负荷监测报告.docx"); | 
|---|
|  |  |  | //获取当前模板的路径 | 
|---|
|  |  |  | File rootFile = new File(ResourceUtils.getURL("classpath:").getPath()); | 
|---|
|  |  |  | File file= new File(rootFile, path); | 
|---|
|  |  |  | //第一步:读取模板 | 
|---|
|  |  |  | XWPFTemplate compile = XWPFTemplate.compile(file.getPath()); | 
|---|
|  |  |  | //第二步:创建数据,用于把我们模板中的{{}}包裹的变量替换掉,map中保存的key一定要和模板中的变量保持一致 | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | //设置单元格,表头 | 
|---|
|  |  |  | RowRenderData header1 = RowRenderData.build(new TextRenderData("6495ED", "序号"), new TextRenderData("6495ED", "道路"),new TextRenderData("6495ED", "平均尘负荷(克/平方米)")); | 
|---|
|  |  |  | RowRenderData header2 = RowRenderData.build(new TextRenderData("6495ED", "序号"), new TextRenderData("6495ED", "道路"),new TextRenderData("6495ED", "平均尘负荷(克/平方米)")); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //获取当前模板的路径 | 
|---|
|  |  |  | File rootFile = new File(ResourceUtils.getURL("classpath:").getPath()); | 
|---|
|  |  |  | File file= new File(rootFile, path); | 
|---|
|  |  |  | //第一步:读取模板 | 
|---|
|  |  |  | XWPFTemplate compile = XWPFTemplate.compile(file.getPath()); | 
|---|
|  |  |  | //第二步:创建数据,用于把我们模板中的{{}}包裹的变量替换掉,map中保存的key一定要和模板中的变量保持一致 | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | //设置单元格,表头 | 
|---|
|  |  |  | RowRenderData header1 = RowRenderData.build(new TextRenderData("6495ED", "序号"), new TextRenderData("6495ED", "道路"),new TextRenderData("6495ED", "平均尘负荷(克/平方米)")); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //设置表内容,如果在实际应用中,传入一个list集合或者数组,方可用foreach进行循环填充 | 
|---|
|  |  |  | List<DustForm> list1 = (List<DustForm>) params.get("list1"); | 
|---|
|  |  |  | List<DustForm> list2 = (List<DustForm>) params.get("list2"); | 
|---|
|  |  |  | ArrayList<RowRenderData> rsList1 = new ArrayList<>(); | 
|---|
|  |  |  | ArrayList<RowRenderData> rsList2 = new ArrayList<>(); | 
|---|
|  |  |  | //设置表内容,如果在实际应用中,传入一个list集合或者数组,方可用foreach进行循环填充 | 
|---|
|  |  |  | List<DustForm> list1 = (List<DustForm>) params.get("list1"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (int i = 0; i < list1.size(); i++) { | 
|---|
|  |  |  | DustForm dustForm = list1.get(i); | 
|---|
|  |  |  | RowRenderData row0 = RowRenderData.build(i+1+"",dustForm.getRoad(), dustForm.getValue()+""); | 
|---|
|  |  |  | rsList1.add(row0); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (int i = 0; i < list2.size(); i++) { | 
|---|
|  |  |  | DustForm dustForm = list2.get(i); | 
|---|
|  |  |  | RowRenderData row0 = RowRenderData.build(i+1+"",dustForm.getRoad(), dustForm.getValue()+""); | 
|---|
|  |  |  | rsList2.add(row0); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ArrayList<Map<String, Object>> mapArrayList = (ArrayList<Map<String, Object>>) params.get("list3"); | 
|---|
|  |  |  | ArrayList<RowRenderData> rsList1 = new ArrayList<>(); | 
|---|
|  |  |  | ArrayList<RowRenderData> rsList3 = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | map.put("table1", new MiniTableRenderData(header1, rsList1)); | 
|---|
|  |  |  | map.put("table2", new MiniTableRenderData(header2, rsList2)); | 
|---|
|  |  |  | map.put("data",params.get("data").toString()); | 
|---|
|  |  |  | map.put("time",params.get("time").toString()); | 
|---|
|  |  |  | for (int i = 0; i < list1.size(); i++) { | 
|---|
|  |  |  | DustForm dustForm = list1.get(i); | 
|---|
|  |  |  | RowRenderData row0 = RowRenderData.build(i+1+"",dustForm.getRoad(), dustForm.getValue()+""); | 
|---|
|  |  |  | rsList1.add(row0); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (int i = 0; i < mapArrayList.size(); i++) { | 
|---|
|  |  |  | Map<String, Object> map1 = mapArrayList.get(i); | 
|---|
|  |  |  | RowRenderData row = RowRenderData.build(i + 1 + "", map1.get("road").toString(), map1.get("value1").toString(), map1.get("value2").toString(), map1.get("value3").toString()); | 
|---|
|  |  |  | rsList3.add(row); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | map.put("table1", new MiniTableRenderData(header1, rsList1)); | 
|---|
|  |  |  | map.put("table3", new MiniTableRenderData(rsList3)); | 
|---|
|  |  |  | map.put("data",params.get("data").toString()); | 
|---|
|  |  |  | map.put("time",params.get("time").toString()); | 
|---|
|  |  |  | map.put("date1",params.get("date1").toString()); | 
|---|
|  |  |  | map.put("date2",params.get("date2").toString()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | compile.render(map); | 
|---|
|  |  |  | //        response.setCharacterEncoding("utf-8"); | 
|---|
|  |  |  | //        response.setContentType("multipart/form-data"); | 
|---|
|  |  |  | //        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("尘负荷监测报告" + params.get("time").toString() + ".docx", "utf-8")); | 
|---|
|  |  |  | //        FileOutputStream fileOutputStream = new FileOutputStream("尘负荷监测报告" + params.get("time").toString() + ".docx"); | 
|---|
|  |  |  | //        compile.write(fileOutputStream); | 
|---|
|  |  |  | //        compile.writeToFile("G:\\hme\\mon1\\" + "尘负荷监测报告"+params.get("time").toString()+".docx"); | 
|---|
|  |  |  | compile.render(map); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //            compile.writeToFile("G:\\home\\mon1\\" + "尘负荷监测报告"+params.get("time").toString()+".docx"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        FileOutputStream out = new FileOutputStream("尘负荷监测报告" + params.get("time").toString() + ".docx"); | 
|---|
|  |  |  | compile.write(response.getOutputStream()); | 
|---|
|  |  |  | compile.close(); | 
|---|
|  |  |  | //        return compile; | 
|---|
|  |  |  | compile.write(response.getOutputStream()); | 
|---|
|  |  |  | compile.close(); | 
|---|
|  |  |  | } catch (IOException e) { | 
|---|
|  |  |  | log.error ("导出失败,请联系网站管理员!", e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|