当前位置:   article > 正文

js 预览excel/csv

js 预览excel/csv

git地址: https://gitee.com/nulicainiao/analyzing-excel-csv.git

一、直接上代码

1.下载引入 使用xlsx.js 插件 及 cptable.js文件

import XLSX from './xlsx'
import cptable from './cptable.full'




function fixdata(data) { //文件流转BinaryString
 var o = "",
   l = 0,
   w = 10240;
 for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
 o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
 return o;
}





export default function importf(file, onResult) {//导入
 var wb;//读取完成的数据
 var rABS = false; //是否将文件读取为二进制字符串
 var isCSV;

 if (!file)
   return;

 let fileObj = file;
 var reader = new FileReader();
 reader.onload = (e) => {
   var data = e.target.result;
   wb = null;
   if (isCSV) {
     data = new Uint8Array(data);
     let f = isUTF8(data);
     // document.getElementById("ff").innerHTML = "是CSV文件,编码" + (f ? "是" : "不是") + "UTF-8";
     if (f) {
       //data = e.target.result;
       readCsvUtf8(fileObj, onResult)
       return false
     } else {

       let str = cptable.utils.decode(936, data);
       wb = XLSX.read(str, { type: "string" });
     }
   }else{
     // document.getElementById("ff").innerHTML ="不是CSV文件"
   }

   if (!wb) {
     wb = rABS|| isCSV ? XLSX.read(Buffer.toString(fixdata(data), 'base64'), { type: 'base64' }) : XLSX.read(data, { type: 'binary' });
   }

   let result =  XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]],{header:1,defval:''})
   let tableHeadTemp = result.shift()
   let tableBody = result.map((item)=> {
     const obj = {}
     tableHeadTemp.map((v,i)=> {
       obj[v] = item[i]
     })
     return obj
   })
   let tableHead = tableHeadTemp.map(item=> ({ prop: item, label: item, visible: true }))

   onResult({tableHead, tableBody})

   //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
   //wb.Sheets[Sheet名]获取第一个Sheet的数据
   // document.getElementById("demo").innerHTML = JSON.stringify();
 };
 isCSV = fileObj.name.split(".").reverse()[0] == "csv";//判断是否是 CSV
 if (rABS || isCSV) {
   reader.readAsArrayBuffer(fileObj);
 } else {
   reader.readAsBinaryString(fileObj);
 }
 file = null
}


function readCsvUtf8(file, onResult) {
 const reader = new FileReader();
 reader.onload = function () {
   const data = reader.result;
   let wb = XLSX.read(data, { type: "string" });
   const result = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]],{header:1,defval:''})
   let tableHeadTemp = result.shift()
   let tableBody = result.map((item)=> {
     const obj = {}
     tableHeadTemp.map((v,i)=> {
       obj[v] = item[i]
     })
     return obj
   })
   let tableHead = tableHeadTemp.map(item=> ({ prop: item, label: item, visible: true }))
   onResult({tableHead, tableBody})
 };
 reader.readAsText(file);
}

function isUTF8(bytes) {
 var i = 0;
 while (i < bytes.length) {
   if ((// ASCII
     bytes[i] == 0x09 ||
     bytes[i] == 0x0A ||
     bytes[i] == 0x0D ||
     (0x20 <= bytes[i] && bytes[i] <= 0x7E)
   )
   ) {
     i += 1;
     continue;
   }

   if ((// non-overlong 2-byte
     (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
     (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF)
   )
   ) {
     i += 2;
     continue;
   }

   if ((// excluding overlongs
       bytes[i] == 0xE0 &&
       (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
       (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
     ) ||
     (// straight 3-byte
       ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
         bytes[i] == 0xEE ||
         bytes[i] == 0xEF) &&
       (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
       (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
     ) ||
     (// excluding surrogates
       bytes[i] == 0xED &&
       (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x9F) &&
       (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
     )
   ) {
     i += 3;
     continue;
   }

   if ((// planes 1-3
       bytes[i] == 0xF0 &&
       (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
       (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
       (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
     ) ||
     (// planes 4-15
       (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
       (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
       (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
       (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
     ) ||
     (// plane 16
       bytes[i] == 0xF4 &&
       (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
       (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
       (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
     )
   ) {
     i += 4;
     continue;
   }
   return false;
 }
 return true;
}

···

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/128227
推荐阅读
  

闽ICP备14008679号