首页 / 预测世界杯足球

Apache POI-Excel入门与实战

2025-06-20 00:56:20预测世界杯足球 2969

目录

一、了解Apache POI

1.1 什么是Apache POI

1.2 为什么要使用ApaChe POI

1.3 Apache POI应用场景

1.4 Apache POI 依赖

二、Apache POI-Excel 入门案例

2.1 写入Excel文件

2.2 读取文件

四、Apache POI实战

4.1 创建一个获取天气的API

4.2高德天气请求API与响应结果

4.2 后端访问API获取数据

4.3 通过获取地方名称获取AdCode编码

4.4 使用Apache POI包装数据并将预测结果输出到Excel表格中

一、了解Apache POI

1.1 什么是Apache POI

1.2 为什么要使用ApaChe POI

兼容性:POI能够与各种版本的Office文档进行交互,无论是在创建新文件还是在读取旧文件时,都提供了良好的兼容性。无需Office安装:使用POI可以在没有实际安装Microsoft Office软件的情况下操作Office文档,这对于服务器环境尤其重要。灵活性:POI提供了丰富的API,允许开发人员以编程方式对Office文档进行复杂的操作,如添加公式、图表、图片、样式等。性能:相比于其他一些解决方案,如使用自动化Office应用程序的方式,POI在处理大量数据时通常具有更好的性能。安全性:由于POI不需要运行任何Office应用程序,因此可以避免潜在的安全风险,比如宏病毒或恶意脚本的执行。开源:POI是一个开源项目,这意味着它是免费的,并且有一个活跃的社区支持,可以提供帮助和持续的更新。

1.3 Apache POI应用场景

1.4 Apache POI 依赖

org.apache.poi

poi

3.16

org.apache.poi

poi-ooxml

3.16

二、Apache POI-Excel 入门案例

2.1 写入Excel文件

/**

* ApachePOI 测试类

*/

public class ApachePOITest {

/**

* 读取 Excel 文件

*/

public static void write() {

// 在内存中创建一个 Excel 文件

XSSFWorkbook excel = new XSSFWorkbook();

// 创建一个工作表sheet

XSSFSheet sheet = excel.createSheet("info");

//在sheet中添加表头第1行,RowNum是从0开始的

XSSFRow row = sheet.createRow(1);

row.createCell(1).setCellValue("姓名");

row.createCell(2).setCellValue("年龄");

//创建一个新的row

XSSFRow row2 = sheet.createRow(2);

//在row中创建单元格

row2.createCell(1).setCellValue("张三");

row2.createCell(2).setCellValue("20");

//创建一个新的row

sheet.createRow(3).createCell(1).setCellValue("李四");

sheet.getRow(3).createCell(2).setCellValue("30");

//将excel写入到文件中

try {

//通过文件输出流将内存中的excel写入到文件中

FileOutputStream fileOutputStream = new FileOutputStream("E:" + File.separator + "POITest.xlsx");

excel.write(fileOutputStream);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

write();

}

//关闭流

excel.close

}

2.2 读取文件

/**

* 读取 Excel 文件

*/

public static void read() throws IOException {

//通过文件输入流读取文件

FileInputStream fileInputStream = new FileInputStream("E:" + File.separator + "POITest.xlsx");

XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);

//读取第一个工作表

//XSSFSheet sheet = excel.getSheet("info");

XSSFSheet sheetAt = excel.getSheetAt(0);

//获取最后一行的行号

int lastRowNum = sheetAt.getLastRowNum();

//从第二行开始读取数据

for (int i = 1; i <= lastRowNum; i++) {

//获取当前行

XSSFRow row = sheetAt.getRow(i);

//获取当前行的最后一个单元格的编号

short lastCellNum = row.getLastCellNum();

//遍历所有的单元格,从第二个单元格开始

for (int j = 1; j < lastCellNum; j++) {

System.out.print(row.getCell(j) + "\t");

}

System.out.println();

}

//关闭流

excel.close();

fileInputStream.close();

}

四、Apache POI实战

1.通过读取Excel方法获取一个城市对应的地址编号

2.通过写入Excel方法将一个城市未来的天气情况写入Excel表格中

4.1 创建一个获取天气的API

这里使用高德地图的天气API

创建应用,获取需要的key

4.2高德天气请求API与响应结果

请求API

由于这里的天气需要用到城市编码表,所以先下载好对应的城市编码表格

下载好后内容如下

返回的Response

APIFOX工具(也可以用postman) 测试:

API地址:https://restapi.amap.com/v3/weather/weatherInfo?parameters

