栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Easypoi上传下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Easypoi上传下载

Easypoi参考文档:http://easypoi.mydoc.io/
实现easypoi上传和下载步骤
第一步:原有的项目里pom导入依赖包

            
            
                org.apache.poi
                poi
                4.1.0
                
                    
                        commons-codec
                        commons-codec
                    
                
            
            
                org.apache.poi
                poi-ooxml
                4.1.0
                
                    
                        poi
                        org.apache.poi
                    
                    
                        xmlbeans
                        org.apache.xmlbeans
                    
                    
                        poi-ooxml-schemas
                        org.apache.poi
                    
                
            
            
                org.apache.poi
                poi-ooxml-schemas
                4.1.2
            

第二步:创建EasyPoiUtil工具类

package com.example.demo1.util;

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 cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.exception.excel.ExcelExportException;
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class EasyPoiUtil {

    
    public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) {
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);

    }

    
    public static void exportExcel(List list, String title, String sheetName, Class pojoClass, String fileName, HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=GBK");
        try {
            response.setHeader("content-disposition",
                    "attachment;filename=" + java.net.URLEncoder.encode(fileName, "GBK")
                            + ";filename*=GBK''" + java.net.URLEncoder.encode(fileName, "GBK"));
        } catch (UnsupportedEncodingException e) {
            // ...
        }
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }

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

    
    public static void exportExcels(List workbooks, List fileNames, String fileName, HttpServletResponse response) {
        try {
            response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".zip");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            ZipOutputStream zipOut = new ZipOutputStream(toClient);
            for (int i = 0; i < workbooks.size(); i++) {
                ZipEntry entry = new ZipEntry(fileNames.get(i) + ".xls");
                zipOut.putNextEntry(entry);
                workbooks.get(i).write(zipOut);
            }
            zipOut.flush();
            zipOut.close();
        } catch (IOException e) {
            throw new ExcelExportException(e.getMessage());
        }
    }

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

    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 ExcelImportException(e.getMessage());
        }
    }

    private static void defaultExport(List> list, String fileName, HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null) {
            ;
        }
        downLoadExcel(fileName, response, workbook);
    }

    public static  List importExcel(String filePath, Integer titleRows, Integer headerRows, Class pojoClass) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new ExcelImportException("模板不能为空");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExcelImportException(e.getMessage());
        }
        return list;
    }

    public static  List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass) {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new ExcelImportException("excel文件不能为空");
        } catch (Exception e) {
            throw new ExcelImportException(e.getMessage());
        }
        return list;
    }
}

第三步:用controller试用一下

 @ApiOperation(value = "导入", notes = "导入")
    @PostMapping(value = "import", produces = MediaType.APPLICATION_JSON_VALUE)
    public ServiceResponse add1(@RequestParam MultipartFile file) {
     List messages = EasyPoiUtil.importExcel(file,1,1,UsersMessage.class);
     return ServiceResponse.ofSuccess(service.saveBatch(messages));
    }

    @ApiOperation("导出下载模板")
    @GetMapping("export")
    public void exportDeviceTemplate(HttpServletResponse response) {
        List list = new ArrayList<>();
        UsersMessage u = new UsersMessage();
        u.setUserid(1);
        list.add(u);
        EasyPoiUtil.exportExcel(list, "标题名", "表格名",UsersMessage.class,"导入模板名",response);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877556.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号