public Result userExportExcel(HttpServletResponse response) {
try {
//创建excel
XSSFWorkbook sheets = new XSSFWorkbook();
//创建行
XSSFSheet sheet = sheets.createSheet("用户信息");
//格式设置
XSSFCellStyle cellStyle = sheets.createCellStyle();
//横向居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//创建单元格第一列
XSSFRow row = sheet.createRow(0);
//表头
this.titleExcel(row,cellStyle);
//查询全部的用户数据 mybatis-plus
List<User> list = list();
//遍历设置值
for(int i=0;i<list.size();i++){
XSSFRow rows = sheet.createRow(i+1);
User user=list.get(i);
//表格里赋值
this.titleExcelValue(user,rows,cellStyle);
}
//设置浏览器响应格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
String filName= URLEncoder.encode("用户信息","UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+filName+".xls");
ServletOutputStream outputStream=response.getOutputStream();
sheets.write(outputStream);
outputStream.close();
sheets.close();
return Result.success();
}catch (Exception e){
e.printStackTrace();
log.info("error:{}",e);
}
return Result.error("300","导出失败");
}
/**
*表格里赋值
**/
public void titleExcelValue(User user, XSSFRow row,XSSFCellStyle cellStyle) {
XSSFCell cellId = row.createCell(0);
cellId.setCellValue(user.getUid());
cellId.setCellStyle(cellStyle);
XSSFCell cellUserName = row.createCell(1);
cellUserName.setCellValue(user.getUname());
cellUserName.setCellStyle(cellStyle);
XSSFCell cellPassword = row.createCell(2);
cellPassword.setCellValue(user.getUpassword());
cellPassword.setCellStyle(cellStyle);
XSSFCell cellSex = row.createCell(3);
cellSex.setCellValue(user.getUsex());
cellSex.setCellStyle(cellStyle);
XSSFCell cellRole = row.createCell(4);
cellRole.setCellValue(user.getRole());
cellRole.setCellStyle(cellStyle);
XSSFCell cellLoveValue = row.createCell(5);
cellLoveValue.setCellValue(user.getRole());
cellLoveValue.setCellStyle(cellStyle);
XSSFCell cellPhone = row.createCell(6);
cellPhone.setCellValue(user.getUphoto());
cellPhone.setCellStyle(cellStyle);
XSSFCell cellAddress = row.createCell(7);
cellAddress.setCellValue(user.getUaddress());
cellAddress.setCellStyle(cellStyle);
}
/**
表头
**/
public void titleExcel(XSSFRow row,XSSFCellStyle cellStyle){
XSSFCell cellId = row.createCell(0);
cellId.setCellValue("用户ID");
cellId.setCellStyle(cellStyle);
XSSFCell cellUserName = row.createCell(1);
cellUserName.setCellValue("用户名");
cellUserName.setCellStyle(cellStyle);
XSSFCell cellPassword = row.createCell(2);
cellPassword.setCellValue("密码");
cellPassword.setCellStyle(cellStyle);
XSSFCell cellSex = row.createCell(3);
cellSex.setCellValue("性别");
cellSex.setCellStyle(cellStyle);
XSSFCell cellRole = row.createCell(4);
cellRole.setCellValue("角色");
cellRole.setCellStyle(cellStyle);
XSSFCell cellLoveValue = row.createCell(5);
cellLoveValue.setCellValue("爱心值");
cellLoveValue.setCellStyle(cellStyle);
XSSFCell cellPhone = row.createCell(6);
cellPhone.setCellValue("电话号码");
cellPhone.setCellStyle(cellStyle);
XSSFCell cellAddress = row.createCell(7);
cellAddress.setCellValue("地址");
cellAddress.setCellStyle(cellStyle);
}
|