• EasyPoi导入数据后,导出发生错误的数据报[object Object]


    后台输出流到前端

    //有错误信息,就导出文件
                if (errorList != null && errorList.size() > 0) {
                    String title = "导入国产药数据的异常数据";
                    ExcelUtils.exportExcel(errorList, title, title, StandardCode.class, title, response);
                    return null;
                }

    在响应拦截器中加以下代码:

    if (headers['content-type'].toLowerCase() === 'application/vnd.ms-excel;charset=utf-8') {
              return response
          }

    如果不加这段代码会报错:

    No converter for [class com.ljxx.entity.Result] with preset Content-Type 'application/vnd.ms-excel;charset=UTF-8'

    由于上述代码返回的是response,故使用filedownload下载的时候应该为response.data

    handleUploadChange1(file) {
          if (file.name.lastIndexOf('.') < 0) {
            this.$message.error('上传文件只能是xls、xlsx格式!')
            return
          }
          const testMsg = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
          const extensionXLS = testMsg == 'xls'
          const extensionXLSX = testMsg == 'xlsx'
          if (!extensionXLS && !extensionXLSX) {
            this.$message.error('上传文件只能是xls、xlsx格式!')
            return
          }
          const isLt2M = file.size / 1024 / 1024 < 2
          if (!isLt2M) {
            this.$message.error('上传文件不能超过 2MB!')
            return
          }
          console.log('import continue')
          this.importLoading = true
          this.importDisabled = true
          const data = new FormData()
          data.append('file', file.raw)
          standardCode.importDomesticExcel(data).then(response => {
            console.log(response)
            if (response.success) {
              this.open2(response.msg)
              this.importLoading = false
              this.importDisabled = false
              this.getList()
            } else {
              this.$message.success("部分数据导入失败,数据已下载到本地,请查看!")
              fileDownload(response.data, '导入数据中的异常数据.xlsx')
              // this.open2(response.msg)
              this.importLoading = false
              this.importDisabled = false
            }
          }).catch(() => {
            this.open2('抱歉,导入失败')
            this.importLoading = false
            this.importDisabled = false
          })
        },

     如果报错文件打不开,如下:

     是因为responseType值应为arraybuffer,而不能是arraybuffer;charset=utf-8

    importDomesticExcel(data) {
        return request({
          url: '/standardCode/import',
          method: 'post',
          data: data,
          responseType: 'arraybuffer',
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
      },
  • 相关阅读:
    Mac下安装Navicat12之破解
    重新认识spring
    冒泡排序(二)
    算法入门篇(一)
    elasticsearch复杂查询-----2
    idea maven web工程明明添加了maven lib的依赖,但启动web容器时始终报No Class Found?
    idea 多模块项目依赖父工程class找不到问题
    apache-maven-3.3.9集成apache-tomcat-7.0.72实现热部署配置细节
    WebService CXF调试常见报错及解决方案
    通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案
  • 原文地址:https://www.cnblogs.com/zwh0910/p/15176126.html
Copyright © 2020-2023  润新知