postman导出excel文件(postman批量发送请求)

POI 工具类,Excel的快速导入导出,Excel模板导出,Word模板导出,可以仅仅5行代码就可以完成Excel的导入导出,修改导出格式简单粗暴,快速有效,easypoi值得你尝试

目前来说,Easypoi确实方便,官网也提供了三种不同的版本,它在开源中国还,还是非常出名的,用的人非常多,也是对他的一个认可。

postman导出excel文件(postman批量发送请求)(1)

小编目前的项目,也是用这个来做,今天我们来做个excel的导入导出例子,看看怎么使用?

包体引入

目前官方提供最新版本是4.2.0,但是我在使用过程中,总是报错,时间关系就没怎么去查找,有兴趣的同学可以呀研究一下,类找不到,这个是apache的一个类,估计是新版本需要引入别的包,没去仔细追究。

java.lang.NoClassDefFoundError: org/apache/poi/xssf/usermodel/XSSFWorkbook

        <!-- springboot核心web -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <!-- 引入EasyPoi包 -->        <dependency>            <groupId>cn.afterturn</groupId>            <artifactId>easypoi-spring-boot-starter</artifactId>            <version>4.1.0</version>        </dependency>                <dependency>            <groupId>commons-fileupload</groupId>            <artifactId>commons-fileupload</artifactId>            <version>1.4</version>        </dependency>

编写导入导出工具类

/** * Excel枚举类型 * @author:溪云阁 * @date:2020年5月29日 */public enum ExcelTypeEnum {    XLS("xls"), XLSX("xlsx");    private String value;    private ExcelTypeEnum(String value) {        this.value = value;    }    public String getValue() {        return value;    }    public void setValue(String value) {        this.value = value;    }}

