Apache POI入门教程:安装、配置与基础操作

科技创新工坊 2019-05-11 ⋅ 188 阅读

Apache POI是一个开源的Java库,用于处理Microsoft Office文件,如Word文档、Excel表格和PowerPoint演示文稿。它提供了丰富的API,使得我们可以在程序中创建、读取和操作这些Office文件。

本教程将介绍如何安装和配置Apache POI,并演示一些基础操作的示例代码。

安装和配置Apache POI

要开始使用Apache POI,首先需要下载对应的JAR文件。你可以在Apache POI的官方网站上找到最新版本的下载链接。下载完成后,将JAR文件添加到你的Java项目中。

为了简化依赖管理,你还可以使用构建工具,如Maven或Gradle,在你的项目配置文件中添加Apache POI的依赖。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>

创建一个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) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sample Sheet");

        Row headerRow = sheet.createRow(0);
        Cell headerCell = headerRow.createCell(0);
        headerCell.setCellValue("Name");

        Row dataRow = sheet.createRow(1);
        Cell dataCell = dataRow.createCell(0);
        dataCell.setCellValue("John Doe");

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

        System.out.println("Excel file created successfully.");
    }
}

在上面的示例中,我们使用XSSFWorkbook类创建一个新的Excel文件。然后,我们创建一个名为"Sample Sheet"的工作表。创建行和单元格,然后将数据写入单元格。

最后,我们使用FileOutputStream将Workbook的内容写入一个文件。你可以使用任何你想要的文件名和路径。

读取一个Excel文件

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

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {
    public static void main(String[] args) {
        try (FileInputStream inputStream = new FileInputStream("sample.xlsx");
             Workbook workbook = new XSSFWorkbook(inputStream)) {

            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                for (Cell cell : row) {
                    String cellValue = getCellValue(cell);
                    System.out.print(cellValue + "\t");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell.getCellType() == CellType.STRING) {
            cellValue = cell.getStringCellValue();
        } else if (cell.getCellType() == CellType.NUMERIC) {
            cellValue = String.valueOf(cell.getNumericCellValue());
        } else if (cell.getCellType() == CellType.BOOLEAN) {
            cellValue = String.valueOf(cell.getBooleanCellValue());
        }
        return cellValue;
    }
}

在上面的示例中,我们使用FileInputStream将Excel文件加载到内存中。然后,我们使用XSSFWorkbook类创建一个Workbook对象,并使用getSheetAt方法获取第一个工作表。

接下来,我们使用嵌套的for循环遍历工作表中的行和单元格。在第二个for循环中,我们通过调用getCellValue方法来获取每个单元格的值。

getCellValue方法根据单元格的类型使用不同的方法来获取值。在我们的示例中,我们处理了字符串、数字和布尔值类型的单元格。

总结

本教程介绍了如何安装和配置Apache POI,并演示了如何创建和读取Excel文件的基础操作。这只是Apache POI提供的功能的冰山一角,你可以查阅官方文档来了解更多高级用法和API。希望本教程能够帮助你入门Apache POI,并在你的Java项目中处理Office文件。


全部评论: 0

    我有话说: