Home > Web Front-end > Vue.js > body text

How to export and import table data in Vue project

WBOY
Release: 2023-10-08 09:42:30
Original
798 people have browsed it

How to export and import table data in Vue project

How to implement the export and import of table data in Vue projects requires specific code examples

Introduction
In Vue projects, tables are very common and important One of the components. In actual projects, we often encounter the need to export table data to Excel or import data into Excel to display in a table. This article will introduce in detail how to export and import table data in the Vue project, and provide specific code examples.

  1. Table data export
    To implement table data export in Vue, we can use existing mature open source libraries, such as xlsx and file-saver.

First, we need to install these two libraries in the Vue project. Open the terminal, enter the project directory, and enter the following command:

npm install xlsx file-saver --save
Copy after login

After the installation is completed, we need to introduce these two libraries into the components that need to export the table:

import XLSX from 'xlsx';
import FileSaver from 'file-saver';
Copy after login

Next, we need Define a method to export tabular data. Assume that our table data is an array tableData:

exportTableData() {
  const worksheet = XLSX.utils.json_to_sheet(this.tableData);
  const workbook = XLSX.utils.book_new();
  XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
  const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
  const dataBlob = new Blob([excelBuffer], { type: 'application/octet-stream' });
  FileSaver.saveAs(dataBlob, 'tableData.xlsx');
}
Copy after login

In the above code, the XLSX.utils.json_to_sheet method converts our table data to work in Excel Sheet, XLSX.utils.book_newCreate a new workbook, XLSX.utils.book_append_sheetAdd the worksheet to the workbook.

Then, write the workbook into excelBuffer through the XLSX.write method, and finally excelBuffer through the FileSaver.saveAs method Save as Excel file.

On the page, we can call the export method through a button:

<button @click="exportTableData">导出表格数据</button>
Copy after login

Finally, when the export button is clicked, the table data will be exported to a file named tableData.xlsxExcel file.

  1. Table data import
    To implement the import of table data in Vue, we can also use the xlsx library.

First of all, we also need to install the xlsx library in the Vue project. Open the terminal, enter the project directory, and enter the following command:

npm install xlsx --save
Copy after login

After the installation is complete, we need to introduce the xlsx library into the table component:

import XLSX from 'xlsx';
Copy after login

Next, we define A method to import table data:

importTableData(file) {
  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 });
    // 处理jsonData,将数据显示在表格中...
  };
  reader.readAsArrayBuffer(file);
}
Copy after login

In the above code, we use FileReader to read the uploaded Excel file. When the reading is complete, we convert the data into Uint8Array and then use the XLSX.read method to parse the data into a workbook.

You can get the name of the first worksheet through workbook.SheetNames[0], and convert the data in the worksheet into Array in JSON format. After reading and converting the data is completed,

jsonData

can be further processed as needed, such as storing the data to a database or displaying it in a table. Finally, we trigger the import method through an upload button:

<input type="file" @change="importTableData($event.target.files[0])">
Copy after login

When the Excel file is selected, the

importTableData

method will be called and the file will be passed as a parameter. this method. Summary

Through the above code examples, we can implement the table data export and import functions in the Vue project. For tabular data export, we use the

xlsx
and file-saver libraries to help us export data to Excel files; for tabular data import, we use the xlsx library to help us export data to Excel files. Parse the uploaded Excel file and convert the data into a processable format. The implementation of these functions can improve user experience and data processing efficiency in actual projects. I hope this article can be helpful to everyone in exporting and importing table data in Vue projects. If you have any questions or concerns, please feel free to leave a message. Thanks!

The above is the detailed content of How to export and import table data in Vue project. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template