提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.modular.demos.controller; |
懒 |
2 |
|
|
3 |
import cn.afterturn.easypoi.entity.vo.MapExcelConstants; |
|
4 |
import cn.afterturn.easypoi.excel.ExcelImportUtil; |
|
5 |
import cn.afterturn.easypoi.excel.entity.ExportParams; |
|
6 |
import cn.afterturn.easypoi.excel.entity.ImportParams; |
|
7 |
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; |
|
8 |
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; |
|
9 |
import cn.afterturn.easypoi.view.PoiBaseView; |
|
10 |
import cn.stylefeng.guns.base.consts.ConstantsContext; |
|
11 |
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo; |
|
12 |
import cn.stylefeng.guns.modular.demos.entity.ExcelItem; |
|
13 |
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum; |
|
14 |
import cn.stylefeng.guns.sys.modular.system.service.UserService; |
|
15 |
import cn.stylefeng.roses.kernel.model.exception.ServiceException; |
|
16 |
import cn.stylefeng.roses.kernel.model.response.ResponseData; |
|
17 |
import com.baomidou.mybatisplus.core.toolkit.IdWorker; |
|
18 |
import lombok.extern.slf4j.Slf4j; |
|
19 |
import org.springframework.beans.factory.annotation.Autowired; |
|
20 |
import org.springframework.stereotype.Controller; |
|
21 |
import org.springframework.ui.ModelMap; |
|
22 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
23 |
import org.springframework.web.bind.annotation.RequestPart; |
|
24 |
import org.springframework.web.bind.annotation.ResponseBody; |
|
25 |
import org.springframework.web.multipart.MultipartFile; |
|
26 |
|
|
27 |
import javax.servlet.http.HttpServletRequest; |
|
28 |
import javax.servlet.http.HttpServletResponse; |
|
29 |
import java.io.File; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
|
|
35 |
/** |
|
36 |
* excel导入导出示例 |
|
37 |
* |
|
38 |
* @author fengshuonan |
|
39 |
* @Date 2019/3/9 11:03 |
|
40 |
*/ |
|
41 |
@Controller |
|
42 |
@RequestMapping("/excel") |
|
43 |
@Slf4j |
|
44 |
public class ExcelController { |
|
45 |
|
|
46 |
@Autowired |
|
47 |
private UserService userService; |
|
48 |
|
|
49 |
/** |
|
50 |
* excel导入页面 |
|
51 |
* |
|
52 |
* @author fengshuonan |
|
53 |
* @Date 2019/3/9 11:03 |
|
54 |
*/ |
|
55 |
@RequestMapping("/import") |
|
56 |
public String importIndex() { |
|
57 |
return "/demos/excel_import.html"; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* 上传excel填报 |
|
62 |
*/ |
|
63 |
@RequestMapping("/uploadExcel") |
|
64 |
@ResponseBody |
|
65 |
public ResponseData uploadExcel(@RequestPart("file") MultipartFile file, HttpServletRequest request) { |
|
66 |
String name = file.getOriginalFilename(); |
|
67 |
request.getSession().setAttribute("upFile", name); |
|
68 |
String fileSavePath = ConstantsContext.getFileUploadPath(); |
|
69 |
try { |
|
70 |
file.transferTo(new File(fileSavePath + name)); |
|
71 |
} catch (Exception e) { |
|
72 |
//log.error("上传那文件出错!", e); |
|
73 |
throw new ServiceException(BizExceptionEnum.UPLOAD_ERROR); |
|
74 |
} |
|
75 |
|
|
76 |
HashMap<String, Object> map = new HashMap<>(); |
|
77 |
map.put("fileId", IdWorker.getIdStr()); |
|
78 |
return ResponseData.success(0, "上传成功", map); |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* 获取上传成功的数据 |
|
83 |
*/ |
|
84 |
@RequestMapping("/getUploadData") |
|
85 |
@ResponseBody |
|
86 |
public Object getUploadData(HttpServletRequest request) { |
|
87 |
String name = (String) request.getSession().getAttribute("upFile"); |
|
88 |
String fileSavePath = ConstantsContext.getFileUploadPath(); |
|
89 |
if (name != null) { |
|
90 |
File file = new File(fileSavePath + name); |
|
91 |
try { |
|
92 |
ImportParams params = new ImportParams(); |
|
93 |
params.setTitleRows(1); |
|
94 |
params.setHeadRows(1); |
|
95 |
List result = ExcelImportUtil.importExcel(file, ExcelItem.class, params); |
|
96 |
|
|
97 |
LayuiPageInfo returns = new LayuiPageInfo(); |
|
98 |
returns.setCount(result.size()); |
|
99 |
returns.setData(result); |
|
100 |
return returns; |
|
101 |
} catch (Exception e) { |
|
102 |
e.printStackTrace(); |
|
103 |
} |
|
104 |
} |
|
105 |
return null; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* excel导出 |
|
110 |
* |
|
111 |
* @author fengshuonan |
|
112 |
* @Date 2019/3/9 11:03 |
|
113 |
*/ |
|
114 |
@RequestMapping("/export") |
|
115 |
public void export(ModelMap modelMap, HttpServletRequest request, |
|
116 |
HttpServletResponse response) { |
|
117 |
|
|
118 |
//初始化表头 |
|
119 |
List<ExcelExportEntity> entity = new ArrayList<>(); |
|
120 |
entity.add(new ExcelExportEntity("用户id", "user_id")); |
|
121 |
entity.add(new ExcelExportEntity("头像", "avatar")); |
|
122 |
entity.add(new ExcelExportEntity("账号", "account")); |
|
123 |
entity.add(new ExcelExportEntity("姓名", "name")); |
|
124 |
entity.add(new ExcelExportEntity("生日", "birthday")); |
|
125 |
entity.add(new ExcelExportEntity("性别", "sex")); |
|
126 |
entity.add(new ExcelExportEntity("邮箱", "email")); |
|
127 |
entity.add(new ExcelExportEntity("电话", "phone")); |
|
128 |
entity.add(new ExcelExportEntity("角色id", "role_id")); |
|
129 |
entity.add(new ExcelExportEntity("部门id", "dept_id")); |
|
130 |
entity.add(new ExcelExportEntity("状态", "status")); |
|
131 |
entity.add(new ExcelExportEntity("创建时间", "create_time")); |
|
132 |
|
|
133 |
//初始化化数据 |
|
134 |
List<Map<String, Object>> maps = userService.listMaps(); |
|
135 |
ArrayList<Map<String, Object>> total = new ArrayList<>(); |
|
136 |
for (int i = 0; i < 100; i++) { |
|
137 |
total.addAll(maps); |
|
138 |
} |
|
139 |
|
|
140 |
ExportParams params = new ExportParams("Guns管理系统所有用户", "用户表", ExcelType.XSSF); |
|
141 |
modelMap.put(MapExcelConstants.MAP_LIST, total); |
|
142 |
modelMap.put(MapExcelConstants.ENTITY_LIST, entity); |
|
143 |
modelMap.put(MapExcelConstants.PARAMS, params); |
|
144 |
modelMap.put(MapExcelConstants.FILE_NAME, "Guns管理系统所有用户"); |
|
145 |
PoiBaseView.render(modelMap, request, response, MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW); |
|
146 |
} |
|
147 |
} |