最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Java編寫一個實用的ExcelUtil工具類

 更新時間:2024年04月09日 08:22:04   作者:小城邊  
在項目中經(jīng)常遇到excel表格導入導出功能,每次都要重復寫有關(guān)excel 的邏輯,所以本文直接使用Java編寫一個實用的ExcelUtil工具類,希望對大家有所幫助

場景:在項目中經(jīng)常遇到excel表格導入導出功能,每次都要重復寫有關(guān)excel 的邏輯,在網(wǎng)上找了一圈沒有找到自己想要的,于是我就自己創(chuàng)建一個工具類,實現(xiàn)了簡單的導入和導出功能。

一、首先引入 Maven 依賴

<!-- java poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<!--支持xlsx讀取-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.17</version>
</dependency>

二、創(chuàng)建 ExcelUtil 工具類

創(chuàng)建了工具類,我們要實現(xiàn)的有導入導出兩個主要功能

實現(xiàn)導入功能

導入功能實現(xiàn)要考慮幾點:

  • Excel表格中的每一列如何和Java實體類中的屬性關(guān)聯(lián)。
  • 表頭占幾行是否需要去掉。
  • 多個 sheet 頁是否都要導入。

我的代碼如下:

/**
     *
     * @param file 導入的excel 文件
     * @param excelRowToTitleMap excel 的表頭對應關(guān)系, key 為第幾列(從 1 開始), value 為 列名
     * @param excelTitleToPojoMap 列名對應java 中實體類屬性名
     * @param sheetList 要導入的 sheetList 的列表,不傳默認全部的 sheet 頁
     * @param startRow 從第幾行開始導入數(shù)據(jù)
     * @param clazz list 列表中的元素類型
     */
    public static <T> List<T> importExcelByFile(MultipartFile file,
                                              Map<Integer, String> excelRowToTitleMap,
                                              Map<String, String> excelTitleToPojoMap,
                                              List<String> sheetList, Integer startRow, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        try (InputStream fis = file.getInputStream()){
            list = importExcel(fis, excelRowToTitleMap, excelTitleToPojoMap, sheetList, startRow, clazz);
        } catch (Exception e) {
            log.info("import excel exception:{}", e.getMessage());
        }
        return list;
    }

    /**
     *
     * @param excelRowToTitleMap excel 的表頭對應關(guān)系, key 為第幾列(從 1 開始), value 為 列名
     * @param excelTitleToPojoMap 列名對應java 中實體類屬性名
     * @param sheetList 要導入的 sheetList 的列表,不傳默認全部的 sheet 頁
     * @param startRow 從第幾行開始導入數(shù)據(jù)
     * @param clazz list 列表中的元素類型
     */
    public static <T> List<T> importExcel(InputStream inputStream,
                                        Map<Integer, String> excelRowToTitleMap,
                                        Map<String, String> excelTitleToPojoMap,
                                        List<String> sheetList, Integer startRow, Class<T> clazz) {
        Set<Integer> rows = excelRowToTitleMap.keySet();
        List<T> list = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(inputStream)) {
            for (Sheet sheet : workbook) {
                String sheetName = sheet.getSheetName();
                // 如果指定了要導入的 sheet 頁,就需要過濾其他的
                if (sheetList != null && !sheetList.contains(sheetName)) {
                    continue;
                }
                for (Row row : sheet) {
                    // 去掉表頭
                    if (row.getRowNum() + 1 < startRow) {
                        continue;
                    }
                    // 反射獲取對象
                    T obj = clazz.newInstance();
                    for (int i = 0; i < row.getLastCellNum(); i++) {
                        Cell cell = row.getCell(i);
                        String cellValue = cell != null ? cell.toString() : null;
                        if (rows.contains(i + 1)) {
                            String rowTitleName = excelRowToTitleMap.get(i + 1);
                            String propertyName = excelTitleToPojoMap.get(rowTitleName);
                            if (propertyName != null) {
                                // 反射獲取字段的set方法
                                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, clazz);
                                Method writeMethod = propertyDescriptor.getWriteMethod();
                                if (writeMethod != null) {
                                    Class<?> propertyType = propertyDescriptor.getPropertyType();
                                    Object value = convertValue(cellValue, propertyType);
                                    writeMethod.invoke(obj, value);
                                }
                            }
                        }
                    }
                    list.add(obj);
                }
            }
        } catch (Exception e) {
            log.info("import excel exception:{}", e.getMessage());
        }
        return list;
    }

