java中读取Excel某一单元格中的内容是时间怎么接收
在Java中,可以使用Apache POI库来读取Excel文件。以下是一个示例代码,演示如何读取Excel中某一单元格的时间内容:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
public class ReadExcel {
public static void main(String[] args) {
String filePath = "path/to/your/excel.xlsx";
String sheetName = "Sheet1";
int rowNum = 1; // 行号,从0开始
int cellNum = 2; // 列号,从0开始
try (FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis)) {
Sheet sheet = workbook.getSheet(sheetName);
Row row = sheet.getRow(rowNum);
Cell cell = row.getCell(cellNum);
if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
System.out.println("The time value is: " + date);
} else {
System.out.println("The cell does not contain a time value.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
请确保你已经将Apache POI库添加到你的项目中。这里使用的是POI的最新版本,即Apache POI 5.0.0。你可以从POI的官方网站(https://poi.apache.org/)下载并添加相应的JAR文件。
原文地址: https://www.cveoy.top/t/topic/i7dH 著作权归作者所有。请勿转载和采集!