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】
  • 相关阅读:
    自定义控件:瀑布流水字母
    Android ViewPager欢迎页+引导页+进入首页
    自定义控件:滑动开关按钮
    自定义控件:滑动开关按钮(自定义属性)
    关于div宽度和高度的100%设定
    html中的图片、css、js等路径加载问题
    Laravel 安装
    phpmyadmin数据库导入大小限制的修改
    linux套件安装过程中configure,make,make install的作用
    Ubuntu 12.04下PHP环境的搭建(LAMP)
  • 原文地址:https://www.cnblogs.com/liaojie970/p/9549278.html
Copyright © 2011-2022 走看看