当前位置:网站首页>Three ways to export excel from pages
Three ways to export excel from pages
2022-06-23 03:37:00 【Confused little Tao】
One Pure front-end implementation ①
If the amount of data is small , And there is no processing logic , You can use that , The package is used file-saver
<div style="padding-top: 30px;">
<el-button type="primary" @click="exportData"> Derived data </el-button>
</div>
<el-table ref="multipleTableData" :data="tableData" :stripe="true">
<el-table-column type="selection" min-width="2" :reserve-selection="true" align="center" />
<el-table-column :resizable="false" prop="name" label=" The student's name " align="center" />
<el-table-column :resizable="false" prop="age" label=" Age " align="center" />
<el-table-column :resizable="false" prop="area" label=" region " align="center" />
</el-table><script>
import fileSaver from 'file-saver'
// Derived data
exportData() {
const dataArray = this.$refs.multipleTableData.selection
console.log(dataArray, 'dataArray')
let dataString = ` full name \t Age \t The province where it is located \n` // Header text
for (let i = 0; i < dataArray.length; i++) {
for (const key in dataArray[i]) {
dataString += `${dataArray[i][key] + '\t'}` // Each row of data is obtained and then spliced with a newline character
}
dataString += '\n'
}
const blob = new Blob([dataString], { type: 'application/vnd.ms-excel' })
const time = Date.now() // Add a timestamp to the table name to distinguish
const filename = ' Student information sheet ' + time + '.xls'
fileSaver.saveAs(blob, filename)
}
</script>The core of this method is that each row of data +'\n' Spliced with the header into a string , And then use it Blob analysis , The use of file-saver Bag saveAs Method is saved as excel;
Blob Represents a large object of binary type , It can be seen as a container for storing binary data , Besides , You can also use Blob Set the of binary data MIME type ;
var blob = new Blob(dataArr:Array<any>, opt:{type:string}); // structure Blob function dataArray Represents an array , Contains information to add to Blob Data in object ,opt Represents an object , Used for setting up Blob Object properties ( Such as :MIME type )
created Blob Object contains two properties :size and type,size Represents the size of the data ( In bytes ),type yes MIME String of type ;
Two Pure front-end implementation ②
Or use Blob Realization , Use dynamic creation a How to label
<script>
// Derived data
exportData() {
const dataArray = this.$refs.multipleTableData.selection
console.log(dataArray, 'dataArray')
let dataString = ` full name \t Age \t The province where it is located \n` // Header text
for (let i = 0; i < dataArray.length; i++) {
for (const key in dataArray[i]) {
dataString += `${dataArray[i][key] + '\t'}` // Each row of data is obtained and then spliced with a newline character
}
dataString += '\n'
}
const blob = new Blob([dataString], { type: 'application/vnd.ms-excel' })
const time = Date.now() // Add a timestamp to the table name to distinguish
const filename = ' Student information sheet ' + time + '.xls'
const link = document.createElement('a') // establish a label
link.download = filename // file name
link.style.display = 'none'
link.href = URL.createObjectURL(blob) // establish URL object
document.body.appendChild(link) // stay body Add a label
link.click()
URL.revokeObjectURL(link.href) // Active release URL object
document.body.removeChild(link) // remove a label
}
</script>among ,URL.createObjectURL() Static methods create a DOMString, It contains a that represents the object given in the parameter URL, This URL Life cycle and create it in the window of document binding . This new URL Object represents the specified File Object or Blob object , Used to specify the source object The content of ;
On each call createObjectURL() When the method is used , Will create a new one URL object , Even if you've created the same object as a parameter . When it's no longer needed URL Object time , Every object must be called by URL.revokeObjectURL() Method to release . The browser document When uninstalling , They are automatically released , But for best performance and memory usage , They should be released at a safe time .
3、 ... and Front and rear end fit
If there are a lot of exported data or the data can only be exported after processing , This method is more suitable for ;
Define the format of data and files from the back end , Return to the front end with a link , The front end can be exported by clicking the link , Use a Labels or window.loacation.href that will do ;
window.location.href = 'url' // or
<a href='url'></a>
边栏推荐
- Get method of fetch request and data of formdata type submitted by post
- Preliminary sequencing problem
- How to make distribution box label
- Downloading sqlserver versions (2016-2019)
- 嵌入式软件测试工具TPT18更新全解析
- Firewall and IP security policy configuration
- 【贪心】leetcode991. Broken Calculator
- 【LeetCode】两数之和II
- C. Differential Sorting
- Which insurance company is the most cost-effective for purchasing serious illness insurance?
猜你喜欢

嵌入式软件测试工具TPT18更新全解析

【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe

innodb_ruby 视角下 MySQL 记录增删改

Encryption related to returnee of national market supervision public service platform

第一批00后下场求职:不要误读他们的“不一样”

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现

Detailed discussion on modular architecture design of MCU firmware

【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行

直接插入排序

LRU cache
随机推荐
Enterprise official website applet building tutorial
Quickly understand the development status of secondary nodes of industrial Internet identity analysis
JS Part 4
Initialize MySQL Gorm through yaml file
Email authentication bypass
Golang resource embedding scheme
[JS reverse hundreds of cases] the login of a HN service network is reverse, and the verification code is null and void
【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
January 17, 2022: word rule II. Give you a pattern and a character
Brief introduction to arm architecture
Gx3001q UI instructions for upgrading 8-bit color to 16 bit color
Encryption related to returnee of national market supervision public service platform
Generate PDF417 code in batch through TXT file
Configuring multi cluster management using kubectl
Troubleshooting and resolution of asydss virtual live broadcast status synchronization and service downtime
Know res.send() and res.end() of Express
Simply use the pagoda to build WordPress
Detailed discussion on modular architecture design of MCU firmware
Customization of openfeign
mysql常用指令