提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.excel.controller; |
懒 |
2 |
|
|
3 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
4 |
import cn.stylefeng.guns.excel.entity.ExcelExportDeploy; |
|
5 |
import cn.stylefeng.guns.excel.service.ExcelExportDeployService; |
|
6 |
import cn.stylefeng.guns.excel.view.JxlsExcelView; |
|
7 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
8 |
import cn.stylefeng.roses.groovy.util.GroovyUtil; |
|
9 |
import cn.stylefeng.roses.kernel.model.response.ErrorResponseData; |
|
10 |
import cn.stylefeng.roses.kernel.model.response.ResponseData; |
|
11 |
import cn.stylefeng.roses.kernel.model.response.SuccessResponseData; |
|
12 |
import com.alibaba.fastjson.support.spring.FastJsonJsonView; |
|
13 |
import org.springframework.stereotype.Controller; |
|
14 |
import org.springframework.web.bind.annotation.PathVariable; |
|
15 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
16 |
import org.springframework.web.bind.annotation.ResponseBody; |
|
17 |
import org.springframework.web.servlet.ModelAndView; |
|
18 |
|
|
19 |
import javax.annotation.Resource; |
|
20 |
import javax.servlet.http.HttpServletRequest; |
|
21 |
import java.util.Map; |
|
22 |
|
|
23 |
/** |
|
24 |
* Excel的导出配置 |
|
25 |
* |
|
26 |
* @author fengshuonan |
|
27 |
* @Date 2019/12/31 22:09 |
|
28 |
*/ |
|
29 |
@Controller |
|
30 |
@RequestMapping("/excelExport") |
|
31 |
public class ExcelExportController { |
|
32 |
|
|
33 |
@Resource |
|
34 |
private ExcelExportDeployService excelExportDeployService; |
|
35 |
|
|
36 |
/** |
|
37 |
* 查看数据格式 |
|
38 |
* |
|
39 |
* @param nid 导出模板的唯一标识 |
|
40 |
* @author fengshuonan |
|
41 |
* @Date 2019/12/31 22:12 |
|
42 |
*/ |
|
43 |
@ResponseBody |
|
44 |
@RequestMapping(value = "/show/{nid}") |
|
45 |
public ResponseData showExport(@PathVariable("nid") String nid, HttpServletRequest request) { |
|
46 |
|
|
47 |
ExcelExportDeploy excelExportDeployParam = new ExcelExportDeploy(); |
|
48 |
excelExportDeployParam.setNid(nid); |
|
49 |
ExcelExportDeploy excelExportDeploy = excelExportDeployService.findBySpec(excelExportDeployParam); |
|
50 |
|
|
51 |
//执行groovy脚本中的类的方法 |
|
52 |
if (excelExportDeploy != null) { |
|
53 |
String groovyText = excelExportDeploy.getDataSource(); |
|
54 |
Object result = GroovyUtil.executeClassMethod(groovyText, "run", new Class[]{HttpServletRequest.class}, new Object[]{request}); |
|
55 |
return new SuccessResponseData(result); |
|
56 |
} else { |
|
57 |
return new ErrorResponseData("模版未定义"); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* 导出链接 |
|
63 |
* |
|
64 |
* @author fengshuonan |
|
65 |
* @Date 2019/12/31 22:12 |
|
66 |
*/ |
|
67 |
@RequestMapping(value = "/export/{nid}") |
|
68 |
public ModelAndView export(@PathVariable("nid") String nid, HttpServletRequest request) { |
|
69 |
|
|
70 |
ExcelExportDeploy excelExportDeployParam = new ExcelExportDeploy(); |
|
71 |
excelExportDeployParam.setNid(nid); |
|
72 |
ExcelExportDeploy excelExportDeploy = excelExportDeployService.findBySpec(excelExportDeployParam); |
|
73 |
|
|
74 |
//执行groovy脚本中的类的方法 |
|
75 |
if (excelExportDeploy != null) { |
|
76 |
String groovyText = excelExportDeploy.getDataSource(); |
|
77 |
Object result = GroovyUtil.executeClassMethod(groovyText, "run", new Class[]{HttpServletRequest.class}, new Object[]{request}); |
|
78 |
if (result != null) { |
|
79 |
Map<String, Object> resultMap = ToolUtil.toMap(result); |
|
80 |
|
|
81 |
//文件输出目录 |
|
82 |
String path = ConstantsContext.getFileUploadPath() + excelExportDeploy.getTemplate(); |
|
83 |
|
|
84 |
//构造响应文件的标题 |
|
85 |
String title = ToolUtil.stringReplaceBuild(excelExportDeploy.getTitle(), resultMap); |
|
86 |
|
|
87 |
//输出文件格式解析 |
|
88 |
String type = excelExportDeploy.getTemplate().contains(".xlsx") ? ".xlsx" : ".xls"; |
|
89 |
|
|
90 |
return new ModelAndView(new JxlsExcelView(path, title, type), resultMap); |
|
91 |
} else { |
|
92 |
ModelAndView mav = new ModelAndView(new FastJsonJsonView()); |
|
93 |
mav.addObject(new ErrorResponseData("脚本执行结果为空")); |
|
94 |
return mav; |
|
95 |
} |
|
96 |
} else { |
|
97 |
ModelAndView mav = new ModelAndView(new FastJsonJsonView()); |
|
98 |
mav.addObject(new ErrorResponseData("模板未定义")); |
|
99 |
return mav; |
|
100 |
} |
|
101 |
} |
|
102 |
} |