当前位置: 首页 > 工具软件 > D2admin > 使用案例 >

D2admin导出Execl

计和顺
2023-12-01
 
 
<template>
  <el-button @click="exportToExcel">导出Excel</el-button>
 
  <el-table  :data="tableData3"  style="width: 100%" id="table-content">
    <el-table-column  prop="date"  label="日期"  width="150">
    </el-table-column>
    <el-table-column label="配送信息">
      <el-table-column  prop="name"  label="姓名"  width="120">
      </el-table-column>
      <el-table-column label="地址">
        <el-table-column  prop="province"  label="省份"  width="120">
        </el-table-column>
        <el-table-column  prop="city"  label="市区"  width="120">
        </el-table-column>
        <el-table-column  prop="address"  label="地址"  width="300">
        </el-table-column>
        <el-table-column  prop="zip" label="邮编"  width="120">
        </el-table-column>
      </el-table-column>
    </el-table-column>
  </el-table>
</template>
 
<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
 
  export default {
    data() {
      return {
        tableData3: [{
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }, {
          date: '2016-05-02',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }]
      }
    },methods:{
exportToExcel () {
    let et = XLSX.utils.table_to_book(document.getElementById('table-content')); 
    //document.getElementById('xxxx'):XXX此处传入table的DOM节点,即el-table的id名称
    let etout = XLSX.write(et, {
        bookType: 'xlsx', 
        bookSST: true, 
        type: 'array' 
    });
    try {
        FileSaver.saveAs(new Blob([etout], { 
            type: 'application/octet-stream' 
        }), 'table.xlsx');   //XXX.xlsx 为导出的文件名
    } catch (e) {
        console.log(e, etout) ;
    }
    return etout;
},
//打印页面内容
printContent(){
    let wpt = document.getElementById('table-content');
    let newContent = wpt.innerHTML;
    let oldContent = document.body.innerHTML;
    document.body.innerHTML = newContent;
    window.print(); //打印方法
    window.localtion.reload();
    document.body.innerHTML = oldContent;
}
}
  }
</script>
 
<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
 
  export default {
    data() {
      return {
        tableData3: [{
          date: '2016-05-03',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }, {
          date: '2016-05-02',
          name: '王小虎',
          province: '上海',
          city: '普陀区',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333
        }]
      }
    },methods:{
exportToExcel () {
    let et = XLSX.utils.table_to_book(document.getElementById('table-content')); 
    //document.getElementById('xxxx'):XXX此处传入table的DOM节点,即el-table的id名称
    let etout = XLSX.write(et, {
        bookType: 'xlsx', 
        bookSST: true, 
        type: 'array' 
    });
    try {
        FileSaver.saveAs(new Blob([etout], { 
            type: 'application/octet-stream' 
        }), 'table.xlsx');   //XXX.xlsx 为导出的文件名
    } catch (e) {
        console.log(e, etout) ;
    }
    return etout;
},
//打印页面内容
printContent(){
    let wpt = document.getElementById('table-content');
    let newContent = wpt.innerHTML;
    let oldContent = document.body.innerHTML;
    document.body.innerHTML = newContent;
    window.print(); //打印方法
    window.localtion.reload();
    document.body.innerHTML = oldContent;
}
}
  }
</script>
 类似资料: