国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出

這篇具有很好參考價值的文章主要介紹了Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

上周學(xué)習(xí)群里有人問到,多個sheet的導(dǎo)出導(dǎo)入,我第一反應(yīng)就是easypoi不是自己就有方法了么?

后面一想,可能有些看客還處于是 找工具類,然后調(diào)試 的寫代碼 的 階段,可能還不會去看jar包的一些函數(shù)。

既然如此,那就寫一寫Excel? 多sheet表 導(dǎo)入導(dǎo)出。

正文

之前的一些excel的相關(guān)的文章,感興趣的看客,也可以看下。

最簡單的入門篇:
Springboot 最簡單的結(jié)合MYSQL數(shù)據(jù)實現(xiàn)EXCEL表格導(dǎo)出及數(shù)據(jù)導(dǎo)入_小目標(biāo)青年的博客-CSDN博客

還寫過一篇單個,多個 excel文件導(dǎo)出,轉(zhuǎn)成ZIP包的:

SpringBoot 導(dǎo)出多個Excel文件,壓縮成.zip格式下載_小目標(biāo)青年的博客-CSDN博客

還有指定模板導(dǎo)出的:
Springboot 指定自定義模板導(dǎo)出Excel文件_小目標(biāo)青年的博客-CSDN博客_自定義導(dǎo)出excel

?還有這種復(fù)合表格的:
Springboot 導(dǎo)入導(dǎo)出Excel ,一對多關(guān)系,復(fù)合表格、合并單元格數(shù)據(jù)_springboot導(dǎo)出excel合并單元格_小目標(biāo)青年的博客-CSDN博客

還有這種動態(tài)導(dǎo)出的:

Springboot 我隨手封裝了一個萬能的導(dǎo)出excel工具,傳什么都能導(dǎo)出_小目標(biāo)青年的博客-CSDN博客

好了,回歸本篇,開搞。

慣例,先看下我們今天要實現(xiàn) excel多sheet表的導(dǎo)入導(dǎo)出,整個實戰(zhàn)下來,需要做多少東西:

基本等于0 ,是的,沒啥好寫的。

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi?

?

1.pom.xml 引入依賴 :

        <!--easypoi-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.0.0</version>
        </dependency>

2.yml 配置:

server:
  port: 8697

spring:
  main:
    allow-bean-definition-overriding: true

3.工具類?ExcelUtil.java? (只保留了該篇用到的函數(shù),精簡)

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import java.util.Map;
import java.util.NoSuchElementException;

public class ExcelUtil {

