日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
excel導出的方法有哪些

這篇文章主要介紹“excel導出的方法有哪些”,在日常操作中,相信很多人在excel導出的方法有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”excel導出的方法有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:成都網(wǎng)站設(shè)計、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的白城網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設(shè)合作伙伴!

1、前端 JS導出excel




     
     haha
	
    

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

        // 一次性寫出內(nèi)容,使用默認樣式,強制輸出標題
        excelWriter.write(rows, true);

        // 設(shè)置所有列為自動寬度,不考慮合并單元格
        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);

        //寫出到的目標流
        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("內(nèi)容為空");
        } 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
        

到此,關(guān)于“excel導出的方法有哪些”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
名稱欄目:excel導出的方法有哪些
文章來源:http://www.dlmjj.cn/article/gjdpec.html