Java导入Excel,保留日期格式为文本格式
Java 读取Excel文件,防止日期格式变为数字
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;public class ExcelImporter {public static void main(String[] args) {String filePath = "path/to/excel/file.xls";try (HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath))) {HSSFSheet sheet = workbook.getSheetAt(0);for (Row row : sheet) {HSSFCell cell = (HSSFCell) row.getCell(0);if (cell != null) {if (cell.getCellType() == CellType.NUMERIC && HSSFDateUtil.isCellDateFormatted(cell)) {Date date = cell.getDateCellValue();SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");String dateString = dateFormat.format(date);System.out.println("日期: " + dateString);}}}} catch (IOException e) {e.printStackTrace();}}
}