Java使用Apache POI

Java使用Apache POI

Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)

记录时间:
时间星期
202371111:27星期二

鹤酒的空间

准备工作

加载pom.xml依赖,这里的业务是要使用excel
<!--apache POI-->
        <!--xls-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.1.0</version>
        </dependency>

        <!--xlsx-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.1.0</version>
        </dependency>
原有代码备份
package com.hejiu45.hejiu45_api_test.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hejiu45.hejiu45_api_test.controller.utils.R;
import com.hejiu45.hejiu45_api_test.ec.kzec.KzEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author hejiu45
 * @date 2023/7/6
 * @apiNote
 */

@RestController
@Slf4j
@RequestMapping("/test")
public class TestMongoCRUDController {
    @Autowired
    MongoTemplate mongoTemplate;

    private KzEntity kzEntity;

    @GetMapping
    public void getAllData() throws FileNotFoundException {
//    public R getAllData() throws FileNotFoundException {
        List<JSONObject> jsondata = mongoTemplate.findAll(JSONObject.class, "test");
//
        List<KzEntity> kzEntities = JSON.parseArray(JSON.toJSONString(jsondata), KzEntity.class);
        Object parse = JSON.parse(JSON.toJSONString(jsondata));


        KzEntity kzEntity = JSONObject.parseObject(JSONObject.toJSONString(jsondata), KzEntity.class);

        String excelFilePath = "E:\\银狐\\大屏综合管理平台\\test.xlsx";
        EasyExcel.write(new FileOutputStream(excelFilePath)).sheet("sheet1").doWrite((Collection<?>) parse);

        for (JSONObject jsonObject: jsondata) {
//            System.out.println(jsonObject);
//            jsonObject.toJavaObject()
            kzEntity = JSON.toJavaObject(jsonObject, KzEntity.class);
            System.out.println(kzEntity.toString());
        }

        EasyExcel.write(new FileOutputStream(excelFilePath)).sheet("sheet1").doWrite((Collection<?>) kzEntity);

        System.out.println(kzEntities.toString());
    }
}

基本固定逻辑
以下代码是写的操作
package com.hejiu45.hejiu45_api_test.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hejiu45.hejiu45_api_test.controller.utils.R;
import com.hejiu45.hejiu45_api_test.ec.kzec.KzEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

/**
 * @author hejiu45
 * @date 2023/7/6
 * @apiNote
 */

@RestController
@Slf4j
@RequestMapping("/test")
public class TestMongoCRUDController {
    //        创建工作簿 03
    Workbook workbo = new HSSFWorkbook();

    //        创建工作簿 07
    Workbook workboa = new XSSFWorkbook();

    //        创建工作表
    Sheet sheet1 = workbo.createSheet("sheet1");
    Sheet sheet2 = workboa.createSheet("sheet1");


    /**
     * apache POI的xls文件的写出方法
     * @throws IOException
     */
    @GetMapping("03")
    public void getAllData() throws IOException {
//        apache poi是根据坐标来读写数据的也就是所谓的行和列
//        创建第一行
        Row row = sheet1.createRow(0);
//        创建第一列第一个单元格
        Cell cell = row.createCell(0);
//        添加第一个单元格内容
        cell.setCellValue("疆内");

//        生成一张excel表格
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\银狐\\大屏综合管理平台\\test.xls");

//        使用工作簿对象写出生成文件
        workbo.write(fileOutputStream);
//        关闭流
        fileOutputStream.close();
        workbo.close();
        System.out.println("文件生成成功!");
    }

    /**
     * apache POI的xlsx文件的写出方法
     * @throws IOException
     */
    @GetMapping("07")
    public void getAllDataB() throws IOException {
//        apache poi是根据坐标来读写数据的也就是所谓的行和列
//        创建第一行
        Row row = sheet2.createRow(0);
//        创建第一列第一个单元格
        Cell cell = row.createCell(0);
//        添加第一个单元格内容
        cell.setCellValue("疆内");

//        生成一张excel表格
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\银狐\\大屏综合管理平台\\test.xlsx");

//        使用工作簿对象写出生成文件
        workboa.write(fileOutputStream);
//        关闭流
        fileOutputStream.close();
        workboa.close();
        System.out.println("文件生成成功!");
    }
}

以下代码是读的操作:
下期在见

鹤酒前端会持续更新,精彩内容。

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值