#8.1 View 介绍
easypoi view 项目是为了更简单的方便搭建在导出时候的操作,利用spring mvc 的view 封装,更加符合spring mvc的风格
view下面包括多个 view的实现
- EasypoiBigExcelExportView 大数据量导出
- EasypoiMapExcelView map 列表导出
- EasypoiPDFTemplateView pdf导出
- EasypoiSingleExcelView 注解导出
- EasypoiTemplateExcelView 模板导出
- EasypoiTemplateWordView word模板导出
- MapGraphExcelView 图表导出
view的是使用方法大同小异,都有一个对应的bean,里面保护指定的参数常量
同意用modelmap.put(‘常量参数名’,‘值’)就可以,最后返回这个view名字
注解目录扫描的时候加上
cn.afterturn.easypoi.view
就可以使用了
##8.2 大数据导出View的用法
EasypoiBigExcelExportView **是针对大数据量导出特定的View,在跳转到这个View的时候不需要查询数据,而且这个View自己去查询数据,用户只要实现IExcelExportServer接口就可以了
对应的常量类BigExcelConstants**
public interface IExcelExportServer {
/**
* 查询数据接口
* @param obj 查询条件
* @param page 当前页数
* @return
*/
public List<Object> selectListForExcelExport(Object obj, int page);
}
EasypoiBigExcelExportView 判断是否还有下一页的条件是,如果selectListForExcelExport 返回null就认为是最后一页了,如果返回有数据这page+1继续查询
在我们自己的controller中
@RequestMapping("load")
public void downloadByPoiBaseView(ModelMap map, HttpServletRequest request,
HttpServletResponse response) {
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
params.setFreezeCol(2);
map.put(BigExcelConstants.CLASS, MsgClient.class);
map.put(BigExcelConstants.PARAMS, params);
//就是我们的查询参数,会带到接口中,供接口查询使用
map.put(BigExcelConstants.DATA_PARAMS, new HashMap<String,String>());
map.put(BigExcelConstants.DATA_INTER,excelExportServer);
PoiBaseView.render(map, request, response, BigExcelConstants.EASYPOI_BIG_EXCEL_VIEW);
}
我们需要把参数条件封装成map或者其他类型,上面的obj可以把参数自己转回来 参数名字 BigExcelConstants.DATA_PARAM
然后把实现查询的接口注入进来就可以了
map.put(BigExcelConstants.DATA_INTER,excelExportServer);
后面就和其他View一样了
##8.3 注解导出View用法
注解导出的View是这个EasypoiSingleExcelView,其实View大家可以忽略不看,主要用到的还是他对应的bean对象
NormalExcelConstants 注解到处还比较简单,大家只要把datalist,class和params 这几个参数put下就可以了。
具体的案例
@RequestMapping()
public String download(ModelMap map) {
List<MsgClient> list = new ArrayList<MsgClient>();
for (int i = 0; i < 100; i++) {
MsgClient client = new MsgClient();
client.setBirthday(new Date());
client.setClientName("小明" + i);
client.setClientPhone("18797" + i);
client.setCreateBy("JueYue");
client.setId("1" + i);
client.setRemark("测试" + i);
MsgClientGroup group = new MsgClientGroup();
group.setGroupName("测试" + i);
client.setGroup(group);
list.add(client);
}
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
params.setFreezeCol(2);
map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
map.put(NormalExcelConstants.CLASS, MsgClient.class);//导出实体
map.put(NormalExcelConstants.PARAMS, params);//参数
map.put(NormalExcelConstants.FILE_NAME, params);//文件名称
return NormalExcelConstants.EASYPOI_EXCEL_VIEW;//View名称
}
和非View导出基本一致,只是把调用方法封装了而已,其他参数还都是一样的,具体可以看下测试项目的
EasypoiSingleExcelViewTest
##8.4 注解变种Map类型的导出View
作为动态注解存在的 List
使用方法都是一样,直接看下例子吧
@RequestMapping()
public String download(ModelMap modelMap) {
List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
ExcelExportEntity excelentity = new ExcelExportEntity("姓名", "name");
excelentity.setNeedMerge(true);
entity.add(excelentity);
entity.add(new ExcelExportEntity("性别", "sex"));
excelentity = new ExcelExportEntity(null, "students");
List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
temp.add(new ExcelExportEntity("姓名", "name"));
temp.add(new ExcelExportEntity("性别", "sex"));
excelentity.setList(temp);
entity.add(excelentity);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
for (int i = 0; i < 10; i++) {
map = new HashMap<String, Object>();
map.put("name", "1" + i);
map.put("sex", "2" + i);
List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
tempList.add(map);
tempList.add(map);
map.put("students", tempList);
list.add(map);
}
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
params.setFreezeCol(2);
modelMap.put(MapExcelConstants.MAP_LIST, list); //数据集合
modelMap.put(MapExcelConstants.ENTITY_LIST, entity); //注解集合
modelMap.put(MapExcelConstants.PARAMS, params);//参数
modelMap.put(MapExcelConstants.FILE_NAME, "EasypoiMapExcelViewTest");//文件名称
return MapExcelConstants.EASYPOI_MAP_EXCEL_VIEW;//View名称
}
具体案例参考EasypoiMapExcelViewTest
##8.5Excel模板导出View
模板导出提供的EasypoiTemplateExcelView以及对应的bean TemplateExcelConstants
案例
@RequestMapping()
public String download(ModelMap modelMap) {
Map<String, Object> map = new HashMap<String, Object>();
TemplateExportParams params = new TemplateExportParams(
"doc/foreach.xlsx");
List<TemplateExcelExportEntity> list = new ArrayList<TemplateExcelExportEntity>();
for (int i = 0; i < 4; i++) {
TemplateExcelExportEntity entity = new TemplateExcelExportEntity();
entity.setIndex(i + 1 + "");
entity.setAccountType("开源项目");
entity.setProjectName("EasyPoi " + i + "期");
entity.setAmountApplied(i * 10000 + "");
entity.setApprovedAmount((i + 1) * 10000 - 100 + "");
list.add(entity);
}
map.put("entitylist", list);
map.put("manmark", "1");
map.put("letest", "12345678");
map.put("fntest", "12345678.2341234");
map.put("fdtest", null);
List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 1; i++) {
Map<String, Object> testMap = new HashMap<String, Object>();
testMap.put("id", "xman");
testMap.put("name", "小明" + i);
testMap.put("sex", "1");
mapList.add(testMap);
}
map.put("maplist", mapList);
mapList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 6; i++) {
Map<String, Object> testMap = new HashMap<String, Object>();
testMap.put("si", "xman");
mapList.add(testMap);
}
map.put("sitest", mapList);
modelMap.put(TemplateExcelConstants.FILE_NAME, "用户信息"); //文件名
modelMap.put(TemplateExcelConstants.PARAMS, params);//参数
modelMap.put(TemplateExcelConstants.MAP_DATA, map);//数据
return TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW;//view名称
}
具体案例EasypoiTemplateExcelViewTest
##8.6 PoiBaseView.render view的补救
假如因为不可抗拒或者其他神奇的原因,view导出无法使用,作者遇到过好几次了,各种神奇原因都有,提供一个统一的封装,算是一个补救措施吧
上面的modelMap写法和设置参数还是一样,最后直接输出就可以了
PoiBaseView.render(modelMap, request, response,View名称);
看个简单demo
@RequestMapping("load")
public void downloadByPoiBaseView(ModelMap map, HttpServletRequest request,
HttpServletResponse response) {
List<MsgClient> list = new ArrayList<MsgClient>();
for (int i = 0; i < 100; i++) {
MsgClient client = new MsgClient();
client.setBirthday(new Date());
client.setClientName("小明" + i);
client.setClientPhone("18797" + i);
client.setCreateBy("JueYue");
client.setId("1" + i);
client.setRemark("测试" + i);
MsgClientGroup group = new MsgClientGroup();
group.setGroupName("测试" + i);
client.setGroup(group);
list.add(client);
}
ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
params.setFreezeCol(2);
map.put(NormalExcelConstants.DATA_LIST, list);
map.put(NormalExcelConstants.CLASS, MsgClient.class);
map.put(NormalExcelConstants.PARAMS, params);
PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
}
更新时间:2022-09-04 22:15