这篇文章主要介绍“excel导出的方法有哪些”,在日常操作中,相信很多人在excel导出的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”excel导出的方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1、前端 JS导出excel
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>haha</title> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function () { jQuery.support.cors = true; $('#JQuery_AJAX_Test').click(function () { $.ajax({ type: "POST", url: "http://localhost:18067/manage/orders/export", xhrFields: { responseType: "blob" }, //关键代码 data: "{\"batchExport\":true}", contentType:"application/json", beforeSend: function(request) { request.setRequestHeader("Authorization","5640edc3-49d3-435d-909e-daea076e6890"); }, success: function(retData){ dl(retData, "abc.xlsx"); }, error: function(e) { alert(e.toString()); } }); }); }); function dl(data, fileName) { if (!data) { return } let url = window.URL.createObjectURL(new Blob([data])) let link = document.createElement('a'); link.style.display = 'none'; link.href = url; link.setAttribute('download', fileName); document.body.appendChild(link); link.click(); } </script> </head> <body> <a href="#" id="JQuery_AJAX_Test">JQuery AJAX Test</a><br/> <div id="result"></div> </body> </html>
2、服务端代码
import cn.hutool.core.util.URLUtil; import cn.hutool.poi.excel.ExcelReader; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; import java.util.Objects; /** * Description ExcelHelper * Date 2021/3/25 11:43 * * @author by mays */ @Slf4j public class ExcelHelper { /** * * @param response response * @param rows rows * @param headerAlias headerAlias * @throws IOException IOException */ public static void excelWriter(HttpServletResponse response, //List<Map<String, Object>> rows, List<Object> rows, Map<String, String> headerAlias) throws IOException { String fileName = URLUtil.encode(String.format("tmp-%s.xlsx", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")))); ExcelWriter excelWriter = ExcelUtil.getBigWriter(); excelWriter.setHeaderAlias(headerAlias); // 一次性写出内容,使用默认样式,强制输出标题 excelWriter.write(rows, true); // 设置所有列为自动宽度,不考虑合并单元格 SXSSFSheet sheet = (SXSSFSheet) excelWriter.getSheet(); sheet.trackAllColumnsForAutoSizing(); excelWriter.autoSizeColumnAll(); //response设置excel类型 response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.addHeader("Cache-Control", "no-cache"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); //写出到的目标流 excelWriter.flush(response.getOutputStream(), true); excelWriter.close(); } /** * * @param file file * @return ExcelReader * @throws IOException IOException */ public static ExcelReader getExcelReader(MultipartFile file) throws IOException { if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) { throw new IllegalArgumentException("文件为空"); } else if (!(file.getOriginalFilename().endsWith(".xlsx") || file.getOriginalFilename().endsWith(".xls"))) { throw new IllegalArgumentException("请上传excel"); } File f = File.createTempFile("pwo-", file.getOriginalFilename()); file.transferTo(f); ExcelReader excelReader = new ExcelReader(f, 0); int rowCount = excelReader.getRowCount(); if (rowCount < 1) { throw new IllegalArgumentException("内容为空"); } else if (rowCount > 1000) { throw new IllegalArgumentException("须导入少于1000条的记录"); } return excelReader; } }
3、maven依赖
<!--poi excel about--> <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> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>4.1.2</version> </dependency> <!-- hutool --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency>
到此,关于“excel导出的方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。