梦见你的名字 发表于 2024-7-27 04:33:50

Apache POI-Excel入门与实战

目次
一、了解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

https://i-blog.csdnimg.cn/direct/2380c8531dcf4ac187a43e3b394fb4a2.png
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 依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency> 二、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
} https://i-blog.csdnimg.cn/direct/bb2deaa4625c4ec69cd00fdb4fc4db69.png
2.2 读取文件

/**
* 读取 Excel 文件
*/
publicstaticvoid 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();
} https://i-blog.csdnimg.cn/direct/fc394c9ebc724c5fadcdcedb1d62ae4a.png
四、Apache POI实战

   1.通过读取Excel方法获取一个都会对应的地址编号
2.通过写入Excel方法将一个都会未来的气候环境写入Excel表格中
4.1 创建一个获取气候的API

这里使用高德舆图的气候API
创建应用,获取需要的key
https://i-blog.csdnimg.cn/direct/7476e7375a1847b59eb549f5fb3afc43.png
https://i-blog.csdnimg.cn/direct/d1fc1f0fa85d49a287851b6d8e03b122.png
https://i-blog.csdnimg.cn/direct/a63fa437fc8a4e4083e9507e162cafa6.png
4.2高德气候请求API与响应效果

请求API
https://i-blog.csdnimg.cn/direct/f69834b6e3bb446eb1d01bd9b21db8b2.png
由于这里的气候需要用到都会编码表,所以先下载好对应的都会编码表格
下载好后内容如下
https://i-blog.csdnimg.cn/direct/dbfa5e06da7c441cba7514fd9c2765e2.png
 返回的Response
https://i-blog.csdnimg.cn/direct/7269bf2f61db4a2e91609cf8041e2794.png
https://i-blog.csdnimg.cn/direct/88fda5ba50a34e01af77e86226fb6e94.png
APIFOX工具(也可以用postman) 测试:
API地址:https://restapi.amap.com/v3/weather/weatherInfo?parameters
https://i-blog.csdnimg.cn/direct/0a8a01e5bfef48c1bae2d51d9c9c2952.png
https://i-blog.csdnimg.cn/direct/cfb9987dc28042d29b254209866a46c4.png
成功返回了数据说明前面一切操纵都是对的。而后端访问则通过HttpClient形式访问
4.2 后端访问API获取数据

引入依赖
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.76</version>
</dependency>
   有了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();
        }

} https://i-blog.csdnimg.cn/direct/2d4448c81da64522bb60e8b8a3e59da8.png
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<sheetAt.getLastRowNum();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);
                        }
                }

        } https://i-blog.csdnimg.cn/direct/d6e53597b9ad4ad898f1ebc8642608aa.png
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<String, Object> map = JSON.parseObject(jsonString, new TypeReference<Map<String, Object>>() {});

                                // 获取casts列表
                                List<Map<String, Object>> casts = (List<Map<String, Object>>) ((List<Map<String, Object>>) 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<String, Object> 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();
        } https://i-blog.csdnimg.cn/direct/225848fb06a644d4885429ba0156c8b4.png


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Apache POI-Excel入门与实战