当前位置:网站首页>js下载文件,FileSaver.js导出txt、excel文件
js下载文件,FileSaver.js导出txt、excel文件
2022-07-26 10:24:00 【唐策】
源代码下载:https://download.csdn.net/download/qq_29132907/11177565
一、导出txt
新开发了一个“证书到期报警”功能,要实现一个功能是:后台将证书已字符串形式传给我,实现导出Txt文件。
我选用了FileSaver.js插件,实现导出txt文件
下载地址:https://github.com/eligrey/FileSaver.js/
用法:
第一步:引用
第二步: 导出生成文本
function downloadText(data) {
var blob = new Blob([JSON.stringify(data)], { type: “text/plain;charset=utf-8” });
saveAs(blob, “export.txt”);
}
第三步:调用
downloadText(data)
二、导出excel
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8" />
<style>
/* 此样式仅用于浏览器页面效果,Excel不会分离表格边框,不需要此样式 */
table {
border-collapse: collapse;
}
</style>
</head>
<body>
<!-- 设置border="1"以显示表格框线 -->
<table border="1">
<!-- caption元素可以生成表标题,其单元格列跨度为表格的列数 -->
<caption>学生成绩表</caption>
<tr>
<!-- 可以使用rowspan和colspan来合并单元格 -->
<th rowspan="2">编号</th>
<th rowspan="2">学号</th>
<th rowspan="2">姓名</th>
<th rowspan="2">性别</th>
<th rowspan="2">年龄</th>
<th colspan="3">成绩</th>
</tr>
<tr>
<th>语文</th>
<th>数学</th>
<th>英语</th>
</tr>
<tr>
<td>1</td>
<td>2016001</td>
<td>张三</td>
<td>男</td>
<td>13</td>
<td>85</td>
<td>94</td>
<td>77</td>
</tr>
<tr>
<td>2</td>
<td>2016002</td>
<td>李四</td>
<td>女</td>
<td>12</td>
<td>96</td>
<td>84</td>
<td>89</td>
</tr>
</table>
<a>导出表格</a>
<script>
// 使用outerHTML属性获取整个table元素的HTML代码(包括<table>标签),然后包装成一个完整的HTML文档,设置charset为urf-8以防止中文乱码
var html = "<html><head><meta charset='utf-8' /></head><body>" + document.getElementsByTagName("table")[0].outerHTML + "</body></html>";
// 实例化一个Blob对象,其构造函数的第一个参数是包含文件内容的数组,第二个参数是包含文件类型属性的对象
var blob = new Blob([html], { type: "application/vnd.ms-excel" });
var a = document.getElementsByTagName("a")[0];
// 利用URL.createObjectURL()方法为a元素生成blob URL
a.href = URL.createObjectURL(blob);
// 设置文件名
a.download = "学生成绩表.xls";
</script>
</body>
</html>
边栏推荐
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
- The fourth week of summer vacation
- Deduct daily question 838 of a certain day
- What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
- Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
- Map key not configured and uniapp routing configuration and jump are reported by the uniapp < map >< /map > component
- Flask框架初学-04-flask蓝图及代码抽离
- 3.1 leetcode daily question 6
- Getting started with SQL - combined tables
- Data communication foundation TCPIP reference model
猜你喜欢

Like, "new programmer" e-book is free for a limited time!

30 minutes to thoroughly understand the synchronized lock upgrade process

How to write a million reading article

Cause: couldn‘t make a guess for 解决方法

抓包工具fiddler和wireshark对比

数通基础-Telnet远程管理设备

如何写一篇百万阅读量的文章

Session based recommendations with recurrent neural networks

Data communication foundation STP principle
![Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da](/img/21/5dceab9815b503f0b62d26a430d7eb.png)
Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
随机推荐
Session based recommendations with recurrent neural networks
Getting started with SQL - combined tables
Android greendao数据库的使用
SQL Server 2008 server engine failed to start?
C language course design Tetris (Part 1)
Wechat official account release reminder (wechat official account template message interface)
【Halcon视觉】算子的结构
Flask framework beginner-03-template
[Qualcomm][Network] qti服务分析
我们的Web3创业项目,黄了
【Halcon视觉】图像的傅里叶变换
Structure of [Halcon vision] operator
How to write a million reading article
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
Flask框架初学-03-模板
equals与==的区别
MySQL function
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
The charm of SQL optimization! From 30248s to 0.001s
Some descriptions of DS V2 push down in spark