导出
Student.html
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:student:export">
<i class="fa fa-download"></i> 导出
</a>
//映射路径
exportUrl: prefix + "/export",
@Excel(name = "xxx",cellType = Excel.ColumnType.NUMERIC)
Controller
//导出
@Log(title = "学生管理", businessType = BusinessType.EXPORT)
@RequiresPermissions("system:student:export")
@PostMapping("/export")
@ResponseBody
public AjaxResult export(GXStudent student){
List<GXStudent> list = studentService.selectStudentList(student);
ExcelUtil<GXStudent> util = new ExcelUtil<GXStudent>(GXStudent.class);
return util.exportExcel(list, "学生数据");
}
导入
导入数据之前我们需要模板
Student.html
<a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="system:student:import">
<i class="fa fa-upload"></i> 导入
</a>
importUrl: prefix + "/importStudent",//导入
importTemplateUrl: prefix + "/importTemplate",//导出模板
<!-- 导入区域 -->
<script id="importTpl" type="text/template">
<form enctype="multipart/form-data" class="mt20 mb10">
<div class="col-xs-offset-1">
<input type="file" id="file" name="file"/>
<div class="mt10 pt5">
<input type="checkbox" id="updateSupport" name="updateSupport" title="如果登录账户已经存在,更新这条数据。"> 是否更新已经存在的用户数据
<a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
</div>
<font color="red" class="pull-left mt10">
提示:仅允许导入“xls”或“xlsx”格式文件!
</font>
</div>
</form>
</script>
Controller
/**
* 导入下载模板
*/
@RequiresPermissions("system:student:view")
@GetMapping("/importTemplate")
@ResponseBody
public AjaxResult importTemplate()
{
ExcelUtil<GXStudent> util = new ExcelUtil<GXStudent>(GXStudent.class);
return util.importTemplateExcel("学生数据");
}
/**
* 导入
*/
@RequiresPermissions("system:student:import")
@PostMapping("/importStudent")
@ResponseBody
public AjaxResult importStudent(MultipartFile file, boolean updateSupport) throws Exception
{
ExcelUtil<GXStudent> util = new ExcelUtil<GXStudent>(GXStudent.class);
List<GXStudent> studentList = util.importExcel(file.getInputStream());
String operName = ShiroUtils.getSysUser().getLoginName();
String message = studentService.importStudent(studentList, updateSupport, operName);
return AjaxResult.success(message);
}
Service.java
/**
* 导入学生数据
*
* @param studentList 学生数据列表
* @param updateSupport 是否更新支持,如果已存在,则进行更新数据
* @param operName 是否更新支持,如果已存在,则进行更新数据
* @return 结果
*/
public String importStudent(List<GXStudent> studentList, Boolean updateSupport, String operName) ;
Mapper.java
写入一个方法
public GXStudent selectStudentByName(String student);
ServiceImpl
其中需要导入的Logger包为
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 导入用户数据
*
* @param studentList 用户数据列表
* @param updateSupport 是否更新支持,如果已存在,则进行更新数据
* @param operName 操作用户
* @return 结果
*/
private static final Logger log = LoggerFactory.getLogger(GXStudentService.class);
@Override
public String importStudent(List<GXStudent> studentList, Boolean updateSupport, String operName)
{
if (StringUtils.isNull(studentList) || studentList.size() == 0)
{
throw new BusinessException("导入用户数据不能为空!");
}
int successNum = 0;
int failureNum = 0;
StringBuilder successMsg = new StringBuilder();
StringBuilder failureMsg = new StringBuilder();
for (GXStudent student : studentList)
{
try
{
// 验证是否存在这个用户
GXStudent u = studentMapper.selectStudentByName(student.getName());
System.out.print(student.getSex()+"");
if (StringUtils.isNull(u))
{
student.setName(student.getName());
this.insertStudent(student);
successNum++;
successMsg.append("<br/>" + successNum + "学校信息" + student.getName() + " 导入成功");
}
else if (updateSupport)
{
student.setUpdateBy(operName);
this.updateStudent(student);
successNum++;
successMsg.append("<br/>" + successNum + "学校信息 " + student.getName() + " 更新成功");
}
else
{
failureNum++;
failureMsg.append("<br/>" + failureNum + "学校信息" + student.getName() + " 已存在");
}
}
catch (Exception e)
{
failureNum++;
String msg = "<br/>" + failureNum + "学校信息" + student.getName() + " 导入失败:";
failureMsg.append(msg + e.getMessage());
log.error(msg, e);
}
}
if (failureNum > 0)
{
failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
throw new BusinessException(failureMsg.toString());
}
else
{
successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
}
return successMsg.toString();
}
Mapper.xml
<!-- 导入数据 -->
<select id="selectStudentByName" parameterType="String" resultMap="GXStudentResult">
<include refid="selectStudentVo"/>
where name = #{name}
</select>