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

使用EasyPOI實現多sheet動態(tài)列導出

 更新時間:2025年03月10日 10:11:40   作者:高山不再高2  
這篇文章主要為大家詳細介紹了如何使用EasyPOI根據指定時間范圍創(chuàng)建動態(tài)列,以及如何將數據組織成符合要求的格式并導出,感興趣的可以了解下

POM

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-base</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-annotation</artifactId>
    <version>3.2.0</version>
</dependency>

主類

public class Demo {

    @Test
    void product() throws IOException, ParseException {
        // 這里時間是用來做動態(tài)列的
        String startTime = "2019-09-01 00:00:00";
        String endTime = "2020-03-31 23:59:59";

        // sheetName 列表
        List<String> sheetNameList = new ArrayList<>();
        sheetNameList.add("sheet1");
        sheetNameList.add("sheet2");
        sheetNameList.add("sheet3");

        // 將sheet1和sheet2使用得map進行包裝
        List<Map<String, Object>> sheetsList = new ArrayList<>();

        for (String sheetName : sheetNameList) {
            // 獲取每一個sheet列(里面有固定列,也有根據時間動態(tài)算出的列)
            List<ExcelExportEntity> excelExportEntities = getExportExcelDefine(startTime, endTime);
            // 獲取行數據(一般數據庫拿)
            List<CustomerMonthProductNum> rowsBean = new ArrayList<>();
            getOriDate(rowsBean);

            // 組裝一個sheet出來(sheet名稱,列名,數據)
            Map<String, Object> oneSheet = this.getOneSheet(sheetName, excelExportEntities, rowsBean);
            // 加入到sheet列表里面
            sheetsList.add(oneSheet);
        }
        // 導出多個sheet
        Workbook workBook = exportExcel(sheetsList);
        FileOutputStream fos = new FileOutputStream("D:/多sheet動態(tài)列.xls");
        workBook.write(fos);
        fos.close();

    }

    /**
     * 獲取原始數據(真實情況可能從數據庫里面拿)
     *
     * @param rowsBean
     */
    void getOriDate(List<CustomerMonthProductNum> rowsBean) {
        CustomerMonthProductNum productNumBean = new CustomerMonthProductNum();
        productNumBean.setCustomerName("張三");
        productNumBean.setAuthCode("121-121");
        productNumBean.setRegion("Q");
        productNumBean.setCustomerId(212312);
        productNumBean.setSum(2323);
        TreeMap<String, Integer> productNum = new TreeMap<>();
        productNum.put("2020-01", 1);
        productNum.put("2020-02", 12);
        productNum.put("2019-09", 19);
        productNumBean.setProductNum(productNum);
        rowsBean.add(productNumBean);
        rowsBean.add(productNumBean);


        CustomerMonthProductNum productNumBean1 = new CustomerMonthProductNum();
        productNumBean1.setCustomerName("張三");
        productNumBean1.setAuthCode("121-121");
        productNumBean1.setRegion("Q");
        productNumBean1.setCustomerId(212312);
        productNumBean1.setSum(2323);
        TreeMap<String, Integer> productNum1 = new TreeMap<>();
        productNum1.put("2020-01", 1);
        productNum1.put("2020-02", 12);
        productNum1.put("2019-09", 19);
        productNumBean1.setProductNum(productNum);
        rowsBean.add(productNumBean1);
        rowsBean.add(productNumBean1);

    }


    /**
     * 導出Ecel
     *
     * @return org.apache.poi.ss.usermodel.Workbook
     */
    private static Workbook exportExcel(List<Map<String, Object>> list) {
        Workbook workbook = new HSSFWorkbook();
        for (Map<String, Object> map : list) {
            MyExcelExportService service = new MyExcelExportService();
            service.createSheetWithList(workbook, (ExportParams) map.get("title"), ExportParams.class, (List<ExcelExportEntity>) map.get("entityList"), (Collection<?>) map.get("data"));
        }
        return workbook;
    }


    private Map<String, Object> getOneSheet(String sheetName, List<ExcelExportEntity> colList, List<CustomerMonthProductNum> dataList) {

        // 創(chuàng)建sheet1使用得map
        Map<String, Object> sheetExportMap = new HashMap<>();
        // Sheet1樣式
        ExportParams sheet1ExportParams = new ExportParams();
        // 設置sheet得名稱
        sheet1ExportParams.setSheetName(sheetName);

        // title的參數為ExportParams類型,目前僅僅在ExportParams中設置了sheetName
        sheetExportMap.put("title", sheet1ExportParams);
        //sheet1樣式
        sheetExportMap.put("entityList", colList);

        //sheet1中要填充得數據
        List<Map<String, String>> rows = new ArrayList<>();
        for (CustomerMonthProductNum data : dataList) {
            rows.add(this.getRowData(data));
        }

        sheetExportMap.put("data", rows);

        return sheetExportMap;
    }


