前端 + 后台(实现 导出 )功能

本人已知的导出实现方式有三种:

1.点击链接、2.解析后台返回的文件流、3.接收数据,纯前端实现

  1. 点击链接:数据、文件格式全部在后台封装好,返回给前端一个链接,前端通过点击链接自动下载,两种方式:
    /* 第一种 */
    window.location.href = 'url';
    
    /* 第二种 */
    <a href='url' download=''></a>
  2. 解析后台返回的文件流:这种方式就是后台将要导出的文件以文件流的方式返回给前端,前端通过blob去解析,再动态创建a标签。
    /* 导出(HTML)解释:form ---代表后端需要的参数数据 */
    <el-button size="mini" icon="el-icon-upload2" type="primary" @click="exportCouponFn(form)">
      导出
    </el-button>         
     
    /* 导出(JS) */
    exportCouponFn (form) {
        this.$message.info('正在导出,请稍等...')
        setTimeout(() => {
            exportCouponApi(form.id).then(res => {  // 封装好的接口Api请求
              if (200 === res.status) {
                let content = res.data;  // 文件流
                let blob = new Blob([content], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});                   
    
                let contentDisposition = res.headers['content-disposition'];
                let fileName = decodeURI(contentDisposition.split('=')[1]);
    
                if ('download' in document.createElement('a')) {  // 非IE下载
                  let link = document.createElement('a');
                  link.download = '批量指定多商优惠券导出信息';  // 文件标题名
                  link.target = "_blank";
                  link.href = URL.createObjectURL(blob);
                  document.body.appendChild(link);
                  link.click();
                  URL.revokeObjectURL(link.href);  // 释放URL 对象
                  document.body.removeChild(link);
    
                 } else {
                  // IE10 + Download
                  navigator.msSaveBlob(blob, fileName);
                 }
    
                 Message.closeAll()
                 this.$message.success('导出成功~')
               }
            }).catch(err => {
              console.error('接口错误err', err)
            })
        }, 1000)
    }
                  
                  
                  
                
                
  3. 接收数据,纯前端实现:这种方式就是后台只需提供对应的数据即可,前端动态生成表格数据,再格式化。
    let excel = '<table>';
    // 生成表头
    let row = '<tr>' +
      '<td>标题1</td>' +
      '<td>标题2</td>' +
      '<td>标题3</td>' +
      '</tr>';
    excel += row + '</tr>';
    // 循环生成表身
    for(let i = 0 ; i < this.excelData.length ; i++ ){
      excel += '<tr>';
      for(let item in this.excelData[i]){
        //增加\t为了不让表格显示科学计数法或者其他格式
        if (!this.excelData[i][item]) {
          this.excelData[i][item] = '';
        }
        excel +=`<td>${ this.excelData[i][item] + '\t'}</td>`;
      }
      excel +='</tr>';
    }
    excel += '</table>';
    //下载的表格模板数据
    var excelFile = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' +
      'xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">';
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel"';
    excelFile += ' charset="UTF-8">';
    excelFile += '<head>';
    excelFile += '<!--[if gte mso 9]>';
    excelFile += '<xml>';
    excelFile += '<x:ExcelWorkbook>';
    excelFile += '<x:ExcelWorksheets>';
    excelFile += '<x:ExcelWorksheet>';
    excelFile += '<x:Name>';
    excelFile += 'sheet';
    excelFile += '</x:Name>';
    excelFile += '<x:WorksheetOptions>';
    excelFile += '<x:DisplayGridlines/>';
    excelFile += '</x:WorksheetOptions>';
    excelFile += '</x:ExcelWorksheet>';
    excelFile += '</x:ExcelWorksheets>';
    excelFile += '</x:ExcelWorkbook>';
    excelFile += '</xml>';
    excelFile += '<![endif]-->';
    excelFile += '</head>';
    excelFile += '<body>';
    excelFile += excel;
    excelFile += '</body>';
    excelFile += '</html>';
    //下载模板
    let uri = 'data:application/vnd.ms-excelcharset=utf-8,' + encodeURIComponent(excelFile);
    let link = document.createElement('a');
    link.href = uri;
    link.style = 'visibility:hidden';
    let myDate = new Date();
    let time = myDate.toLocaleDateString().split('/').join('-');
    link.download = 'fileName' + time + '.xls';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于Java前后端分离的项目,Spring Boot提供了方便的操作SQLite数据库的功能。以下是导入和导出SQLite文件的步骤: 1. 确定SQLite版本:Spring Boot默认使用的是3.8.11.2版本,如果需要使用其他版本,可以在application.properties文件中修改。 2. 添加SQLite的依赖:在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>3.34.0</version> </dependency> ``` 3. 配置SQLite数据库:在application.properties文件中添加以下配置: ```properties # 配置SQLite数据库文件的路径 spring.datasource.url=jdbc:sqlite:/path/to/your/sqlite.db # 配置SQLite数据库驱动类 spring.datasource.driver-class-name=org.sqlite.JDBC # 配置SQLite数据库用户名和密码(可选) spring.datasource.username=yourusername spring.datasource.password=yourpassword ``` 4. 导入SQLite文件:在Java代码中使用以下代码导入SQLite文件: ```java // 获取数据源 DataSource dataSource = DataSourceBuilder.create() .driverClassName("org.sqlite.JDBC") .url("jdbc:sqlite:/path/to/your/sqlite.db") .build(); // 获取数据库连接 Connection connection = dataSource.getConnection(); // 执行导入命令 Statement statement = connection.createStatement(); String sql = "ATTACH DATABASE '/path/to/your/importfile.db' AS importDB; " + "INSERT INTO main.table SELECT * FROM importDB.table;"; statement.executeUpdate(sql); // 关闭连接 statement.close(); connection.close(); ``` 其中,`importfile.db`是要导入的SQLite文件,`main.table`是要导入数据的目标表,`importDB.table`是要导入数据的源表。 5. 导出SQLite文件:在Java代码中使用以下代码导出SQLite文件: ```java // 获取数据源 DataSource dataSource = DataSourceBuilder.create() .driverClassName("org.sqlite.JDBC") .url("jdbc:sqlite:/path/to/your/sqlite.db") .build(); // 获取数据库连接 Connection connection = dataSource.getConnection(); // 执行导出命令 Statement statement = connection.createStatement(); String sql = "ATTACH DATABASE '/path/to/your/exportfile.db' AS exportDB; " + "CREATE TABLE exportDB.table AS SELECT * FROM main.table;"; statement.executeUpdate(sql); // 关闭连接 statement.close(); connection.close(); ``` 其中,`exportfile.db`是要导出的SQLite文件,`main.table`是要导出数据的源表,`exportDB.table`是要导出数据的目标表。 注意:在执行导入和导出命令前,需要确保SQLite数据库文件和导入/导出的SQLite文件都存在,并且具有读写权限。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值