上面代碼中excelRowToTitleMap和excelTitleToPojoMap解決了Excel表格中的每一列如何和Java實體類中的屬性關(guān)聯(lián)關(guān)系。sheetList 列出了需要導入的 sheet 頁,startRow 則是要從第幾行讀取數(shù)據(jù)。這樣上面的幾個問題就都解決了。

實現(xiàn)導出功能

項目中的常見的就是 list 導出到 excel。

同樣的要考慮幾個問題:

  • excel 表頭可以自定義中文名。
  • 可以指定導出哪些字段。

我的代碼如下:

/**
     * 將 list 導入 excel
     * @param list list 數(shù)據(jù)列表
     * @param obj list 的實體對象
     * @param header 表頭數(shù)組  (為 null 時,實體類中屬性名作為表頭)
     * @param rows 導出的列  (為 null 導出所有列)
     * @param fileName 文件名稱
     * @param response response
     */
    public static void exportListToExcel(List<?> list, Class obj, String[] header, String[] rows, String fileName, HttpServletResponse response) {
        try {
            //1.創(chuàng)建工作簿
            Workbook book = new XSSFWorkbook();

            //2.創(chuàng)建 sheet 頁
            Sheet sheet = book.createSheet();

            //獲取 get 方法
            List<Method> methods = new LinkedList<>();
            if (rows == null || rows.length == 0) {
                //獲取實體類中所有列 get 方法
                BeanInfo beanInfo = Introspector.getBeanInfo(obj);
                MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
                for (int i = 0; i < methodDescriptors.length; i++) {
                    String method = methodDescriptors[i].getName();
                    if (Pattern.matches("[g][e][t]\\w*",method) && !"getClass".equals(method)) {
                        methods.add(methodDescriptors[i].getMethod());
                    }
                }
            } else {
                //獲取指定列的 get 方法
                for (int i = 0; i < rows.length; i++) {
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(rows[i], obj);
                    methods.add(propertyDescriptor.getReadMethod());
                }
            }
            if (header == null || header.length == 0) {
                header = new String[methods.size()];
                for (int i = 0; i < header.length; i++) {
                    String methodName = methods.get(i).getName();
                    header[i] = methodName.substring(3,4).toLowerCase() + methodName.substring(4);
                }
            }


            //3.創(chuàng)建表頭
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = getHeaderStyle(book);
            setHeaderRow(header, headerRow, headerStyle);

            //4.填充數(shù)據(jù)
            int sheetFlag = 0;
            if (!CollectionUtils.isEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    //超過最大行數(shù),自動創(chuàng)建下一頁
                    if ((i+1)%1048575 == 0) {
                        sheet = book.createSheet("sheet" + sheetFlag);
                        headerRow = sheet.createRow(0);
                        setHeaderRow(header, headerRow, headerStyle);
                        sheetFlag ++;
                    }
                    //getLastRowNum 獲取行標(表頭行標為 0)
                    Row row = sheet.createRow(sheet.getLastRowNum() + 1);
                    Object o = list.get(i);
                    for (int j = 0; j < methods.size(); j++) {
                        Object value = methods.get(j).invoke(o);
                        row.createCell(j).setCellValue(value != null ? value.toString() : "-");
                    }
                }
            }

            //5.將 excel 表格導出
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes(StandardCharsets.UTF_8),"ISO8859-1") + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();
            book.write(outputStream);
            outputStream.close();

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

我通過header和rows兩個參數(shù)可以將excel 列表和實體類中的屬性關(guān)聯(lián)起來(兩個數(shù)組中的元素位置要一一對應),然后通過反射獲取到list列表元素寫入到excel 中。

