springboot2入门到实战 -整合POI框架

本文介绍了ApachePOI,一个Java库,用于读写MicrosoftOffice格式文件,尤其是在Excel中的应用。文章详细讲解了如何使用POI进行数据写入和读取,并展示了在SpringBoot项目中整合的步骤。
摘要由CSDN通过智能技术生成

poi

Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。是目前项目中较流行的导入导出excel框架。我们可以在其基础上做二次开发适用于项目业务。大家也可以使用阿里的easyexcel快速完成poi框架的功能。

image-20231124161320699

POI与excel的映射基础

大家参考excel的基本存储视图,我们用poi何其对应。

以添加一行数据为例,excel涉及以下操作:

  • 创建一个excel文档 : 以.xlxs结尾(excel格式,早期版本为 .xlx) --对应图(1)
  • 创建一个sheet表单–对应图(2)
  • 创建一行数据row–对应图(3)
  • 在行中单元格CELL中添加数据–对应图(4,5)

image-20231124162541543

image-20231124162808862

springboot整合框架

引入依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>

写入数据到excel

@Test
public void writeToExcel() throws Exception {
    //1.文档
    XSSFWorkbook doc = new XSSFWorkbook();
    //2.表单
    XSSFSheet sheet = doc.createSheet("东方老人");
    //3.行
    XSSFRow row = sheet.createRow(0);
    //4.单元格
    XSSFCell cell = row.createCell(0);
    //5. 写入数据
    cell.setCellValue("理想老人");

    doc.write(new FileOutputStream("D:/orderw.xlsx"));
    doc.close();

}

读取excel中数据

包装了一个POIUitl,以读取excel中数据为例

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
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;
@Test
public void readFromExcel() throws  Exception{
    XSSFWorkbook doc = new XSSFWorkbook(new FileInputStream("D:/order.xlsx"));
    XSSFSheet sheet = doc.getSheet("东方养老院");

    int lastRow = sheet.getLastRowNum();

    for(int r=0;r<=lastRow;r++){
        XSSFRow row = sheet.getRow(r);
        int lastCell = row.getLastCellNum();
        for(int c=0; c<lastCell;c++){
            XSSFCell cell = row.getCell(c);
            //System.out.println(cell.getStringCellValue());
            CellType cellType = cell.getCellType();
         //   System.out.println(cellType);
            Older older = new Older();
            switch (cellType){
                case NUMERIC:
                    if(DateUtil.isCellDateFormatted(cell)){
                        System.out.print(cell.getDateCellValue()+"\t");
                    }else{ //数值类型
                        System.out.print(cell.getNumericCellValue()+"\t");
                    }
                    break;
                case STRING:
                    System.out.print(cell.getStringCellValue()+"\t");
                    break;
                default:
                    break;
            }
        }
        System.out.println();
    }
   // XSSFCell cell = row.getCell(1);
   // System.out.println(cell.getStringCellValue());
    doc.close();

    }
}
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个简单的Spring Boot整合POI解析Excel的示例代码: 首先,需要在pom.xml文件中添加POI的依赖: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Apache POI --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> </dependencies> ``` 接下来,创建一个Controller类来处理Excel文件的上传和解析: ```java import org.apache.poi.ss.usermodel.*; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Controller public class ExcelController { @PostMapping("/upload") public ResponseEntity<List<String>> uploadExcel(@RequestParam("file") MultipartFile file) { List<String> data = new ArrayList<>(); try { Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { data.add(cell.toString()); } } workbook.close(); } catch (IOException e) { e.printStackTrace(); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity<>(data, HttpStatus.OK); } } ``` 在这个示例中,我们使用`WorkbookFactory`来创建Workbook对象,然后获取第一个Sheet,并遍历所有的行和单元格来获取数据。最后,将解析的数据返回给客户端。 注意:这里只是一个简单的示例,实际项目中可能需要根据Excel的具体格式进行更复杂的解析逻辑。 希望以上示例能够帮助你理解如何在Spring Boot整合POI进行Excel解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值