import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Objects;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* 读取Excel
*/
public class ReadExcelUtils {
public static void main(String[] args) {
try {
String filepath = "C:\\Users\\Administrator\\Desktop\\xxx.xlsx";
File file = new File(filepath);//path是excel路径
XSSFWorkbook sheets = new XSSFWorkbook(file);
for (Sheet sheet : sheets) {
String sheetName = sheet.getSheetName();
int lastRowNum = sheet.getPhysicalNumberOfRows();
System.err.println(sheetName);
for (int i = 0; i < lastRowNum; i++) {
Row row = sheet.getRow(i);
int physicalNumberOfCells = row.getPhysicalNumberOfCells();
for (int j = 0; j < physicalNumberOfCells; j++) {
Cell cell = row.getCell(j);
Date date = null;
try {
date = cell.getDateCellValue();
} catch (IllegalStateException e) {
String stringCellValue = cell.getStringCellValue();
System.err.print(stringCellValue + "\t\t");
}
if (Objects.nonNull(date)) {
System.err.println(date);
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();
System.err.println(localDate);
System.err.println(localDateTime);
}
}
System.err.println();
}
}
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
}
}
}
Java一次性读取excel文件快速实现
最新推荐文章于 2025-02-28 00:15:00 发布