至此基礎(chǔ)的導入和導出功能就ok了

完整的代碼

package com.base.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Pattern;

@Slf4j
public class ExcelUtil {

    /**
     *
     * @param file 導入的excel 文件
     * @param excelRowToTitleMap excel 的表頭對應關(guān)系, key 為第幾列(從 1 開始), value 為 列名
     * @param excelTitleToPojoMap 列名對應java 中實體類屬性名
     * @param sheetList 要導入的 sheetList 的列表,不傳默認全部的 sheet 頁
     * @param startRow 從第幾行開始導入數(shù)據(jù)
     * @param clazz list 列表中的元素類型
     */
    public static <T> List<T> importExcelByFile(MultipartFile file,
                                              Map<Integer, String> excelRowToTitleMap,
                                              Map<String, String> excelTitleToPojoMap,
                                              List<String> sheetList, Integer startRow, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        try (InputStream fis = file.getInputStream()){
            list = importExcel(fis, excelRowToTitleMap, excelTitleToPojoMap, sheetList, startRow, clazz);
        } catch (Exception e) {
            log.info("import excel exception:{}", e.getMessage());
        }
        return list;
    }

    /**
     *
     * @param excelRowToTitleMap excel 的表頭對應關(guān)系, key 為第幾列(從 1 開始), value 為 列名
     * @param excelTitleToPojoMap 列名對應java 中實體類屬性名
     * @param sheetList 要導入的 sheetList 的列表,不傳默認全部的 sheet 頁
     * @param startRow 從第幾行開始導入數(shù)據(jù)
     * @param clazz list 列表中的元素類型
     */
    public static <T> List<T> importExcel(InputStream inputStream,
                                        Map<Integer, String> excelRowToTitleMap,
                                        Map<String, String> excelTitleToPojoMap,
                                        List<String> sheetList, Integer startRow, Class<T> clazz) {
        Set<Integer> rows = excelRowToTitleMap.keySet();
        List<T> list = new ArrayList<>();
        try (Workbook workbook = WorkbookFactory.create(inputStream)) {
            for (Sheet sheet : workbook) {
                String sheetName = sheet.getSheetName();
                // 如果指定了要導入的 sheet 頁,就需要過濾其他的
                if (sheetList != null && !sheetList.contains(sheetName)) {
                    continue;
                }
                for (Row row : sheet) {
                    // 去掉表頭
                    if (row.getRowNum() + 1 < startRow) {
                        continue;
                    }
                    // 反射獲取對象
                    T obj = clazz.newInstance();
                    for (int i = 0; i < row.getLastCellNum(); i++) {
                        Cell cell = row.getCell(i);
                        String cellValue = cell != null ? cell.toString() : null;
                        if (rows.contains(i + 1)) {
                            String rowTitleName = excelRowToTitleMap.get(i + 1);
                            String propertyName = excelTitleToPojoMap.get(rowTitleName);
                            if (propertyName != null) {
                                // 反射獲取字段的set方法
                                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, clazz);
                                Method writeMethod = propertyDescriptor.getWriteMethod();
                                if (writeMethod != null) {
                                    Class<?> propertyType = propertyDescriptor.getPropertyType();
                                    Object value = convertValue(cellValue, propertyType);
                                    writeMethod.invoke(obj, value);
                                }
                            }
                        }
                    }
                    list.add(obj);
                }
            }
        } catch (Exception e) {
            log.info("import excel exception:{}", e.getMessage());
        }
        return list;
    }


    /**
     * 將 list 導入 excel
     * @param list list 數(shù)據(jù)列表
     * @param obj list 的實體對象
     * @param header 表頭數(shù)組  (為 null 時,實體類中屬性名作為表頭)
     * @param rows 導出的列  (為 null 導出所有列)
     * @param fileName 文件名稱
     * @param response response
     */
    public static void exportListToExcel(List<?> list, Class obj, String[] header, String[] rows, String fileName, HttpServletResponse response) {
        try {
            //1.創(chuàng)建工作簿
            Workbook book = new XSSFWorkbook();

            //2.創(chuàng)建 sheet 頁
            Sheet sheet = book.createSheet();

            //獲取 get 方法
            List<Method> methods = new LinkedList<>();
            if (rows == null || rows.length == 0) {
                //獲取實體類中所有列 get 方法
                BeanInfo beanInfo = Introspector.getBeanInfo(obj);
                MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
                for (int i = 0; i < methodDescriptors.length; i++) {
                    String method = methodDescriptors[i].getName();
                    if (Pattern.matches("[g][e][t]\\w*",method) && !"getClass".equals(method)) {
                        methods.add(methodDescriptors[i].getMethod());
                    }
                }
            } else {
                //獲取指定列的 get 方法
                for (int i = 0; i < rows.length; i++) {
                    PropertyDescriptor propertyDescriptor = new PropertyDescriptor(rows[i], obj);
                    methods.add(propertyDescriptor.getReadMethod());
                }
            }
            if (header == null || header.length == 0) {
                header = new String[methods.size()];
                for (int i = 0; i < header.length; i++) {
                    String methodName = methods.get(i).getName();
                    header[i] = methodName.substring(3,4).toLowerCase() + methodName.substring(4);
                }
            }


            //3.創(chuàng)建表頭
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = getHeaderStyle(book);
            setHeaderRow(header, headerRow, headerStyle);

            //4.填充數(shù)據(jù)
            int sheetFlag = 0;
            if (!CollectionUtils.isEmpty(list)) {
                for (int i = 0; i < list.size(); i++) {
                    //超過最大行數(shù),自動創(chuàng)建下一頁
                    if ((i+1)%1048575 == 0) {
                        sheet = book.createSheet("sheet" + sheetFlag);
                        headerRow = sheet.createRow(0);
                        setHeaderRow(header, headerRow, headerStyle);
                        sheetFlag ++;
                    }
                    //getLastRowNum 獲取行標(表頭行標為 0)
                    Row row = sheet.createRow(sheet.getLastRowNum() + 1);
                    Object o = list.get(i);
                    for (int j = 0; j < methods.size(); j++) {
                        Object value = methods.get(j).invoke(o);
                        row.createCell(j).setCellValue(value != null ? value.toString() : "-");
                    }
                }
            }

            //5.將 excel 表格導出
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes(StandardCharsets.UTF_8),"ISO8859-1") + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();
            book.write(outputStream);
            outputStream.close();

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

    private static Object convertValue(String cellValue, Class<?> targetType) {
        if (cellValue == null || targetType == null) {
            return null;
        }
        if (String.class.equals(targetType)) {
            return cellValue;
        } else if (Integer.class.equals(targetType) || int.class.equals(targetType)) {
            return Integer.valueOf(cellValue);
        } else if (Double.class.equals(targetType) || double.class.equals(targetType)) {
            return Double.valueOf(cellValue);
        } else if (Boolean.class.equals(targetType) || boolean.class.equals(targetType)) {
            return Boolean.valueOf(cellValue);
        }
        // 可以根據(jù)需要添加其他類型的轉(zhuǎn)換
        return cellValue;
    }


    /**
     * 設(shè)置表頭
     * @param header
     * @param headerRow
     * @param headerStyle
     */
    private static void setHeaderRow(String[] header, Row headerRow, CellStyle headerStyle) {
        for (int i = 0; i < header.length; i++) {
            Cell headerCell = headerRow.createCell(i);
            headerCell.setCellValue(header[i]);
            headerCell.setCellStyle(headerStyle);
        }
    }

    /**
     * 表頭樣式
     * @param book
     */
    private static CellStyle getHeaderStyle(Workbook book) {
        CellStyle headerStyle = book.createCellStyle();
        Font headerFont = book.createFont();
        headerFont.setBold(true); //設(shè)置粗體
        headerFont.setFontName("表頭加粗字體");
        headerStyle.setFont(headerFont);
        return headerStyle;
    }
}