/** * Excel导出工具类 * @author:溪云阁 * @date:2020年5月29日 */@Componentpublic class ExcelExportUtils {    @Autowired    private HttpServletResponse response;    /**     * 导出excel     * @author 溪云阁     * @param list 泛型数据     * @param title 标题     * @param sheetName sheet的名称     * @param pojoClass 需要导出的对象     * @param fileName 文件名称     * @param isCreateHeader 是否创建表头     * @throws IOException void     */    public void exportExcel(List<?> list, Class<?> pojoClass, String title, String sheetName, String fileName,            boolean isCreateHeader) throws IOException {        final ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);        exportParams.setCreateHeadRows(isCreateHeader);        baseExport(list, pojoClass, fileName, exportParams);    }    /**     * 导出excel     * @author 溪云阁     * @param list 泛型数据     * @param title 标题     * @param sheetName sheet的名称     * @param pojoClass 需要导出的对象     * @param fileName 文件名称     * @param response     * @throws IOException void     */    public void exportExcel(List<?> list, Class<?> pojoClass, String title, String sheetName, String fileName)            throws IOException {        baseExport(list, pojoClass, fileName, new ExportParams(title, sheetName, ExcelType.XSSF));    }    /**     * 导出excel     * @author 溪云阁     * @param list 泛型数据     * @param pojoClass 需要导出的对象     * @param fileName 文件名称     * @param exportParams 文件书香     * @param response     * @throws IOException void     */    public void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams)            throws IOException {        baseExport(list, pojoClass, fileName, exportParams);    }    /**     * 多个sheet导出     * @author 溪云阁     * @param list     * @param fileName     * @throws IOException void     */    public void exportExcel(List<Map<String, Object>> list, String fileName) throws IOException {        baseExport(list, fileName);    }    /**     * 最基础的对象导出     * @author 溪云阁     * @param list 数据列表     * @param pojoClass 导出对象     * @param fileName 文件名称     * @param exportParams 导出文件属性     * @throws IOException void     */    private void baseExport(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams)            throws IOException {        final Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);        downLoadExcel(fileName, workbook);    }    /**     * 最基础的多sheet导出     * @author 溪云阁     * @param list 多个不同数据对象的列表     * @param fileName 文件名称     * @throws IOException void     */    private void baseExport(List<Map<String, Object>> list, String fileName) throws IOException {        final Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);        downLoadExcel(fileName, workbook);    }    /**     * 文件下载     * @author 溪云阁     * @param fileName 文件名称     * @param workbook exce对象     * @throws IOException void     */    private void downLoadExcel(String fileName, Workbook workbook) throws IOException {        ServletOutputStream output = null;        try {            final String downloadName = URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8");            response.setCharacterEncoding("UTF-8");            response.setHeader("content-Type", "application/vnd.ms-excel");            response.setHeader("Content-Disposition", "attachment;filename=" + downloadName);            output = response.getOutputStream();            workbook.write(output);        }        catch (final Exception e) {            throw new IOException(e.getMessage());        }        finally {            if (output != null) {                output.flush();                output.close();            }        }    }}

/** * Excel导入工具类 * @author:溪云阁 * @date:2020年5月29日 */@Componentpublic class ExcelImportUtils {    /**     * 从指定位置获取文件后进行导入     * @author 溪云阁     * @param filePath 文件路径     * @param titleRows 表格标题行数,默认0     * @param headerRows 表头行数,默认1     * @param pojoClass 上传后需要转化的对象     * @return     * @throws IOException List<T>     */    public <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass)            throws Exception {        if (Strings.isEmpty(filePath)) {            return null;        } else {            final ImportParams params = new ImportParams();            // 表格标题行数,默认0            params.setTitleRows(titleRows);            // 表头行数,默认1            params.setHeadRows(headerRows);            // 是否需要保存上传的Excel            params.setNeedSave(true);            // 保存上传的Excel目录            params.setSaveUrl("/excel/");            return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);        }    }    /**     * 上传文件导入     * @author 溪云阁     * @param file     * @param titleRows 标题行     * @param headerRows 表头行     * @param needVerfiy 是否检验excel内容     * @param pojoClass 导入的对象     * @return     * @throws Exception List<T>     */    public <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, boolean needVerfiy,            Class<T> pojoClass) throws Exception {        if (file == null) {            return null;        } else {            return baseImport(file.getInputStream(), titleRows, headerRows, needVerfiy, pojoClass);        }    }    /**     * 最基础导入     * @author 溪云阁     * @param inputStream     * @param titleRows 表格标题行数,默认0     * @param headerRows 表头行数,默认1     * @param needVerify 是否需要检测excel     * @param pojoClass 导入的对象     * @return     * @throws IOException List<T>     */    private <T> List<T> baseImport(InputStream inputStream, Integer titleRows, Integer headerRows,            boolean needVerify, Class<T> pojoClass) throws Exception {        if (inputStream == null) {            return null;        } else {            final ImportParams params = new ImportParams();            params.setTitleRows(titleRows);            params.setHeadRows(headerRows);            params.setSaveUrl("/excel/");            params.setNeedSave(true);            params.setNeedVerify(needVerify);            return ExcelImportUtil.importExcel(inputStream, pojoClass, params);        }    }}

编写导入导出对象

这里,为了覆盖更全一点,我分别用了不同的类型来做实验,数字类型采用NumberFormat进行格式化操作。

/** * 用户信息 * @author:溪云阁 * @date:2020年5月29日 */public class User implements Serializable {    // 数字格式化    private NumberFormat nf = NumberFormat.getNumberInstance();    private static final long serialVersionUID = 1L;    @Excel(name = "用户id", orderNum = "0", width = 15)    @Setter    @Getter    private long userId;    @Excel(name = "性别", orderNum = "1", width = 15, replace = { "男_1", "女_2" }, suffix = "孩")    @Setter    @Getter    private int sex;    @Excel(name = "金钱", orderNum = "2", width = 15)    @Setter    private double money;    public String getMoney() {        return nf.format(money);    }    @Excel(name = "用户信息", orderNum = "3", width = 15)    @Setter    @Getter    private String userName;    @Excel(name = "价格", orderNum = "4", width = 15)    @Setter    @Getter    private float price;    @Excel(name = "时间", orderNum = "5", width = 15, format = "yyyy-MM-dd")    @Setter    @Getter    private Date now;}

编写测试方法

/** * excel导入导出 * @author:溪云阁 * @date:2020年5月29日 */@Api(tags = { "APP服务:数据接口" })@RestController@RequestMapping("view/ie")public class ImportExportController {    @Autowired    private ExcelExportUtils excelExportUtils;    @Autowired    private ExcelImportUtils excelImportUtils;    /**     * 导出用户信息     * @author 溪云阁 void     */    @ApiOperation(value = "导出excel")    @GetMapping(value = "/exportExcel")    public void exportExcel() throws Exception {        final List<User> userList = new ArrayList<>();        for (int i = 0; i < 10; i++) {            final User user = new User();            user.setUserId(i);            user.setSex(1);            user.setMoney(12332123 + i);            user.setUserName("小明" + i);            user.setPrice(23.1f + i);            user.setNow(new Date());            userList.add(user);        }        excelExportUtils.exportExcel(userList, User.class, "用户信息", "员工信息的sheet", "用户信息表");    }    /**     * 导入用户信息     * @author 溪云阁     * @param file     * @return     * @throws IOException Object     */    @ApiOperation(value = "导入excel")    @GetMapping(value = "/importExcel")    public ResponseMsg<List<User>> importExcel(@RequestParam("file") MultipartFile file) throws Exception {        final List<User> userList = excelImportUtils.importExcel(file, 1, 1, false, User.class);        return MsgUtils.buildSuccessMsg(userList);    }}

导出结果

在导出中,直接在浏览器输入地址接口,结果如截图所示

其中,金钱,时间上,我们分别进行了格式化

postman导出excel文件(postman批量发送请求)(2)

导入结果

把刚刚导出来的文件,直接导入进去,这里采用postMan进行操作,其中要注意的点,我已经用红色的圈圈标出来。

从实验结果上看,已经可以导入进去,并且把数据返回来

postman导出excel文件(postman批量发送请求)(3)

问题

在导入进去的构成中,这里留下一个问题给同学自行解决,导入的金额是0,可自行研究解决

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页