zoukankan      html  css  js  c++  java
  • 使用EasyPOI导出excel示例

    package com.mtoliv.sps.controller;
     
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
     
    import javax.servlet.http.HttpServletResponse;
     
    import org.apache.commons.io.FilenameUtils;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestPart;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
     
    import com.mtoliv.sps.model.MapImportHanlder;
     
    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.params.ExcelExportEntity;
    import cn.afterturn.easypoi.excel.export.ExcelExportServer;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
     
    @RestController
    @RequestMapping(value = "/api/v1/excel")
    @Api(tags = "导入导出相关操作参考实现 ")
    public class ExcelController {
        
        private static final Logger logger = LoggerFactory.getLogger(ExcelController.class);
     
        @GetMapping(value = "/exportExcels", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        @ApiOperation(value = "导出数据")
        public void exportExcels(HttpServletResponse response) throws IOException {
     
            List<ExcelExportEntity> entityList = new ArrayList<>();
            entityList.add(new ExcelExportEntity("用户ID", "id", 15));
            entityList.add(new ExcelExportEntity("用户名", "name", 15));
            entityList.add(new ExcelExportEntity("用户年龄", "age", 15));
            List<Map<String, String>> dataResult = getData();
     
            ExcelExportServer server = new ExcelExportServer();
            Workbook workbook = new HSSFWorkbook();
     
            ExportParams exportParams = new ExportParams();
            exportParams.setSheetName("用户列表");
            server.createSheetForMap(workbook, exportParams, entityList, dataResult);
     
            response.setCharacterEncoding("UTF-8");
            String filedisplay = "users.xls";
            filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
     
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.close();
        }
        
        @PostMapping(value = "/importUsers")
        public void importUsers(@RequestPart(value = "file") MultipartFile  file) {
            
            logger.info(file.getOriginalFilename());
            
            String originalFilename = file.getOriginalFilename();
            String extension = FilenameUtils.getExtension(originalFilename);
            logger.info(extension);
            if (!"xlsx".equals(extension) && !"xls".equals(extension)) return;
            
            ImportParams params = new ImportParams();
            params.setDataHanlder(new MapImportHanlder());
            try {
                
                List<Map<String, Object>> list = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
                logger.info(list.size() + "");
            } catch (Exception e) {
                
                e.printStackTrace();
            }
            
        }
     
        private List<Map<String, String>> getData() {
     
            List<Map<String, String>> dataResult = new ArrayList<>();
            Map<String, String> u1 = new LinkedHashMap<>();
            u1.put("id", "1");
            u1.put("name", "cyf");
            u1.put("age", "21");
            Map<String, String> u2 = new LinkedHashMap<>();
            u2.put("id", "2");
            u2.put("name", "cy");
            u2.put("age", "22");
            dataResult.add(u1);
            dataResult.add(u2);
            return dataResult;
        }
     
    }
    注意:api部分:@GetMapping(value = "/api/v1/record/exportFireExcels", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)

    【转载:https://blog.csdn.net/zerobz/article/details/78962839】
  • 相关阅读:
    MFC子窗体、父窗体
    私有云计算安全问题不容忽视
    云计算更多的是一种模式而不是技术
    原型模式向量的原型
    企业发票异常分析分离进项与销项
    考试系统框架搭建
    抽象工厂模式人与肤色
    工厂方法模式加密算法
    简单工厂模式女娲造人
    企业发票异常分析导入,清洗
  • 原文地址:https://www.cnblogs.com/liaojie970/p/9549278.html
Copyright © 2011-2022 走看看