以上就是基于Java編寫一個實用的ExcelUtil工具類的詳細內(nèi)容,更多關(guān)于Java ExcelUtil工具類的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 淺談Java生命周期管理機制

    淺談Java生命周期管理機制

    最近有位細心的朋友在閱讀筆者的文章時,對java類的生命周期問題有一些疑惑,筆者打開百度搜了一下相關(guān)的問題,看到網(wǎng)上的資料很少有把這個問題講明白的,主要是因為目前國內(nèi)java方面的教材大多只是告訴你“怎樣做”,但至于“為什么這樣做”卻不多說
    2016-01-01
  • Java關(guān)于BeabUtils.copyproperties的用法

    Java關(guān)于BeabUtils.copyproperties的用法

    這篇文章主要介紹了Java關(guān)于BeabUtils.copyproperties的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解java 中Spring jsonp 跨域請求的實例

    詳解java 中Spring jsonp 跨域請求的實例

    這篇文章主要介紹了詳解java 中Spring jsonp 跨域請求的實例的相關(guān)資料,jsonp 可用于解決主流瀏覽器的跨域數(shù)據(jù)訪問的問題,需要的朋友可以參考下
    2017-08-08
  • Hibernate原理及應用

    Hibernate原理及應用

    本文主要介紹了Hibernate原理及應用。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • Java入門異常處理最佳實踐

    Java入門異常處理最佳實踐

    本文詳細介紹了Java異常處理的核心概念、體系結(jié)構(gòu)、處理方式、執(zhí)行流程,并提供了實現(xiàn)自定義異常的方法和開發(fā)中的最佳實踐,感興趣的朋友跟隨小編一起看看吧
    2026-03-03
  • 解決mybatis中order by排序無效問題

    解決mybatis中order by排序無效問題

    這篇文章主要介紹了解決mybatis中order by排序無效問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Java搭配Selenium實現(xiàn)網(wǎng)頁訪問與自動截圖的實戰(zhàn)指南

    Java搭配Selenium實現(xiàn)網(wǎng)頁訪問與自動截圖的實戰(zhàn)指南

    將Java與Selenium相結(jié)合,不僅可以實現(xiàn)高效的網(wǎng)頁自動化操作,還能通過自動截圖功能,為測試和數(shù)據(jù)采集過程提供直觀的可視化支持,下面我們就來看看具體實現(xiàn)方法吧
    2026-02-02
  • Java使用Cipher類實現(xiàn)加密的過程詳解

    Java使用Cipher類實現(xiàn)加密的過程詳解

    這篇文章主要介紹了Java使用Cipher類實現(xiàn)加密的過程詳解,Cipher類提供了加密和解密的功能,創(chuàng)建密匙主要使用SecretKeySpec、KeyGenerator和KeyPairGenerator三個類來創(chuàng)建密匙。感興趣可以了解一下
    2020-07-07
  • Mybatis-plus更新字段update_by失敗問題

    Mybatis-plus更新字段update_by失敗問題

    在遇到實體類字段更新不正確的問題時,首先復現(xiàn)問題,確定受影響的字段,使用Debug模式查看變量的實際賦值情況,通過查看執(zhí)行的SQL語句,確認SQL是否正確反映了預期的更新,如出現(xiàn)問題,可以參考Mybatis-plus官網(wǎng)的解決方案
    2024-09-09
  • 使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決

    使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決

    這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08

最新評論

德令哈市| 华阴市| 南川市| 鱼台县| 汶上县| 台湾省| 长岛县| 蓬安县| 兴化市| 临邑县| 镇康县| 沂水县| 无为县| 乡城县| 庄河市| 新竹县| 巫山县| 盐津县| 平远县| 广宗县| 永丰县| 安龙县| 永顺县| 六安市| 康乐县| 腾冲县| 邹平县| 德化县| 南和县| 横峰县| 青州市| 兴化市| 衡东县| 乌鲁木齐市| 金华市| 镇江市| 遵义市| 双城市| 湄潭县| 宜都市| 滦南县|