关于java:如何使用FTP中的模板文件和EasyPOI来导出Excle

7次阅读

共计 2947 个字符,预计需要花费 8 分钟才能阅读完成。

问题形容

因工作须要导出 Excel 文件,应用技术为 EasyPOI,EasyPOI 是一个十分好的导出文件工具,官网提供十分具体的应用文档,在我的项目中应用 EasyPOI 的模板导出性能,官网提供的示例代码中,模板的门路都是本地,我应用时也是把 Excle 模板文件放在本地,因为之前须要导出的中央,不是很多,模板文件放在本地也没有太大问题,然而因为当初需要变更,会有大量的模板须要导出,如果放在本地会造成我的项目容量变大。当初想把导出的模板保留在近程的 FTP 服务中,EasyPOI 读取 FTP 的中模板文件生成 Excle 文件。

解决步骤

1、查找解决形式
上网找了许多相干材料,官网上也没有找到解决办法,意外浏览了一篇文章,文章中提到了一句话,说 EasyPOI 读取模板文件,只反对读取本地模板文件,换句话来说,我只须要把 FTP 中的模板文件下载到本地指定门路,而后,就能够读取模板文件。

2、创立测试项目
创立一个 SpringBoot 我的项目,POM 文件中引入须要的 Jar 包,如下

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.5</version>
</dependency>

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.3.0</version>
</dependency>
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.6</version>
</dependency>

3、增加一些配置文件

ftp:
  host: 192.168.2.2  # IP
  port: 21  # 端口
  username: root # 用户名
  password: 123456 # 明码
  mode: Passive # ftp 模式
  remotePath: /root/export/ # ftp 模板门路
  localPath: /Users/simonxue/Developer/Temp/ # 本地模板门路
template:
  employee: employee.xlsx #模板文件

3、创立一个 FTP 下载办法,办法返回地址模板全路径名,如下所示

@Value("${ftp.host}")
private String host;
@Value("${ftp.port}")
private Integer port;
@Value("${ftp.username}")
private String username;
@Value("${ftp.password}")
private String password;
@Value("${ftp.mode}")
private String mode;
@Value("${ftp.remotePath}")
private String remotePath;
@Value("${ftp.localPath}")
private String localPath;

/**
    * 拷贝 FTP 中的文件到本地
    * @param fileName  ftp 中的文件名
    * @return
    */
@SneakyThrows
public String localPathName(String fileName) {Ftp ftp = new Ftp(host, port, username, password, Charset.defaultCharset());
    ftp.setMode(FtpMode.Passive.name().equals(mode)? FtpMode.Passive: FtpMode.Active);
    String localName = localPath + fileName;
    ftp.download(remotePath, fileName, FileUtil.file(localName));
    ftp.close();
    return localName;
}

4、须要依据模板导出的中央,应用下面的办法, 如下

@SneakyThrows
@Override
public void templateTest(HttpServletResponse response) {String localPathName = ftpUtil.localPathName(employeeTemplateName);
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment;filename="+ IdUtil.getSnowflake(0,0).nextIdStr()+".xlsx");
    Map<String, Object> resultMap = new HashMap<>();
    List<Employee> employeeList = new ArrayList<>();
    Employee employee = new Employee();
    employee.setJobNumber("0001");
    employee.setUsername("小码农薛尧");
    employee.setPhone("1234567901");
    employee.setEmail("xueyao.me@gmail.com");
    employeeList.add(employee);
    List<Map<String, Object>> maps = employeeList.stream().map(a -> {Map<String, Object> map = BeanUtil.beanToMap(a);
        return map;
    }).collect(Collectors.toList());
    resultMap.put("employees", maps);
    // 此处须要指定模板门路
    TemplateExportParams params = new TemplateExportParams(localPathName, "test");
    Workbook workbook = ExcelExportUtil.exportExcel(params, resultMap);
    OutputStream outputStream = response.getOutputStream();
    workbook.write(outputStream);
    outputStream.close();}

5、运行代码,生成的文件如下

总结

EasyPOI 不提供读取近程模板文件,然而咱们能够通过其它办法来实现,下次导出 Excle 有格局款式扭转,咱们能够间接调整 FTP 中的模板文件就能够实现,不必重新部署我的项目。

我的项目代码已寄存在 Github 上

链接地址

正文完
 0