    /**
     * 獲取列定義
     *
     * @param start 開始時間
     * @param end   結束時間
     */
    private List<ExcelExportEntity> getExportExcelDefine(String start, String end) throws ParseException {
//        String start = "2019-09-01 00:00:00";
//        String end = "2020-03-14 00:00:12";
        List<String> monthBetweenDates = DateUtil.getMonthBetweenDates(start, end);
        //定義表格列名,該集合存放的就是表格的列明,每個對象就是表格中的一列
        List<ExcelExportEntity> modelList = new ArrayList<>();
        //該對象就是定義列屬性的對象
        ExcelExportEntity excelentity;

        //定義第一個列
        excelentity = new ExcelExportEntity("授權名", "agentName");
        excelentity.setWidth(20);
        excelentity.setHeight(10);
        modelList.add(excelentity);

        //定義第一個列
        excelentity = new ExcelExportEntity("大區(qū)", "region");
        excelentity.setWidth(20);
        excelentity.setHeight(10);
        modelList.add(excelentity);

        //定義第一個列
        excelentity = new ExcelExportEntity("授權碼", "auth");
        excelentity.setWidth(20);
        excelentity.setHeight(10);
        modelList.add(excelentity);
        for (String monthBetweenDate : monthBetweenDates) {
            //定義第一個列
            excelentity = new ExcelExportEntity(monthBetweenDate, monthBetweenDate);
            excelentity.setWidth(10);
            excelentity.setHeight(10);
            modelList.add(excelentity);
        }

        //定義列
        excelentity = new ExcelExportEntity("合計", "sum");
        excelentity.setWidth(20);
        excelentity.setHeight(10);
        modelList.add(excelentity);
        return modelList;

    }


    /**
     * 將對象數據轉換為導出需要的map數據結構
     * <pre>
     *     map的可以對應了列定義里面的key。eg: ExcelExportEntity("授權名", "agentName");
     * </pre>
     *
     * @param productNum bean
     */
    private Map<String, String> getRowData(CustomerMonthProductNum productNum) {
        Map<String, String> data = new HashMap<>();
        data.put("agentName", productNum.getCustomerName());
        data.put("auth", productNum.getAuthCode());
        data.put("region", productNum.getRegion());
        data.put("sum", productNum.getSum().toString());
        TreeMap<String, Integer> productNum1 = productNum.getProductNum();
        for (Map.Entry<String, Integer> stringIntegerEntry : productNum1.entrySet()) {
            data.put(stringIntegerEntry.getKey(), stringIntegerEntry.getValue().toString());
        }
        return data;
    }


}

導出邏輯

import cn.afterturn.easypoi.excel.annotation.ExcelTarget;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import cn.afterturn.easypoi.exception.excel.ExcelExportException;
import cn.afterturn.easypoi.exception.excel.enums.ExcelExportEnum;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
 * 自定義下導出邏輯
 * @author huan
 * @version 2.8.2
 * @date  2019/7/5
 */
@Slf4j
public class MyExcelExportService extends ExcelExportService {
    public void createSheetWithList(Workbook workbook, ExportParams entity, Class<?> pojoClass, List<ExcelExportEntity> entityList, Collection<?> dataSet) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Excel export start ,class is {}", pojoClass);
            LOGGER.debug("Excel version is {}",
                    entity.getType().equals(ExcelType.HSSF) ? "03" : "07");
        }
        if (workbook == null || entity == null || pojoClass == null || dataSet == null) {
            throw new ExcelExportException(ExcelExportEnum.PARAMETER_ERROR);
        }
        try {
            List<ExcelExportEntity> excelParams = entityList;
            // 得到所有字段
            Field[] fileds = PoiPublicUtil.getClassFields(pojoClass);
            ExcelTarget etarget = pojoClass.getAnnotation(ExcelTarget.class);
            String targetId = etarget == null ? null : etarget.value();
            getAllExcelField(entity.getExclusions(), targetId, fileds, excelParams, pojoClass,
                    null, null);
            //獲取所有參數后,后面的邏輯判斷就一致了
            createSheetForMap(workbook, entity, excelParams, dataSet);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new ExcelExportException(ExcelExportEnum.EXPORT_ERROR, e.getCause());
        }
    }
}