成功返回了数据说明前面一切操作都是对的。而后端访问则通过HttpClient形式访问

4.2 后端访问API获取数据

引入依赖

org.apache.httpcomponents

httpclient

4.5.13

com.alibaba

fastjson

1.2.76

有了API和城市编码表格,现在我们只需要在用户输入对应的城市或者区域的时候,遍历表格进行读取,如果有对应的区则返回adcode

测试类:

@Test

void WeatherTest() throws URISyntaxException, IOException {

// TODO:这里的key需要引用自己的

String key = "xxxxxxx";

String city = "110101";

CloseableHttpClient httpClient = HttpClients.createDefault();

URIBuilder uriBuilder = new URIBuilder("https://restapi.amap.com/v3/weather/weatherInfo");

uriBuilder.addParameter("key", key);

uriBuilder.addParameter("city", city);

HttpGet httpGet = new HttpGet(uriBuilder.build());

try (CloseableHttpResponse response = httpClient.execute(httpGet)) {

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode == 200) {

HttpEntity entity = response.getEntity();

String result = EntityUtils.toString(entity, "UTF-8");

System.out.println(result);

} else {

System.out.println("Failed to retrieve data, status code: " + statusCode);

}

}

httpClient.close();

}

}

4.3 通过获取地方名称获取AdCode编码

@Test

void AdCodeQuery() throws IOException {

// TODO:这里输入下载好的地址对照表

String AMapFilePath = "E:\\AMap_adcode_citycode.xlsx";

// 需要查询的地址

String address = "北京市";

FileInputStream fileInputStream = new FileInputStream(AMapFilePath);

XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

XSSFSheet sheetAt = workbook.getSheetAt(0);

for (int i=1;i

XSSFCell cell = sheetAt.getRow(i).getCell(0);

String stringCellValue = cell.getStringCellValue();

if (stringCellValue.equals(address)){

XSSFCell cell1 = sheetAt.getRow(i).getCell(1);

String adcode = cell1.getStringCellValue();

System.out.println("查询到地区编号为"+adcode);

}

}

}

4.4 使用Apache POI包装数据并将预测结果输出到Excel表格中

@Test

void WeatherTest() throws URISyntaxException, IOException {

// TODO:这里的key需要引用自己的

String key = "xxxxxxx";

String city = "110101";

String extensions = "all";

CloseableHttpClient httpClient = HttpClients.createDefault();

URIBuilder uriBuilder = new URIBuilder("https://restapi.amap.com/v3/weather/weatherInfo");

uriBuilder.addParameter("key", key);

uriBuilder.addParameter("city", city);

uriBuilder.addParameter("extensions",extensions);

HttpGet httpGet = new HttpGet(uriBuilder.build());

try (CloseableHttpResponse response = httpClient.execute(httpGet)) {

int statusCode = response.getStatusLine().getStatusCode();

if (statusCode == 200) {

HttpEntity entity = response.getEntity();

String jsonString = EntityUtils.toString(entity, "UTF-8");

// 通过POI写入文件

// 将JSON字符串解析为Java对象

Map map = JSON.parseObject(jsonString, new TypeReference>() {});

// 获取casts列表

List> casts = (List>) ((List>) map.get("forecasts")).get(0).get("casts");

// 创建Excel工作簿

Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Weather Forecasts");

// 创建标题行

Row headerRow = sheet.createRow(0);

CellStyle headerStyle = workbook.createCellStyle();

headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());

headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

// 写入标题

int cellIndex = 0;

String[] columnHeaders = {"date", "week", "dayweather", "nightweather", "daytemp", "nighttemp", "daywind", "nightwind", "daypower", "nightpower", "daytemp_float", "nighttemp_float"};

for (String columnHeader : columnHeaders) {

Cell cell = headerRow.createCell(cellIndex++);

cell.setCellValue(columnHeader);

cell.setCellStyle(headerStyle);

}

// 创建数据行

int rowIndex = 1;

for (Map cast : casts) {

Row row = sheet.createRow(rowIndex++);

// 写入数据

cellIndex = 0;

for (String columnHeader : columnHeaders) {

Cell cell = row.createCell(cellIndex++);

cell.setCellValue(cast.get(columnHeader).toString());

}

}

// 写入Excel文件

try (FileOutputStream fileOut = new FileOutputStream("WeatherForecasts.xlsx")) {

workbook.write(fileOut);

}

// 关闭工作簿

workbook.close();

} else {

System.out.println("Failed to retrieve data, status code: " + statusCode);

}

}

httpClient.close();

}