温馨提示×

maven中poi的使用方法是什么

小亿
109
2024-03-01 11:45:13
栏目: 编程语言

在 Maven 中使用 Apache POI,需要在项目的 pom.xml 文件中添加以下依赖:

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

然后可以在项目中使用 Apache POI 提供的 API 来操作 Excel 文件,例如:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelWriter {

    public static void main(String[] args) {
        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("Sheet1");
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue("Hello, World!");

            try (FileOutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
                workbook.write(fileOut);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这段代码演示了如何创建一个 Excel 文件,并在第一个单元格中写入内容。您可以根据需要使用 Apache POI 的各种类和方法来操作 Excel 文件。

0