First, why to read the table

Some time ago, the operation sister complained to the product brother that the new product (SKU, SPU) was too slow, and it was a waste of time. Then the product little brother is very strong to get a batch of import requirements, the back end little brother said, before the interface is good, your front end should be no problem. Then I have this requirement, the management system reads the operational table, then processes the data, generates the SKU, generates the SPU and then relates the corresponding SKU.

Two, happy development, happy meng

After I a Google messy CV, finally is to fix this broken demand.

But little elder brother testing no matter what kind of form didn’t import response (this is obviously not my problem, my computer can), then send me a few form, I found that when development environment import access to data is garbled, 👇 like this (I was stupid, zha test will also Korean)

So the problem is that the data obtained when the import of the encoding is not UTF -*, I can not go down (later I tried a lot of forms, found that the operation of the young lady’s table data encoding strange, many kinds!) , so I had to solve the coding problem.

Three, need to use plug-ins

As we all know, NMP is the baidu for programmers, so after a search, two more suitable plug-ins were found

  • Chardet -> Parse the data encoding format
  • Iconv-lite -> Data encoding format converts to the default encoding format utF8

With these two plugins, you can easily solve the previous problem (yes, in Node)

Finally, I attach my processing code

const Service = require('egg').Service;
const fs = require('fs');
const chardet = require('chardet');
const iconv = require('iconv-lite');
const xlsx = require('xlsx');

class ImportFileService extends Service {
  async parseImportFile( filePath ) {
  	// The data read
    const fileContent = fs.readFileSync(`${filePath}`)
    const codeType = chardet.detect(fileContent);
    
    // Parse the converted data
    const newFileContent = iconv.decode(fileContent, codeType);
    const newCodeType = chardet.detect(newFileContent);
    
    // Convert to string
    const workbook = xlsx.read(newFileContent, {type: 'string'});
    const sheetNames = workbook.SheetNames;
    const worksheet = workbook.Sheets[sheetNames[0]].const sheetJson = xlsx.utils.sheet_to_json(worksheet);
    
    console.log('🍊 oldFileCodeType', codeType)
    console.log('👻 newFileCodeType', newCodeType)
    returnsheetJson; }}module.exports = ImportFileService;
Copy the code

The above –