使用service.selectMapsPage获取到map类型的分页数据,取出map类型的集合。由于集合中的任意一个元素都是excel中任意一行的完整数据,所以对mapList.get(0).entrySet()进行for循环得到key的集合,即对应的表中字段集合。
excel标题
在对应的xxControl中定义标题集合,以及标题和字段对应关系的集合。(因为list集合是有序的!!利用有序的特点可以将数据有序地放进excel中,keyValueList 集合的顺序和页面显示的顺序是一致的。)
@Override
public String[] keyArray() {
String[] headers = {"科目名称", "级别", "理论", "操作", "理、操", "人数总计", "费用总计"};
return headers;
}
@Override
public List keyValueRelation() {
List keyValueList = new ArrayList<>();
keyValueList.add(new KeyValue("subject", "科目名称"));
keyValueList.add(new KeyValue("levelname", "级别"));
keyValueList.add(new KeyValue("theryNum", "理论"));
keyValueList.add(new KeyValue("operateNum", "操作"));
keyValueList.add(new KeyValue("theryOperateNum", "理、操"));
keyValueList.add(new KeyValue("allNum", "人数总计"));
keyValueList.add(new KeyValue("allMoney", "费用总计"));
return keyValueList;
}
headers集合和keyValueList集合长度是一样的,所以利用headers进行for循环或者keyValueList进行for循环都是一样的,都能够将keyValueList集合中的元素依次取出来,并拿到字段名(比如subject),再通过字段名将mapList中的值取出来存进单元格中。
for (short j = 0; j < headers.length; j++) {
// for (short j = 0; j < keyValueList.size(); j++) {
//创建单元格
Cell cell = row1.createCell(j);
String ziduan = keyValueList.get(j).getZiduan();
sheet.setColumnWidth(i, 256 * 15);//设置某列的列宽,列号为“i”
Object o = mapList.get(i).get(ziduan);
System.out.println(o);
cell.setCellValue(mapList.get(i).get(ziduan) == null ? " " : mapList.get(i).get(ziduan).toString());
}
对应xxControl代码
public class BaseRestController, M extends BaseMapper, T, I extends Serializable> {
/**
* 导出接口,暂时未用
*/
@RequestMapping(value = "export", method = RequestMethod.GET)
public ResponseObj export(@ModelAttribute Page page,
@ModelAttribute T model,
HttpServletResponse response,
HttpServletRequest request) throws Exception {
String name = model.getClass().getSimpleName();
Page
接口调用:http://127.0.0.1:10002/api/feeAnalysis/export?current=1&size=20