Spring Boot+Layui 导入Excel文件

本文介绍了如何在Spring Boot应用中结合Layui框架实现Excel文件的导入。主要内容包括在pom.xml中添加相关依赖,编写Controller层代码以处理文件上传,以及在页面上设置正确的交互方式。在实施过程中,作者分享了遇到的坑,即在Layui中直接使用标签的onclick方法导致请求失败,改为不包裹方法后问题解决。同时,作者欢迎读者在评论区提出代码改进建议,并提供QQ联系方式以提供进一步帮助。
摘要由CSDN通过智能技术生成

1、首先是pom.xml,添加依赖。

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.17</version>
		</dependency>

2、Controller层代码

@ResponseBody
@RequestMapping("/importExcel")
	public WebResult importExcel(@RequestParam("file") MultipartFile file) throws IOException {
   

		int success = 0;
		try {
   
			ExcelData data = ExcelRead.getInst().parse(file.getInputStream(), file.getOriginalFilename(), false, true);
			List<String[]> datas = data.getDatas(
实现预览 Word、Excel 文件可以通过以下步骤: 1. 在 Spring Boot 中添加 Maven 依赖: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个 Controller 类,提供文件预览的接口。 ```java @RestController public class FilePreviewController { @GetMapping("/preview") public void previewFile(HttpServletResponse response, @RequestParam(name = "filePath") String filePath) throws IOException { File file = new File(filePath); if (!file.exists()) { throw new FileNotFoundException(); } String fileName = file.getName(); String extension = FilenameUtils.getExtension(fileName).toLowerCase(); InputStream inputStream = new FileInputStream(file); response.setHeader("Content-Disposition", "inline;filename=" + fileName); response.setContentType(getContentType(extension)); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } private String getContentType(String extension) { switch (extension) { case "doc": return "application/msword"; case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; case "xls": return "application/vnd.ms-excel"; case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; default: return "application/octet-stream"; } } } ``` 3. 在 HTML 页面中调用接口,使用 Layui 的 iframe 模块显示文件预览。 ```html <div class="layui-col-md6"> <div class="layui-card"> <div class="layui-card-header">文件预览</div> <div class="layui-card-body"> <div class="layui-btn-group"> <button class="layui-btn layui-btn-warm" onclick="previewFile('doc')">预览 Word</button> <button class="layui-btn layui-btn-warm" onclick="previewFile('xls')">预览 Excel</button> </div> <div class="layui-col-md12"> <iframe id="previewIframe" frameborder="0" style="width: 100%;height: 600px;"></iframe> </div> </div> </div> </div> <script> function previewFile(extension) { let filePath = "/preview?filePath=" + encodeURIComponent("path/to/file." + extension); $("#previewIframe").attr("src", filePath); } </script> ``` 这样就可以实现 Word、Excel 文件的预览了。注意,以上示例仅供参考,具体实现方式需要根据实际情况进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值