若依框架——>导出,导入

导出

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="如果登录账户已经存在,更新这条数据。"> 是否更新已经存在的用户数据
					&nbsp;	<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>
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值