The front end uses the xlsx plug-in to read excel file data

Using xlsxa plug-in to read Excel file data on the front end has the following advantages and disadvantages, and is applicable to the following scenarios:

advantage:

  1. Easy to use: xlsxThe plug-in provides a simple API to read Excel file data without complex configuration and dependencies.
  2. Complete functionality: xlsxThe plug-in supports reading various Excel file formats, including common formats such as XLS and XLSX, and can read multiple worksheets and multiple data types.
  3. Cross-platform support: xlsxThe plug-in can be used in the browser and Node.js environment, and is suitable for the Excel data processing needs of the front end and the back end.
  4. Data conversion: xlsxThe plug-in can convert Excel data into common data formats such as JSON and CSV, which is convenient for further processing and display on the front end.

shortcoming:

  1. Performance issues: For large Excel files, xlsxthe add-in can cause performance issues as it needs to load the entire file into memory for parsing.
  2. Browser compatibility: Due to browser security policy restrictions, some browsers may not support direct reading of local files, and you need to use a back-end API to receive uploaded files.

scenes to be used:

  1. Data import: When users need to import data from Excel files into front-end applications, they can use xlsxplug-ins to read Excel file data and convert them into data formats usable by the front-end.
  2. Data analysis: For applications that require data analysis, you can use xlsxplug-ins to read data in Excel files and perform statistics, calculations and other operations.
  3. Data display: When you need to display data in an Excel file on the front end, you can use xlsxa plug-in to read the Excel file data and convert it into a format suitable for display, such as tables, charts, etc.

It should be noted that for large Excel files or scenarios that require complex data processing, you may need to consider using a back-end Excel processing library to improve performance and flexibility.

Detailed code description

To use a plug-in to read Excel file data on the front end xlsx, you need to install the plug-in first xlsx. It can be installed with the following command:

npm install xlsx

After the installation is complete, you can import the module in the front-end code xlsxand use the methods it provides to read Excel file data. Here is a sample code:

import XLSX from 'xlsx';

// 读取Excel文件数据
function readExcelFile(file) {
    
    
  return new Promise((resolve, reject) => {
    
    
    const reader = new FileReader();
    reader.onload = (e) => {
    
    
      const data = new Uint8Array(e.target.result);
      const workbook = XLSX.read(data, {
    
     type: 'array' });
      const worksheet = workbook.Sheets[workbook.SheetNames[0]];
      const jsonData = XLSX.utils.sheet_to_json(worksheet, {
    
     header: 1 });
      resolve(jsonData);
    };
    reader.onerror = (e) => {
    
    
      reject(e);
    };
    reader.readAsArrayBuffer(file);
  });
}

// 选择文件并读取数据
document.getElementById('file-input').addEventListener('change', (e) => {
    
    
  const file = e.target.files[0];
  if (file) {
    
    
    readExcelFile(file).then((data) => {
    
    
      console.log(data);
      // 在这里处理读取到的Excel数据
    }).catch((error) => {
    
    
      console.error(error);
    });
  }
});

In the above code, we defined a readExcelFilefunction to read Excel file data. This function uses FileReaderan object to read the file into binary data, then uses XLSX.reada method to parse the binary data into a workbook object, and then uses XLSX.utils.sheet_to_jsona method to convert the worksheet data into JSON format. Finally, we use Promiseto return the read data.

In the page, we use <input type="file">elements to select Excel files, and changelisten to changes in file selection through events. When the user selects a file, we call readExcelFilethe function to read the file data, and thenprocess the read data in the callback.

Note: Due to browser security policy restrictions, some browsers may not support direct reading of local files. In this case, you may need to use the backend API to receive the uploaded Excel file and return it to the frontend after the backend parses the file data.

multiple methods

When using xlsxa plug-in to read Excel file data on the front end, you can use the following methods:

1. Read the entire workbook data:

const readWorkbook = (workbook) => {
    
    
  const worksheet = workbook.Sheets[workbook.SheetNames[0]];
  const jsonData = XLSX.utils.sheet_to_json(worksheet, {
    
     header: 1 });
  console.log(jsonData);
};
const readExcelFile = (file) => {
    
    
  return new Promise((resolve, reject) => {
    
    
    const reader = new FileReader();
    reader.onload = (e) => {
    
    
      const data = new Uint8Array(e.target.result);
      const workbook = XLSX.read(data, {
    
     type: 'array' });
      readWorkbook(workbook);
      resolve();
    };
    reader.onerror = (e) => {
    
    
      reject(e);
    };
    reader.readAsArrayBuffer(file);
  });
};

In this method, we first define a readWorkbookfunction for reading workbook data. Then, in readExcelFilethe function, we use FileReaderthe object to read the file into binary data, and then use XLSX.readthe method to parse the binary data into a workbook object. Finally, we call readWorkbookthe function to read the workbook data.

2. Read the specified worksheet data:

const readWorksheet = (worksheet) => {
    
    
  const jsonData = XLSX.utils.sheet_to_json(worksheet, {
    
     header: 1 });
  console.log(jsonData);
};
const readExcelFile = (file) => {
    
    
  return new Promise((resolve, reject) => {
    
    
    const reader = new FileReader();
    reader.onload = (e) => {
    
    
      const data = new Uint8Array(e.target.result);
      const workbook = XLSX.read(data, {
    
     type: 'array' });
      const worksheet = workbook.Sheets['Sheet1']; // 指定工作表名称
      readWorksheet(worksheet);
      resolve();
    };
    reader.onerror = (e) => {
    
    
      reject(e);
    };
    reader.readAsArrayBuffer(file);
  });
};

In this method, we first define a readWorksheetfunction to read the data of the specified worksheet. Then, in readExcelFilethe function, we use FileReaderthe object to read the file into binary data, and then use XLSX.readthe method to parse the binary data into a workbook object. Finally, we use workbook.Sheets['Sheet1']to get the worksheet with the specified name, and call readWorksheetthe function to read the worksheet data.

3. Read multiple worksheet data:

const readWorkbook = (workbook) => {
    
    
  workbook.SheetNames.forEach((sheetName) => {
    
    
    const worksheet = workbook.Sheets[sheetName];
    const jsonData = XLSX.utils.sheet_to_json(worksheet, {
    
     header: 1 });
    console.log(`Sheet Name: ${
      
      sheetName}`);
    console.log(jsonData);
  });
};
const readExcelFile = (file) => {
    
    
  return new Promise((resolve, reject) => {
    
    
    const reader = new FileReader();
    reader.onload = (e) => {
    
    
      const data = new Uint8Array(e.target.result);
      const workbook = XLSX.read(data, {
    
     type: 'array' });
      readWorkbook(workbook);
      resolve();
    };
    reader.onerror = (e) => {
    
    
      reject(e);
    };
    reader.readAsArrayBuffer(file);
  });
};

In this method, we first define a readWorkbookfunction for looping to read data from multiple worksheets. Then, in readExcelFilethe function, we use FileReaderthe object to read the file into binary data, and then use XLSX.readthe method to parse the binary data into a workbook object. Finally, we workbook.SheetNames.forEachloop through the sheet names with , and call readWorkbookthe function to read each sheet's data.

These methods can be selected and combined according to specific needs to read data in Excel files.

Guess you like

Origin blog.csdn.net/ACCPluzhiqi/article/details/131614062