| | |
| | | package com.moral.api.controller; |
| | | |
| | | import cn.afterturn.easypoi.word.WordExportUtil; |
| | | import cn.hutool.poi.word.WordUtil; |
| | | import com.deepoove.poi.NiceXWPFDocument; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.data.MiniTableRenderData; |
| | | import com.deepoove.poi.data.RowRenderData; |
| | | import com.deepoove.poi.data.TextRenderData; |
| | | import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO; |
| | | |
| | | 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.xwpf.usermodel.XWPFDocument; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.util.ResourceUtils; |
| | | import org.springframework.web.bind.annotation.CrossOrigin; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.springframework.web.multipart.MultipartHttpServletRequest; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.net.URLEncoder; |
| | | import java.text.ParseException; |
| | | import java.util.ArrayList; |
| | | |
| | | import java.util.HashMap; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | import com.moral.api.entity.Dustld; |
| | | import com.moral.api.pojo.bo.ExcelBO; |
| | | import com.moral.api.pojo.dto.historySecondCruiser.HistorySecondCruiserResultDTO; |
| | | import com.moral.api.pojo.dust.DustForm; |
| | | import com.moral.api.pojo.vo.excel.DailyVo; |
| | | import com.moral.api.service.DustldService; |
| | |
| | | import com.moral.constant.ResponseCodeEnum; |
| | | import com.moral.constant.ResultMessage; |
| | | import com.moral.util.WebUtils; |
| | | 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.commons.io.FileUtils; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.apache.xmlbeans.impl.jam.provider.ResourcePath; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static com.moral.api.controller.SysTestController.getPath; |
| | | |
| | |
| | | if (!params.containsKey("mac") || !params.containsKey("startTime") || !params.containsKey("endTime")){ |
| | | return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg()); |
| | | } |
| | | |
| | | System.out.println(CruiserController.class.getResource("/word/尘负荷监测报告.docx").getPath()); |
| | | URL resource = getClass().getResource("/word/尘负荷监测报告.docx"); |
| | | String path = resource.getPath(); |
| | | System.out.println(path); |
| | | Map<String, Object> map = dustldService.dailyDustlds(params); |
| | | if (ObjectUtils.isEmpty(map)){ |
| | | return ResultMessage.fail(ResponseCodeEnum.TARGET_IS_NULL.getCode(), ResponseCodeEnum.TARGET_IS_NULL.getMsg()); |
| | | } |
| | | test(map,response); |
| | | // return new ResultMessage(ResponseCodeEnum.SUCCESS.getCode(),ResponseCodeEnum.SUCCESS.getMsg(),map); |
| | | |
| | | return ResultMessage.ok(); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | |
| | | public void test( Map<String, Object> params,HttpServletResponse response) throws IOException { |
| | | // String path="D:\\尘负荷监测报告.docx"; |
| | | String path = getPath("尘负荷监测报告.docx"); |
| | | public static void test( Map<String, Object> params,HttpServletResponse response) throws IOException { |
| | | InputStream inputStream = WordUtil.class.getClassLoader().getResourceAsStream("word/尘负荷监测报告.docx"); |
| | | XWPFDocument compile = null; |
| | | 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<>(); |
| | | //设置单元格,表头 |
| | |
| | | map.put("time",params.get("time").toString()); |
| | | map.put("date1",params.get("date1").toString()); |
| | | map.put("date2",params.get("date2").toString()); |
| | | // map.put("data",params.get("data").toString()); |
| | | compile.render(map); |
| | | // response.addHeader("Access-Contro1-Allow-Origin", "*"); |
| | | |
| | | response.setHeader("Access-Control-Allow-Origin","*");//允许所有请求跨域 |
| | | response.setHeader("Access-Control-Allow-Methods", "*");//允许跨域的请求方法GET, POST, HEAD 等 |
| | | response.setHeader("Access-Control-Allow-Headers", "*");//允许跨域的请求头 |
| | | response.setHeader("Access-Control-Allow-Credentials", "true");//是否携带cooki |
| | | |
| | | File docxFile = new File("尘负荷监测报告.docx"); |
| | | FileUtils.copyToFile(inputStream,docxFile); |
| | | compile = WordExportUtil.exportWord07(docxFile.getPath(),map); |
| | | compile.write(response.getOutputStream()); |
| | | compile.close(); |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | log.error ("导出失败,请联系网站管理员!", e); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | }finally { |
| | | compile.close(); |
| | | inputStream.close(); |
| | | |
| | | } |
| | | |
| | | } |