使用apache poi 实现创建excel文件并写入数据
依赖
<dependencies>
<!-- hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version> 5.8.16</version>
</dependency>
<!-- poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version> 4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version> 4.1.2</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
<!-- slf4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
实现
package org.example;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.example.util.ExcelUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* apache poi 创建基础excel 简单例子
*/
@Slf4j
public class Demo03 {
public static void main(String[] args) {
try {
/* ********* 1、创建路径和设置文件名 ********** */
//获取项目根路径
String rootPath = System.getProperty("user.dir") + "/shell";
//文件名称,为了保证文件名不重复,给文件名加时间戳
String fileName = "Demo03_" + DateUtil.format(new Date(), "yyyyMMdd_HHmmss") + ExcelUtil.XLS;
//指向根目录
File shell = new File(rootPath);
//如果改目录不存在,则创建
if (!shell.exists()) {
//创建目录
boolean mkdirs = shell.mkdirs();
//判断是否创建成功
if (!mkdirs) {
log.error("失败,创建目录:" + shell.getPath());
throw new IllegalStateException("【异常信息】创建目录失败,目录:" + shell.getPath());
}
log.info("成功,创建目录:" + shell.getPath());
}
/* ********* 2、创建表对象 ********** */
//创建一个表格对象
XSSFWorkbook workbook = new XSSFWorkbook();
//创建工作表
XSSFSheet sheet = workbook.createSheet(fileName);
/* ********* 3、给表对象写入数据 ********** */
//表头信息
String[] title = {"城市名", "地理代码", "经纬度"};
//插入表头信息
//插入第一行的表头
//创建一行,第0行
XSSFRow row = sheet.createRow(0);
for (int k = 0; k < title.length; k++) {
//创建一列
XSSFCell cell = row.createCell(k);
//填入数据
cell.setCellValue(title[k]);
}
//数据
List<Map<String, Map<String, Object>>> data = getData();
//插入数据信息,因为第0行为表头,因此从1开始
for (int i = 0; i < data.size(); i++) {
//城市名数据
String name = data.get(i).get("properties").get("name").toString();
//地理代码数据
String adCode = data.get(i).get("properties").get("adcode").toString();
//经纬度数据
List center = (List) data.get(i).get("properties").get("center");
if (StrUtil.isEmpty(name)){
log.warn("空数据:"+i+"===>"+data.get(i).get("properties"));
continue;
}
//创建一行
XSSFRow nrow = sheet.createRow(i + 1);
//填数据
//城市名
XSSFCell ncell = nrow.createCell(0);
ncell.setCellValue(name);
//地理代码
ncell = nrow.createCell(1);
ncell.setCellValue(adCode);
//经纬度
ncell = nrow.createCell(2);
ncell.setCellValue(StrUtil.join(",",center));
}
/* ********* 4、创建excel表格 ********** */
//创建表格文件
File file = new File(shell, fileName);
//输出到指定位置
FileOutputStream outputStream = new FileOutputStream(file);
//创建文件
workbook.write(outputStream);
//刷新流
outputStream.flush();
//关闭流
outputStream.close();
log.info("成功,创建文件:" + file.getPath());
} catch (IOException e) {
log.error(e.getMessage());
}
}
/**
* 获取数据
*
* @return {List}
*/
public static List<Map<String, Map<String, Object>>> getData() {
//这个是阿里提供的中国地理数据API,网址:https://datav.aliyun.com/portal/school/atlas/area_selector 。
String s = HttpUtil.get("https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json", 3000);//http请求,超时时间是3秒
//解析json,当然如果封装实体类存储是最好的,这里主要是我懒就不创建实体类了。
JSONObject entries = JSONUtil.parseObj(s);
//通过分析json获取直接需要的数据
List<Map<String, Map<String, Object>>> features = entries.get("features", ArrayList.class);
return features;
}
}
效果
项目地址
https://gitee.com/lucky-deer/demo-poi.git
在这里
可以直接运行测试,参考学习。