    protected static final Logger logger = LoggerFactory.getLogger(ExcelUtil.class);
    private static final String XLS = "xls";
    private static final String XLSX = "xlsx";
    private static final String SPLIT = ".";

    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
        defaultExport(list, fileName, response);
    }


    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null);
        downLoadExcel(fileName, response, workbook);
    }



    public static <T> List<T> importExcelMore(MultipartFile file, Class<T> pojoClass,ImportParams params){
        if (file == null){
            return null;
        }

        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        }catch (NoSuchElementException e){
            throw new RuntimeException("excel文件不能為空");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return list;
    }

    public static Workbook getWorkbook(MultipartFile file) {
        Workbook workbook=null;
        try {
            // 獲取Excel后綴名
            String fileName = file.getOriginalFilename();
            if (StringUtils.isEmpty(fileName) || fileName.lastIndexOf(SPLIT) < 0) {
                logger.warn("解析Excel失敗,因為獲取到的Excel文件名非法!");
                return null;
            }
            String fileType = fileName.substring(fileName.lastIndexOf(SPLIT) + 1, fileName.length());
            // 獲取Excel工作簿
            if (fileType.equalsIgnoreCase(XLS)) {
                workbook = new HSSFWorkbook(file.getInputStream());
            } else if (fileType.equalsIgnoreCase(XLSX)) {
                workbook = new XSSFWorkbook(file.getInputStream());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return workbook;
    }




}

4.導(dǎo)出導(dǎo)入用到的 數(shù)據(jù)類 User.java

import cn.afterturn.easypoi.excel.annotation.Excel;

public class User {
    @Excel(name = "學(xué)號", orderNum = "0")
    private Integer id;
    @Excel(name = "姓名", orderNum = "1")
    private String  userName;
    @Excel(name = "年齡", orderNum = "2")
    private String  userAge;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", userAge='" + userAge + '\'' +
                '}';
    }

    public User() {
    }

    public User(Integer id, String userName, String userAge) {
        this.id = id;
        this.userName = userName;
        this.userAge = userAge;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserAge() {
        return userAge;
    }

    public void setUserAge(String userAge) {
        this.userAge = userAge;
    }
}

5.寫個測試的controller 試一下 :
?

就2個接口,1個導(dǎo)入,1個導(dǎo)出, 都是 多sheet的(其實看懂代碼就知道,多sheet是包含單sheet的)

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi?

?

先是導(dǎo)出接口 :

?

    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) {
        List<User> userListOne = new ArrayList<>();
        User user1 = new User();
        user1.setId(1001);
        user1.setUserName("JCccc");
        user1.setUserAge("18");
        userListOne.add(user1);


        List<User> userListTwo = new ArrayList<>();
        User user2 = new User();
        user2.setId(2001);
        user2.setUserName("Mike");
        user2.setUserAge("18");
        userListTwo.add(user2);


        // 多個sheet配置參數(shù)
        final List<Map<String, Object>> sheetsList = Lists.newArrayList();

        final String sheetNameOne = "sheet1-1班";
        Map<String, Object> exportMapOne = Maps.newHashMap();
        final ExportParams exportParamsOne = new ExportParams(null, sheetNameOne, ExcelType.HSSF);
        // 以下3個參數(shù)為API中寫死的參數(shù)名 分別是sheet配置/導(dǎo)出類(注解定義)/數(shù)據(jù)集
        exportMapOne.put("title", exportParamsOne);
        exportMapOne.put("entity", User.class);
        exportMapOne.put("data", userListOne);

        final String sheetNameTwo = "sheet2-2班";
        Map<String, Object> exportMapTwo = Maps.newHashMap();
        final ExportParams exportParamsTwo = new ExportParams(null, sheetNameTwo, ExcelType.HSSF);
        // 以下3個參數(shù)為API中寫死的參數(shù)名 分別是sheet配置/導(dǎo)出類(注解定義)/數(shù)據(jù)集
        exportMapTwo.put("title", exportParamsTwo);
        exportMapTwo.put("entity", User.class);
        exportMapTwo.put("data", userListTwo);

        // 加入多sheet配置列表
        sheetsList.add(exportMapOne);
        sheetsList.add(exportMapTwo);

        //導(dǎo)出操作
        ExcelUtil.exportExcel(sheetsList, "userList.xls", response);
    }

代碼簡析:

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi?

?

調(diào)用一下接口看看效果:

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi

打開下載的excel文件,可以看到多個sheet表數(shù)據(jù)如愿出現(xiàn):
?

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi?

?Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi

?

再看下導(dǎo)入接口 :

?

    @PostMapping("importExcel")
    public void importExcel(@RequestParam("file") MultipartFile multipartFile) {
        try {
            //標(biāo)題占幾行
            Integer titleRows = 0;
            //表頭占幾行
            Integer headerRows = 1;
            Workbook workBook = ExcelUtil.getWorkbook(multipartFile);

            //獲取sheet數(shù)量
            int sheetNum = workBook.getNumberOfSheets();
            ImportParams params = new ImportParams();
            //表頭在第幾行
            params.setTitleRows(titleRows);
            params.setHeadRows(headerRows);
            for (int numSheet = 0; numSheet < sheetNum; numSheet++) {
                String sheetName = workBook.getSheetAt(numSheet).getSheetName();
                //第幾個sheet頁
                params.setStartSheetIndex(numSheet);
                List<User> result = ExcelUtil.importExcelMore(multipartFile, User.class, params);
                System.out.println("sheetNum=" + numSheet + "   sheetName=" + sheetName);
                System.out.println("導(dǎo)入的數(shù)據(jù)=" + result.toString());

            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

代碼簡析:
Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi

?導(dǎo)入的數(shù)據(jù)樣例:
Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi

?

調(diào)用下接口玩下:

Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi

?

可以看到數(shù)據(jù)都解析拿到了:
Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出,跟我一起玩轉(zhuǎn) SpringBoot,excel,spring boot,導(dǎo)入導(dǎo)出,多sheet,easypoi

?

好了,就到這吧。復(fù)制粘貼就能用。文章來源地址http://www.zghlxwxcb.cn/news/detail-574130.html

到了這里,關(guān)于Springboot Excel 最簡單的 多sheet表 導(dǎo)入導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包