All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mobaijun.excel.service.impl.ExcelServiceImpl Maven / Gradle / Ivy

/*
 * Copyright (C) 2022 [www.mobaijun.com]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mobaijun.excel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.mobaijun.excel.constant.ExcelConstants;
import com.mobaijun.excel.exception.ExcelException;
import com.mobaijun.excel.listener.ExcelDataListener;
import com.mobaijun.excel.model.ExcelExportParam;
import com.mobaijun.excel.service.ExcelService;
import jakarta.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * software:IntelliJ IDEA 2022.1
 * class name: ExcelServiceImpl
 * class description:
 *
 * @author MoBaiJun 2022/10/28 13:40
 */
public class ExcelServiceImpl implements ExcelService {

    @Override
    public  void easyExportDownload(ExcelExportParam excelExportParam) {
        if (excelExportParam == null) {
            return;
        }

        try {
            HttpServletResponse response = excelExportParam.getResponse();
            if (response == null) {
                throw new NullPointerException("HttpServletResponse information is abnormal! Please try again later!");
            }

            if (excelExportParam.getClazz() == null) {
                throw new NullPointerException("excelExportParam attempt exception! Please confirm the parameters and try again!");
            }

            // 默认值
            createDefaultValue(excelExportParam);

            ExcelTypeEnum excelTypeEnum = excelExportParam.getExcelTypeEnum();

            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            String fileName = URLEncoder.encode(excelExportParam.getFileName(), StandardCharsets.UTF_8).replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", String.format("%s%s%s", "attachment;filename=", fileName, excelTypeEnum.getValue()));

            EasyExcel.write(response.getOutputStream(), excelExportParam
                            .getClazz())
                    .excelType(excelTypeEnum)
                    .sheet(excelExportParam.getSheetName())
                    .doWrite(excelExportParam.getDataList());

        } catch (Exception e) {
            // 组装提示信息
            throw new ExcelException(ExcelConstants.OFFICE_MODULE_NAME, e);
        }

    }

    @Override
    public  void easyWriteToFile(ExcelExportParam excelExportParam) {
        // 默认值
        createDefaultValue(excelExportParam);

        ExcelTypeEnum excelTypeEnum = excelExportParam.getExcelTypeEnum();
        String excelFileWriteAbsolutePath = excelExportParam.getExcelFileWriteAbsolutePath();

        try {
            EasyExcel.write(excelFileWriteAbsolutePath, excelExportParam.getClazz()).excelType(excelTypeEnum).sheet(excelExportParam.getSheetName()).doWrite(excelExportParam.getDataList());
        } catch (Exception e) {
            // 组装提示信息
            throw new ExcelException(ExcelConstants.OFFICE_MODULE_NAME, e);
        }
    }

    @Override
    public  List easyReadToList(InputStream inputStream, Class clazz) {
        if (inputStream == null) {
            return Collections.emptyList();
        }

        // 创建一个简单的数据监听器
        ExcelDataListener readListener = new ExcelDataListener<>();

        // 读取文件
        try {
            EasyExcel.read(inputStream, clazz, readListener).sheet().doRead();
        } catch (Exception e) {
            // 组装提示信息
            throw new ExcelException(ExcelConstants.OFFICE_MODULE_NAME, e);
        }

        return readListener.getDataList();
    }

    @Override
    public  List easyReadToList(InputStream inputStream, Integer rowNum, Class clazz) {
        if (inputStream == null) {
            return Collections.emptyList();
        }

        // 创建一个简单的数据监听器
        ExcelDataListener readListener = new ExcelDataListener<>();

        // 读取文件
        try {
            EasyExcel.read(inputStream, clazz, readListener).sheet().headRowNumber(rowNum).doRead();
        } catch (Exception e) {
            // 组装提示信息
            throw new ExcelException(ExcelConstants.OFFICE_MODULE_NAME, e);
        }
        return readListener.getDataList();
    }

    /**
     * excel导出文件的默认属性
     *
     * @param param Excel导出参数
     */
    private  void createDefaultValue(ExcelExportParam param) {
        if (param.getSheetName().isEmpty()) {
            param.setSheetName(ExcelConstants.OFFICE_EXCEL_DEFAULT_SHEET_NAME);
        }

        if (param.getFileName().isEmpty()) {
            param.setFileName(ExcelConstants.OFFICE_EXCEL_EXPORT_DEFAULT_FILE_NAME);
        }

        if (param.getExcelTypeEnum() == null) {
            param.setExcelTypeEnum(ExcelTypeEnum.XLSX);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy