当前位置:网站首页>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>
边栏推荐
- 【LeetCode】两数之和II
- Tencent cloud tcapulusdb helps tmeland, the first virtual Music Carnival in China, and tens of thousands of people cross the new year together!
- Bi skills - authority control
- 【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM
- This point (II)
- Engineer culture: should the company buy genuine software
- 【LeetCode】23. 合并K个升序链表
- 1058 multiple choice questions (20 points)
- Customization of openfeign
- The power of code refactoring: how to measure the success of refactoring
猜你喜欢

【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM

嵌入式软件测试工具TPT18更新全解析
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

R tree of search tree

Detailed discussion on modular architecture design of MCU firmware

【贪心】leetcode991. Broken Calculator

【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
![Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]](/img/81/4d3cb059f700dd9243645e64023be7.jpg)
Analysis of the number of urban residents covered by basic medical insurance, their treatment and medical treatment in other places in China in 2021 [figure]

Using jhipster to build microservice architecture

Analysis on the development of China's graphene industry chain in 2021: with the support of energy conservation and environmental protection policies, the scale of graphene industry will continue to e
随机推荐
RTOS system selection for charging point software design
Compile your own openwrt firmware using Tencent cloud lightweight application server
What is the difference between comparator and comparable?
Installing serverstatus probe using pagoda
Svn local computer storage configuration
【owt】owt-client-native-p2p-e2e-test vs2017构建2 :测试单元构建及运行
How to solve the problem that easynvr cannot be cascaded to the easynvs platform? View ports first
【LeetCode】179. 最大数
JSON. Function of the stringify() optional parameter
Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo
Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)
直接插入排序
The difference between code39 and code93
How to share small programs released by wechat
新版kali切换最高账户
【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe
【LeetCode】两数之和II
Minecraft server technology explanation 𞓜 teach you how to get to the ashes from Xiaobai -- server technology explanation
Goframe framework (RK boot): enable tls/ssl
The power of code refactoring: how to measure the success of refactoring