In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the methods of excel export". In the daily operation, I believe that many people have doubts about the methods of excel export. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the methods of excel export?" Next, please follow the editor to study!
1. Frontend JS export excel
Haha $(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 "}) / / key code 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 ();} JQuery AJAX Test
2. Server code
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 11:43 on 2021-3-25 * * @ author by mays * / @ Slf4jpublic class ExcelHelper {/ * @ param response response * @ param rows rows * @ param headerAlias headerAlias * @ throws IOException IOException * / public static void excelWriter (HttpServletResponse response, / / List rows, List rows Map 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); / / write out the content at once, using the default style, force the output of the title excelWriter.write (rows, true); / / set all columns to automatic widths, regardless of merging cells SXSSFSheet sheet = (SXSSFSheet) excelWriter.getSheet (); sheet.trackAllColumnsForAutoSizing (); excelWriter.autoSizeColumnAll () / / response sets excel type 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) / / destination stream excelWriter.flush (response.getOutputStream (), true) written out; 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 ("file is empty") } else if (! (file.getOriginalFilename (). EndsWith (".xlsx") | | file.getOriginalFilename () .endsWith (".xls")) {throw new IllegalArgumentException ("please upload 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 ("less than 1000 records to be imported");} return excelReader;}}
3. Maven dependence
Org.apache.poi poi 4.1.2 org.apache.poi poi-ooxml 4.1.2 org.apache.poi poi-ooxml-schemas 4.1.2 cn.hutool Hutool-all 4.6.3 so far The study of "what are the methods of excel export" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.