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;
}

···

你可能感兴趣的:(前端,javascript)