0
点赞
收藏
分享

微信扫一扫

java excelfile 存储到本地

龙毓七七 2023-12-18 阅读 51

Java Excel文件存储到本地

在Java中,我们经常需要处理Excel文件,包括读取、写入和存储到本地。本文将介绍如何使用Java操作Excel文件,并将其存储到本地。

Excel文件的读取和写入

在Java中,我们可以使用Apache POI库来处理Excel文件。首先,我们需要在项目中引入POI的依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖:

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

读取Excel文件

首先,我们需要创建一个Workbook对象,它代表了整个Excel文件。然后,我们可以通过调用getSheet()方法获取特定的工作表。

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

public class ExcelReader {
    public static void main(String[] args) {
        try {
            String filePath = "path/to/excel.xlsx";
            Workbook workbook = WorkbookFactory.create(new File(filePath));
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历表格数据
            for (Row row : sheet) {
                for (Cell cell : row) {
                    String cellValue = "";
                    if (cell.getCellType() == CellType.STRING) {
                        cellValue = cell.getStringCellValue();
                    } else if (cell.getCellType() == CellType.NUMERIC) {
                        cellValue = String.valueOf(cell.getNumericCellValue());
                    }
                    System.out.println(cellValue);
                }
            }

            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上述代码读取Excel文件的第一个工作表,并逐行遍历其中的单元格数据。

写入Excel文件

要将数据写入Excel文件,我们首先需要创建一个Workbook对象,并创建一个新的工作表。然后,我们可以使用createRow()方法创建一个新的行对象,并使用createCell()方法创建一个新的单元格对象。最后,我们可以使用setCellValue()方法设置单元格的值。

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

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, Excel!");

            // 保存文件
            String filePath = "path/to/new_excel.xlsx";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            fileOut.close();

            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上述代码创建一个新的Excel文件,并在第一个工作表的第一个单元格中写入了"Hello, Excel!"。

存储Excel文件到本地

要将Excel文件存储到本地,我们只需要使用FileOutputStreamWorkbook对象写入文件即可。

import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;

public class ExcelStorage {
    public static void main(String[] args) {
        try {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Sheet1");

            // 写入数据

            // 保存文件
            String filePath = "path/to/excel.xlsx";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            fileOut.close();

            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

上述代码将Excel文件存储到本地的path/to/excel.xlsx路径下。

总结

本文介绍了如何使用Java操作Excel文件,并将其存储到本地。我们使用了Apache POI库来读取和写入Excel文件,通过WorkbookSheetRowCell等对象进行操作。通过本文的示例代码,你可以更好地理解如何在Java中处理Excel文件,并将其存储到本地。

序列图

下面是读取Excel文件的序列图:

sequenceDiagram
    participant JavaApp
    participant ExcelFile
    participant Workbook
    participant Sheet
    participant Row
    participant Cell

    JavaApp->>ExcelFile: 创建ExcelFile对象
    ExcelFile->>Workbook: 创建Workbook对象
    Workbook->>Sheet: 获取工作表
    Sheet->>Row: 遍历
举报

相关推荐

0 条评论