关于导出Excel表中存在部门或用户数据权限问题

/**
* 导出Controller
*/
@RequiresPermissions("xxx:weeklightlimit:download")
@RequestMapping("/DownLoad")

//注意:关于权限问题必须将@RequestParam Map<String, Object> params放在HttpServletResponse response前,否则系统会报错:("msg":"数据权限接口,只能是Map类型参数,且不能为NULL","code":500})
public R DownLoad(@RequestParam Map<String, Object> params,HttpServletResponse response){
LimitService.downLoadList(params,response);
return R.ok();
}

//接口实现类

@DataFilter(subDept=true,user=false,tableAlias="a",deptId="dept_id",userId="user_id")
@Override
public void downLoadList(Map<String, Object> params,HttpServletResponse response) {
try {
String title="参数信息.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook();
response.setCharacterEncoding("utf-8");
//以下设置格式
response.setHeader("conent-type", "application/octet-stream");
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
response.setHeader("charset", "utf-8");
response.setHeader("content-disposition","attachment;filename="+new String(title.getBytes("gbk"), "ISO8859-1"));
/*--------------------------------------------------------------------------------*/
OutputStream os = response.getOutputStream();
XSSFSheet sheet=workbook.createSheet("text");
//创建第一行标题行

XSSFRow row=sheet.createRow(0);//创建标题行
XSSFCell num=row.createCell(0);
num.setCellValue("参数");
XSSFCell  num001=row.createCell(1);
num001.setCellValue("参数1");
XSSFCell  num002=row.createCell(2);
num002.setCellValue("参数2");
XSSFCell  num003=row.createCell(3);
num003.setCellValue("参数三");

SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*----------------------------------------------------------------------------------*/
//开始获取需要导入到excel中的数据
//根据x名查询DetailEntity表中所有信息
Page<XxxEntity> pagination =(Page<XxxEntity>)new Query<XxxEntity>().getPage(params);
//根据x名查询DetailEntity表中所有信息
List<XxxEntity> list = (List<XxxEntity>) baseMapper.getWeekLightLimit(pagination,params);
int dataIndex=1;//取行索引
for(XxxEntityweek xxxEntity:list)
{

XSSFRow datarow=sheet.createRow(dataIndex);//创建数据的第一行
XSSFCell idDate=datarow.createCell(0);
idDate.setCellValue(xxxEntity.getId());

XSSFCell deptIdDate=datarow.createCell(1);
deptIdDate.setCellValue(xxxEntity.getDeptId());

XSSFCell oltpwrDate=datarow.createCell(2);
oltpwrDate.setCellValue(xxxEntity.getOltpwr());

XSSFCell ontpwrDate=datarow.createCell(3);
ontpwrDate.setCellValue(xxxEntity.getOntpwr());

dataIndex++;
}
workbook.write(os);
os.flush();
os.close();
workbook.close();
}
catch (Exception e) {
System.out.println(e);
}

}

//注意:当添加DataFilter过滤器注解时,只能拦截到baseMapper接口的方法,同时注意在sql语句添加条件判断语句:

<where>

<if test="params.sql_filter!=null">
and ${params.sql_filter}
</if>
</where>

获取到的参数(接受到的参数)也必须有params.来引用;例如:

<choose>
<when test="params.sidx !='' and params.order!= null">
ORDER BY a.${params.sidx} ${params.order}
</when>
<otherwise>
ORDER BY a.id asc
</otherwise>
</choose>

//根据x名查询DetailEntity表中所有信息
Page<XxxEntity> pagination =(Page<XxxEntity>)new Query<XxxEntity>().getPage(params); 
//根据x名查询DetailEntity表中所有信息
List<XxxEntity> list = (List<XxxEntity>) baseMapper.getWeekLightLimit(pagination,params); 

posted @ 2020-02-28 10:13  唯恐不及  阅读(471)  评论(0编辑  收藏  举报