惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
小众软件
小众软件
量子位
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Jina AI
Jina AI
T
Threat Research - Cisco Blogs
博客园_首页
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Security Latest
Security Latest
博客园 - 叶小钗
The Last Watchdog
The Last Watchdog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
L
Lohrmann on Cybersecurity
V
V2EX
P
Proofpoint News Feed
I
Intezer
云风的 BLOG
云风的 BLOG
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Help Net Security
T
Tor Project blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
L
LINUX DO - 热门话题
D
DataBreaches.Net
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
P
Proofpoint News Feed
NISL@THU
NISL@THU
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
P
Palo Alto Networks Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Security @ Cisco Blogs

皮皮赖's Blog

Script @php think service:discover handling the post-autoload-dump event returned with error code 255 Centos7 网卡型号 网卡硬件型号 网卡芯片型号支持列表 - 皮皮赖's Blog uniapp IOS15、16 uni.scanCode 扫码后页面跳转卡死 - 皮皮赖's Blog PHP导出数据至Excel PHPExcel库使用方法 例子 示例 - 皮皮赖's Blog golang 实现 SD-WAN IWAN 例子 简单例子 示例 宝塔面板邮局管理器Postfix无法启动,重装后不能正常运行Postfix修复 - 皮皮赖's Blog idea goland phpstorm pycharm IntelliJ 打开项目后,只显示文件,项目中的目录不显示 《掌上 WeGame》宣布退市,9月8日正式停运 - 皮皮赖's Blog 常见内网IP段,不跟公网冲突的内网IP段有那些? - 皮皮赖's Blog
Javascript导出数据至Excel JS导出数据至Excel - 皮皮赖's Blog
博主: Reaper · 2023-02-20 · via 皮皮赖's Blog

要将数据从JavaScript导出到Excel,您可以使用JavaScript库或原生JavaScript。以下是使用原生JavaScript的简单示例:

<!-- 创建一个按钮并添加单击事件 -->
<button onclick="exportToExcel()">导出到Excel</button>

<script>
function exportToExcel() {
  // 获取要导出的数据
  const data = [
    ['姓名', '年龄', '性别'],
    ['张三', '25', '男'],
    ['李四', '30', '女'],
    ['王五', '28', '男']
  ];

  // 创建一个新的Excel文档
  const wb = new ExcelJS.Workbook();
  const ws = wb.addWorksheet('Sheet1');

  // 将数据添加到工作表中
  data.forEach((row) => {
    ws.addRow(row);
  });

  // 将工作表保存为Excel文件
  wb.xlsx.writeBuffer().then((buffer) => {
    const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = 'data.xlsx';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
  });
}
</script>

在此示例中,我们使用了ExcelJS库来创建Excel文件。首先,我们创建一个包含数据的数组。然后,我们创建一个新的工作簿并将数据添加到名为“Sheet1”的工作表中。最后,我们将工作表保存为Excel文件并将其提供给用户进行下载。

赞赏作者

您的赞赏是对我最大的支持。