Apache POI 时间单元

excel

1个回答

写回答

Xiaojing0617

2025-06-24 09:30

+ 关注

Java
Java

Apache POI 时间单元

Apache POI 是一个用于读写 Microsoft Office 格式文件的 Java 库。它提供了处理 word 文档、excel 表格和 PowerPoint 幻灯片的功能。其中,Apache POI 时间单元是在处理 excel 表格时非常重要的概念。

在 excel 中,时间以一种特定的单元表示。Apache POI 提供了几种不同的时间单元,以满足不同的需求。下面将介绍几种常用的时间单元及其使用方法。

日期时间单元

日期时间单元用于表示日期和时间。在 Apache POI 中,我们可以使用 Java.util.Date 或 Java.util.Calendar 类来表示日期和时间。可以使用 org.apache.poi.ss.usermodel.DateUtil 类中的方法将日期和时间转换为 excel 中的日期时间单元。

下面是一个使用日期时间单元的示例代码:

Java

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

public class DateTimeExample {

public static void mAIn(String[] args) {

Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Sheet1");

Row row = sheet.createRow(0);

// 创建一个日期时间单元格

Cell cell = row.createCell(0);

cell.setcellValue(new Java.util.Date());

// 将日期时间单元格的值格式化为日期时间字符串

CellStyle cellStyle = workbook.createCellStyle();

cellStyle.setDataFormat(workbook.getcreationHelper().createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

cell.setcellStyle(cellStyle);

// 将工作簿写入文件

try (FileOutputStream outputStream = new FileOutputStream("datetime_example.xlsx")) {

workbook.write(outputStream);

} catch (IOException e) {

e.printStackTrace();

}

}

}

以上代码创建了一个新的 excel 文件,其中包含一个日期时间单元格。日期时间单元格的值为当前日期和时间,并使用指定的日期时间格式进行格式化。最后,将工作簿写入文件 "datetime_example.xlsx"。

时间单元

时间单元用于表示时间。在 Apache POI 中,我们可以使用 Java.util.Date 或 Java.util.Calendar 类来表示时间。可以使用 org.apache.poi.ss.usermodel.DateUtil 类中的方法将时间转换为 excel 中的时间单元。

下面是一个使用时间单元的示例代码:

Java

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

public class TimeExample {

public static void mAIn(String[] args) {

Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Sheet1");

Row row = sheet.createRow(0);

// 创建一个时间单元格

Cell cell = row.createCell(0);

cell.setcellValue(new Java.util.Date());

// 将时间单元格的值格式化为时间字符串

CellStyle cellStyle = workbook.createCellStyle();

cellStyle.setDataFormat(workbook.getcreationHelper().createDataFormat().getFormat("HH:mm:ss"));

cell.setcellStyle(cellStyle);

// 将工作簿写入文件

try (FileOutputStream outputStream = new FileOutputStream("time_example.xlsx")) {

workbook.write(outputStream);

} catch (IOException e) {

e.printStackTrace();

}

}

}

以上代码创建了一个新的 excel 文件,其中包含一个时间单元格。时间单元格的值为当前时间,并使用指定的时间格式进行格式化。最后,将工作簿写入文件 "time_example.xlsx"。

日期单元

日期单元用于表示日期。在 Apache POI 中,我们可以使用 Java.util.Date 或 Java.util.Calendar 类来表示日期。可以使用 org.apache.poi.ss.usermodel.DateUtil 类中的方法将日期转换为 excel 中的日期单元。

下面是一个使用日期单元的示例代码:

Java

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

public class DateExample {

public static void mAIn(String[] args) {

Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Sheet1");

Row row = sheet.createRow(0);

// 创建一个日期单元格

Cell cell = row.createCell(0);

cell.setcellValue(new Java.util.Date());

// 将日期单元格的值格式化为日期字符串

CellStyle cellStyle = workbook.createCellStyle();

cellStyle.setDataFormat(workbook.getcreationHelper().createDataFormat().getFormat("yyyy-MM-dd"));

cell.setcellStyle(cellStyle);

// 将工作簿写入文件

try (FileOutputStream outputStream = new FileOutputStream("date_example.xlsx")) {

workbook.write(outputStream);

} catch (IOException e) {

e.printStackTrace();

}

}

}

以上代码创建了一个新的 excel 文件,其中包含一个日期单元格。日期单元格的值为当前日期,并使用指定的日期格式进行格式化。最后,将工作簿写入文件 "date_example.xlsx"。

Apache POI 时间单元是处理 excel 表格时非常重要的概念。本文介绍了日期时间单元、时间单元和日期单元的使用方法,并给出了相应的示例代码。通过使用 Apache POI,我们可以轻松地处理 excel 中的时间数据。

举报有用(4)分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号