這篇文章主要介紹“excel導(dǎo)出的方法有哪些”,在日常操作中,相信很多人在excel導(dǎo)出的方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”excel導(dǎo)出的方法有哪些”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的白城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
1、前端 JS導(dǎo)出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" }, //關(guān)鍵代碼 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、服務(wù)端代碼
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); // 一次性寫出內(nèi)容,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題 excelWriter.write(rows, true); // 設(shè)置所有列為自動(dòng)寬度,不考慮合并單元格 SXSSFSheet sheet = (SXSSFSheet) excelWriter.getSheet(); sheet.trackAllColumnsForAutoSizing(); excelWriter.autoSizeColumnAll(); //response設(shè)置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); //寫出到的目標(biāo)流 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("請(qǐng)上傳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("內(nèi)容為空"); } else if (rowCount > 1000) { throw new IllegalArgumentException("須導(dǎo)入少于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>
到此,關(guān)于“excel導(dǎo)出的方法有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站欄目:excel導(dǎo)出的方法有哪些
URL地址:http://aaarwkj.com/article42/gjdpec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、動(dòng)態(tài)網(wǎng)站、商城網(wǎng)站、App設(shè)計(jì)、定制開發(fā)、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)