前端页面Excel下载超链接实现教程 - 使用Thymeleaf和Spring Boot
单个Excel文件下载
以下示例展示如何下载名为example.xlsx的Excel文件:
<a th:href='@{/static/excel/example.xlsx}' download>下载Excel文件</a>
其中,@{/static/excel/example.xlsx}表示Excel文件在静态资源目录下的路径。在Spring Boot中,需要将Excel文件放在src/main/resources/static/excel/目录下。
多个Excel文件下载
如果页面上有多个Excel文件需要下载,可以使用Thymeleaf的循环指令来动态生成超链接列表:
<ul>
<li th:each='file : ${excelFiles}'>
<a th:href='@{/static/excel/__${file}__}' download th:text='${file}'></a>
</li>
</ul>
其中,${excelFiles}是一个包含所有Excel文件名的集合,使用th:each指令遍历集合,生成多个超链接。在th:href表达式中,使用双下划线包裹文件名,保证Thymeleaf能正确解析,同时在th:text中设置超链接文本为文件名。
请注意,在使用Thymeleaf时,需要引入必要的依赖项,并配置Thymeleaf的模板解析器。
原文地址: https://www.cveoy.top/t/topic/ohlp 著作权归作者所有。请勿转载和采集!