POI导入导出excle

java代码: 

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;


/**
 *@ClassName ExcelUtiles
 *@Description TODO
 *@Author 杨帅军
 *@Date 2018/10/22 10:56
 *@Version 1.0
 **/
public class ExcelUtils {
    /**
     * 导出Excel方法
     * @param list
     * @param title
     * @param sheetName
     * @param pojoClass
     * @param fileName
     * @param isCreateHeader
     * @param response
     */
    public static void exportExcel(List list, String title, String sheetName, Class pojoClass,
                                   String fileName, boolean isCreateHeader, HttpServletResponse response, HttpServletRequest request){
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, request, exportParams);
    }

    private static void defaultExport(List list, Class pojoClass, String fileName,
                                      HttpServletResponse response, HttpServletRequest request, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
        if (workbook != null); downLoadExcel(fileName, response, request, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, HttpServletRequest request, Workbook workbook) {
        try {
            String agent = request.getHeader("USER-AGENT").toLowerCase();
            response.setContentType("application/vnd.ms-excel");
            String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            //处理firefox中文名乱码
            if (agent.contains("firefox")) {
                response.setCharacterEncoding("utf-8");
                response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls");
            } else {
                response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
            }
            workbook.write(response.getOutputStream());

        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * @Descript 公用的easypoi导入方法
     * @param file
     * @param titleRows 标题行
     * @param HeadRows  头儿行
     * @param StartRows 开始行
     * @param startSheetIndex   sheet开始行
     * @param sheetNum sheet数目
     * @param pojoClass
     * @param 
     * @return
     */
    public static  List importExcel(MultipartFile file, Integer titleRows,Integer HeadRows,Integer StartRows,Integer startSheetIndex,Integer sheetNum,Class pojoClass){
        if (file == null){
            List list = new ArrayList();
            list.add(0,"空文件");
            return list;
        }else {
            ImportParams params = new ImportParams();
            params.setStartSheetIndex(startSheetIndex);
            params.setSheetNum(sheetNum);
            params.setHeadRows(HeadRows);
            params.setTitleRows(titleRows);
            params.setStartRows(StartRows);
            params.setNeedVerfiy(true);           // 需要验证

            List list = null;
            try {
                list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
            } catch (NoSuchElementException e) {
                throw new RuntimeException(e.getMessage());
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
            return list;
        }
    }
}

 

导出示例:

/**
 * 导出excle
 *@Auther 
 * 
 * @return
 */
@ResponseBody
@RequestMapping("/exportExcelExt")
public void exportExcelExt( HttpServletResponse response, HttpServletRequest request) {
    try {
       
        //1、查询数据
        List extractedExpertList = expExtractService.getExtractedExpertsByCondition(projectIdsList, expertIdsList, expertStatus);

        //2\导出excle
        ExcelUtils.exportExcel(extractedExpertList, null, "导出excle,shell名称", ExportExcelExperts.class, "导出excle文件名", true, response,request);

    }catch (Exception e){
        logger.error("导出失败,错误信息:", e);
    }
}

 

 

 

你可能感兴趣的:(Spring,Boot,Jpa)