当前位置:网站首页>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>
边栏推荐
- 面试第二家公司的面试题及答案(二)
- Learning about tensorflow (II)
- Tower of Hanoi II | tower of Hanoi 4 columns
- Use of pclint in vs2013
- Learning about tensor (III)
- [Halcon vision] array
- 30分钟彻底弄懂 synchronized 锁升级过程
- Data communication foundation telnet remote management equipment
- About automatic operation on Web pages
- [C language] named type and anonymous type
猜你喜欢
The charm of SQL optimization! From 30248s to 0.001s
Introduction to latex, EPS picture bounding box
Review of database -- 1. Overview
Data communication foundation TCPIP reference model
Cause: couldn‘t make a guess for 解决方法
Distributed network communication framework: how to publish local services into RPC services
【Halcon视觉】形态学腐蚀
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
Data communication foundation STP principle
抓包工具fiddler和wireshark对比
随机推荐
Learning about tensor (III)
Okaleido ecological core equity Oka, all in fusion mining mode
3.1 leetcode daily question 6
如何写一篇百万阅读量的文章
Review of database -- 1. Overview
Review of database -- 3. SQL language
SQL优化的魅力!从 30248s 到 0.001s
Learning about opencv (4)
SPARK中 DS V2 push down(下推)的一些说明
【Halcon视觉】图像灰度变化
详细解析js中的混合方式构造对象(构造加属性,原型加方法)
PTA class a 1001
Redis realizes distributed lock and gets a watchdog
MLX90640 红外热成像仪测温传感器模块开发笔记(六)
[gossip] error loading psychopg2 module: no module named psychopg2
Like, "new programmer" e-book is free for a limited time!
The CLOB field cannot be converted when querying Damon database
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
equals与==的区别
【Halcon视觉】图像滤波