Table of contents
1. Download the third-party dependency package:
2. Check whether the download is successful:
3. Introduce the page js that needs to be used
4. Write export table function
6. Add id to the original table
How to include custom .d.ts files into TypeScript compilation process?
Demand background:
The project needs to realize the export of the table table data of the page as an excel file
Project environment:
This function is implemented for Vue3+Element Plus based on the project environment
final effect:
Click the button to download directly to the local
Implementation:
1. Download the third-party dependency package:
pnpm download command:
pnpm install --save xlsx file-saver
npm download command:
npm install --save xlsx file-saver
2. Check whether the download is successful:
The terminal displays:
and can be seen in the package.json file:
"file-saver": "^2.0.5", "xlsx": "^0.18.5"
Indicates that the download was successful
3. Introduce the page js that needs to be used
import FileSaver from 'file-saver' import * as XLSX from 'xlsx';
4. Write export table function
function exportClick() { const wb = XLSX.utils.table_to_book(document.querySelector('#my-table'))// 关联dom节点 /* get binary string as output */ const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array', }) try { FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream', }), '心理测试成绩.xlsx') } catch (e) { // eslint-disable-next-line max-statements-per-line, no-console if (typeof console !== 'undefined') { console.log(e, wbout) } } return wbout }
5. Add export button label
<el-button type="warning" plain @click="exportClick "> 导出表格 </el-button>
6. Add id to the original table
<el-table id="my-table" :data="filterTableData" style="width: 100%;">
Problems that may arise:
- import FileSaver from 'file-saver'报红
- The red location is 'file-saver'
- The error message shows: The declaration file for the module "file-saver" cannot be found.
Solution:
custom.d.ts
Create a new file called (or another name of your choosing) in the root of your project .- Add the following to that file:
declare module 'file-saver';
- Include this
.d.ts
file in your TypeScript compilation process.Doing so will manually create a module declaration that tells the TypeScript compiler what to do with
file-saver
the module's types.How to include custom
.d.ts
files in TypeScript compilation process?
Open the file in the project
tsconfig.json
. If there is no such file, please create a newtsconfig.json
one in the project root directory.In
tsconfig.json
the file, add or modifyinclude
the property, making sure it includes the path or file where your.d.ts
file is located:{ "compilerOptions": { // 其他选项... }, "include": [ "src", // 示例,包含您的源代码目录 "custom.d.ts" // 添加您的自定义.d.ts文件 ] }
Note that the path and file name need to be adjusted according to the actual situation.
Save and close
tsconfig.json
the file.
Reference article: Import and export excel files based on Element Plus tables_elementplus file export excel_Latte drink pudding blog-CSDN blog