当前位置:首页 > java > 正文内容

java若依框架ruoyi导入Excel(附详细代码)

关中浪子2个月前 (05-16)java280
【腾讯云】2核2G4M云服务器新老同享99元/年,续费同价

Excel导入

可能出现的问题

1.开发模板下载功能(如需定制列,可以单独创建一个实体类,@Excel注解定义好名字)


2.导出后的列表如果有字典, @Excel(name = “建筑业资质等级”,dictType=“sys_qualifications_leave”)


后台代码

controller层

 /**
     * 导出合理性分析列表
     */
    @RequiresPermissions("business:hlxfx:export")
    @Log(title = "合理性分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcQyHlxfx gcQyHlxfx) throws ParseException {
        List<GcQyHlxfx> list = gcQyHlxfxService.selectGcQyHlxfxList(gcQyHlxfx);
        ExcelUtil<GcQyHlxfx> util = new ExcelUtil<GcQyHlxfx>(GcQyHlxfx.class);
        return util.exportExcel(list, "合理性分析数据");
    }
    //导入数据
    @Log(title = "合理分析", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GcQyHlxfx> util = new ExcelUtil<GcQyHlxfx>(GcQyHlxfx.class);
        List<GcQyHlxfx> gcQyHlxfxList = util.importExcel(file.getInputStream());
        String message = gcQyHlxfxService.importHlxfx(gcQyHlxfxList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }
//模板下载
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<GcQyHlxfxImport> util = new ExcelUtil<GcQyHlxfxImport>(GcQyHlxfxImport.class);
        return util.importTemplateExcel("合理分析");
    }



Service层


     public String importHlxfx(List<GcQyHlxfx> gcQyHlxfxList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(gcQyHlxfxList) || gcQyHlxfxList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GcQyHlxfx res : gcQyHlxfxList)
        {
            try
            {
                // 验证是否存在这个企业
                GcQyJbxx jbxx = gcQyJbxxMapper.selectGcQyJbxxByxydm(res.getSocialCode());
                if (!StringUtils.isNull(jbxx)){
                    LocalDate localDate=LocalDate.now();
                    res.setJzyzzdj(jbxx.getJZYZZDJ());
                    res.setQyszcq(jbxx.getQYSZCQ());
                    GcQyHlxfx sfgcQyHlxfx = new GcQyHlxfx();
                    sfgcQyHlxfx.setSocialCode(res.getSocialCode());
                    DecimalFormat df2 = new DecimalFormat("00");
                    String yMonth=localDate.getYear()+"-"+df2.format(localDate.getMonth().getValue());
                    sfgcQyHlxfx.setYearMonths(yMonth);
                    List<GcQyHlxfx> gcQyHlxfxes = gcQyHlxfxMapper.selectGcQyHlxfxList(sfgcQyHlxfx);
                    GcQyHlxfx gcQyHlxfx = new GcQyHlxfx();
                    String jzbnd = "0";//本平台累积产值
                    if (gcQyHlxfxes.size()>0){
                        gcQyHlxfx = gcQyHlxfxes.get(0);
                        jzbnd = gcQyHlxfx.getBndzcz();
                    }
                    Integer i = 0;
                    Integer c = 0;
                    double b = 0;
                    String bnd = res.getZhptJzcyzcz();//智慧平台本年度累积产值
                    if (bnd.contains(".")){
                        b = Double.parseDouble(bnd);
                        i = (int)b;
                    }else{
                        i = Integer.parseInt(bnd);
                    }
                    if (jzbnd.contains(".")){
                        b = Double.parseDouble(jzbnd);
                        c = (int)b;
                    }else{
                        c = Integer.parseInt(jzbnd);
                    }
                    //计算校核
                   Integer checks = c - i;
                    if (checks>0){
                        res.setDataMatch(1L);
                    }else if (checks==0){
                        res.setDataMatch(2L);
                    }else if (checks<0){
                        res.setDataMatch(3L);
                    }
                    res.setChecks(Long.parseLong(checks.toString()));
                        if (gcQyHlxfxes.size()>0){
                       BeanValidators.validateWithException(validator, res);
                       res.setId(gcQyHlxfx.getId());
                        res.setUpdateBy(operName);
                        //res.setYearMonths(yMonth);
                        res.setGxsj(new Date());
                        res.setGxr(ShiroUtils.getSysUser().getUserId().toString());
                        this.updateGcQyHlxfx(res);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、企业: " + res.getQymc() + " 更新成功");
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、企业: " + res.getQymc() +" 本月未上报");
                    }
                }else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、企业: " + res.getQymc() +" <font color='red'>系统内未查到该企业信息!</font>");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、企业: " + res.getQymc() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "共" + successNum + " 条数据导入成功, " + failureNum + " 条数据格式不正确,错误如下:");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }



JS

                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                importUrl: prefix + "/importData",
                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>
    <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:hlxfx:export">
                    <i class="fa fa-download"></i> 导出
                </a>
                <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="system:hlxfx:export">
                    <i class="fa fa-upload"></i> 导入
                </a>
            </div>




找梯子最重要的就是稳定,这个已经上线三年了,一直稳定没有被封过,赶紧下载备用吧!

扫描二维码推送至手机访问。

版权声明:本文由码农翻生发布,如需转载请注明出处。

本文链接:https://lubojian.cn/post/273.html

分享给朋友:

相关文章

Spring AOP的JDK动态代理和CGLIB代理 的原理

Spring AOP的JDK动态代理和CGLIB代理 的原理

一.AOP的概念         在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统...

静态代理和动态代理的区别

静态代理静态代理,设计模式的代理模式举例通常是用这种方式实现的,对于每一个要代理的类为了添加相同的操作,需要分别去实现其接口,容易造成代理类过多public interface Subject {  ...

java poi读取excel文件(xlsx)

poi读取excel文件先maven导入jar包<dependency>     <groupId>org.apache.poi</groupId>  ...

JAVA POI读取EXCEL 最简洁写法

package com.sunland.poi;   import java.io.FileInputStream; import java.io.IOException;  ...

java Scheduled 设置定时任务为每隔5分执行一次

设置定时任务为每天凌晨2点执行和每小时执行一次每天凌晨2点 0 0 2 * * ?和每天隔一小时 0 * */1 * * ?例1:每隔5秒执行一次:*/5 * * * * ?例2:每隔5分执行一次:0 */5 * * * ?在26分、29分...

nohup 命令详解 >nohup.out 2>&1 &

nohup 命令详解 >nohup.out 2>&1 &

1.nohup用途:不挂断地运行命令。语法:nohup Command [ Arg … ] [ & ]  无论是否将 nohup 命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中。  如果当前目录的 no...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。