RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
excel导出的方法有哪些

这篇文章主要介绍“excel导出的方法有哪些”,在日常操作中,相信很多人在excel导出的方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”excel导出的方法有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的白城网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

1、前端 JS导出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" }, //关键代码
                     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、服务端代码

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> 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);

        // 一次性写出内容,使用默认样式,强制输出标题
        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依赖

        
        
            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
        

到此,关于“excel导出的方法有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


文章标题:excel导出的方法有哪些
标题来源:http://scyingshan.cn/article/gjdpec.html