<template>
    <div class="excel-reader">
      <input type="file" @change="handleFileSelect" />
      <button  @click="exportToExcel" >导出</button>
      ==================================================

      <div>单行转换<input type="text" v-model="sqltext" style="margin: 0px 10px;"/> 
        <button  @click="exportToExcelOneline" > 导出单行</button> <br/>
        编译后数据  <input type="text" v-model="sqltext2">
      </div>
      <table>
        <thead>
          <tr>
            <th v-for="(column, index) in headers" :key="index">{{ column }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(row, index) in rows" :key="index">
            <td v-for="(item, i) in row" :key="i">{{ item }}</td>
          </tr>
        </tbody>
      </table>
    </div>
  </template>
  
  <script>
  import { read, utils ,write} from 'xlsx';
  import { saveAs } from 'file-saver';
  import CryptoJS from 'crypto-js';

  export default {
    data() {
      return {
        rows: [],
        headers: [],
        sqltext:'',
        sqltext2:''
      };
    },
    methods: {
      exportToExcelOneline(){
        if(!this.sqltext)return;
        this.sqltext2 = this.addCryto(this.sqltext)
      },
      addCryto(str){
        let key = '736a6677797130313233343536373839'
        key = CryptoJS.enc.Hex.parse(key)
        const encrypted = CryptoJS.AES.encrypt(str,key,{
            mode:CryptoJS.mode.ECB,
            padding:CryptoJS.pad.Pkcs7
        })
        return encrypted.ciphertext.toString()
      },
      async handleFileSelect(event) {
        let vm = this
        const file = event.target.files[0];
        const reader = new FileReader();
        reader.onload = (e) => {
          const data = new Uint8Array(e.target.result);
          const workbook = read(data, { type: 'array' });
          const sheetName = workbook.SheetNames[0]
          const worksheet = workbook.Sheets[sheetName];
          const json = utils.sheet_to_json(worksheet, { header: 1 });

        
          if (json.length > 0) {
            this.headers = json.shift();
            this.rows = json;
            for (let index = 0; index < this.rows.length; index++) {
              const element = this.rows[index];
              // console.log(element[8])
              element[9]=vm.addCryto(element[8])
            }

            console.log(this.rows)
          }
        };
        reader.readAsArrayBuffer(file);
      },
      exportToExcel() {
        const worksheet = utils.json_to_sheet(this.rows, { header: this.headers });
        const workbook = utils.book_new();
        utils.book_append_sheet(workbook, worksheet);
        const excelBuffer = write(workbook, { bookType: 'xlsx', type: 'array' });
        const filename = '编译后.xlsx';
        saveAs(new Blob([excelBuffer], { type: 'application/octet-stream' }), filename);
      }
    }
  };
  </script>
  
  <style scoped>
  table {
    border-collapse: collapse;
    width: 100%;
  }
  
  th,
  td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
  }
  
  th {
    background-color: #4caf50;
    color: white;
  }
  </style>
  