時間處理類

public class DateUtil {
    /**
     * 獲取某個時間段內所有月份
     *
     * @param minDate
     * @param maxDate
     * @return
     */
    public static List<String> getMonthBetweenDates(String minDate, String maxDate) throws ParseException {
        Date d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(minDate);//定義起始日期
        Date d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(maxDate);//定義結束日期  可以去當前月也可以手動寫日期。
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化為年月
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.setTime(d1);
//        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        max.setTime(d2);
//        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
        return result;
    }
}

結果:

以上就是使用EasyPOI實現多sheet動態(tài)列導出的詳細內容,更多關于EasyPOI多sheet導出的資料請關注腳本之家其它相關文章!

相關文章

  • Java 如何從list中刪除符合條件的數據

    Java 如何從list中刪除符合條件的數據

    這篇文章主要介紹了Java 如何從list中刪除符合條件的數據,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • JavaMail實現帶附件的郵件發(fā)送

    JavaMail實現帶附件的郵件發(fā)送

    這篇文章主要為大家詳細介紹了JavaMail實現帶附件的郵件發(fā)送,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Spring Boot Thymeleaf實現國際化的方法詳解

    Spring Boot Thymeleaf實現國際化的方法詳解

    這篇文章主要給大家介紹了關于Spring Boot Thymeleaf實現國際化的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-10-10
  • 導入項目出現Java多個工程相互引用異常A cycle was detected in the build path of project的解決辦法

    導入項目出現Java多個工程相互引用異常A cycle was detected in the build path o

    今天小編就為大家分享一篇關于導入項目出現Java多個工程相互引用異常A cycle was detected in the build path of project的解決辦法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java中println輸出漢字亂碼問題一招解決方案

    Java中println輸出漢字亂碼問題一招解決方案

    這篇文章主要介紹了Java中println輸出漢字亂碼問題一招解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Security6.4.2?自定義異常中統(tǒng)一響應遇到的問題

    Security6.4.2?自定義異常中統(tǒng)一響應遇到的問題

    本文主要介紹了Security6.4.2?自定義異常中統(tǒng)一響應遇到的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-03-03
  • 深入理解Java動態(tài)代理與靜態(tài)代理

    深入理解Java動態(tài)代理與靜態(tài)代理

    這篇文章主要介紹了深入理解Java動態(tài)代理與靜態(tài)代理,靜態(tài)代理,代理類和被代理的類實現了同樣的接口,代理類同時持有被代理類的引用,動態(tài)代理的根據實現方式的不同可以分為JDK動態(tài)代理和CGlib動態(tài)代理
    2022-06-06
  • java集合框架 arrayblockingqueue應用分析

    java集合框架 arrayblockingqueue應用分析

    ArrayBlockingQueue是一個由數組支持的有界阻塞隊列。此隊列按 FIFO(先進先出)原則對元素進行排序。隊列的頭部 是在隊列中存在時間最長的元素
    2012-11-11
  • Java實現瀏覽器大文件上傳的示例詳解

    Java實現瀏覽器大文件上傳的示例詳解

    文件上傳是許多項目都有的功能,用戶上傳小文件速度一般都很快,但如果是大文件幾個g,幾十個g的時候,上傳了半天,馬上就要完成的時候,網絡波動一下,文件又要重新上傳,所以本文給大家介紹了Java實現瀏覽器大文件上傳的示例,需要的朋友可以參考下
    2024-07-07
  • Java簡易登錄注冊功能實現代碼解析

    Java簡易登錄注冊功能實現代碼解析

    這篇文章主要介紹了Java簡易登錄注冊功能實現代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論

剑阁县| 崇左市| 朝阳县| 梧州市| 莱阳市| 浙江省| 桓仁| 蓝田县| 朝阳县| 贡觉县| 汉川市| 耿马| 灵寿县| 荔浦县| 江源县| 芮城县| 内黄县| 青海省| 荣成市| 鄂托克旗| 晋宁县| 潼关县| 呼和浩特市| 柳林县| 卢氏县| 江安县| 东兰县| 天全县| 茶陵县| 比如县| 抚宁县| 英德市| 香格里拉县| 永福县| 延吉市| 安国市| 庆云县| 丰城市| 肃北| 长兴县| 威远县|