使用前端读取CSV文件,csv中类似于8888-8的数据被读取为日期
private readCsvOrExcelCompatiblyANSI(file): Promise<any> {
return new Promise(function (resolve, reject) {
let wb,// file content
rABS = false, //is read file as binary
isCSV,
reader = new FileReader(),
is_file_contains_needed_columns = true,
is_file_contains_Y_nes = true;
reader.onload = () => {
try {
let data:any = reader.result;
wb = null;
if (isCSV) {
data = new Uint8Array(data);
let isUTF8Coding = this.isUTF8(data);
if (isUTF8Coding) {
data = reader.result;
} else {
let decodeF = window['cptable']['utils']['decode'];
let str = decodeF(936, data);
wb = XLSX.read(str, { type: "string" });
}
}
if (!wb) {
let a = btoa(this.fixdata(data))
console.log(a)
wb = rABS || isCSV ? XLSX.read(a, { type: 'base64' }) : XLSX.read(data, { type: 'binary' });
console.log("wb: ", wb)
}
let fileContent = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]], { header: 1, raw: false });
// 1.check needed heads
let head = fileContent[0];
这里面
let a = btoa(this.fixdata(data)) 是:
private fixdata(data) { //file stream transfer to binary
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;
运行的结果是没有错误的:
但是在运行这一行 wb = rABS || isCSV ? XLSX.read(a, { type: 'base64' }) : XLSX.read(data, { type: 'binary' });
就会发生读取成日期的问题