fix: parse string datetime cells in external import

This commit is contained in:
2026-07-21 16:37:24 +08:00
parent 02e8b48f6d
commit 5a42a0fc5a
2 changed files with 58 additions and 8 deletions

View File

@@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class ExternalJobExcelParserTest {
@Test
void parsesExcelDatesAndLocationFallback() throws Exception {
byte[] data = workbookBytes();
byte[] data = workbookBytes(false);
List<ExternalJobCandidate> rows = new ExternalJobExcelParser().parse(new ByteArrayInputStream(data));
assertEquals(1, rows.size());
@@ -27,7 +27,16 @@ class ExternalJobExcelParserTest {
assertEquals("Java 工程师", row.getJobTitle());
}
private byte[] workbookBytes() throws Exception {
@Test
void parsesStringDateTimeCellsWithoutTreatingThemAsNumericDates() throws Exception {
List<ExternalJobCandidate> rows = new ExternalJobExcelParser().parse(new ByteArrayInputStream(workbookBytes(true)));
assertEquals(1, rows.size());
assertEquals("2026-07-14", rows.get(0).getPostingDate());
assertEquals("2026-07-20", rows.get(0).getCollectDate());
}
private byte[] workbookBytes(boolean useStringDateTimes) throws Exception {
String[] headers = {"Aca112", "Acb22a", "SalaryLow", "SalaryHight", "Aae397", "AAB004", "ORG", "ACE760", "Collect_time", "City"};
try (XSSFWorkbook workbook = new XSSFWorkbook(); ByteArrayOutputStream output = new ByteArrayOutputStream()) {
Row header = workbook.createSheet("合并数据").createRow(0);
@@ -41,13 +50,21 @@ class ExternalJobExcelParserTest {
data.createCell(1).setCellValue("负责平台开发");
data.createCell(2).setCellValue(5000);
data.createCell(3).setCellValue(10000);
data.createCell(4).setCellValue(Date.from(LocalDate.of(2026, 7, 14).atStartOfDay(java.time.ZoneId.systemDefault()).toInstant()));
data.getCell(4).setCellStyle(dateStyle);
if (useStringDateTimes) {
data.createCell(4).setCellValue("2026-07-14 00:00:00");
} else {
data.createCell(4).setCellValue(Date.from(LocalDate.of(2026, 7, 14).atStartOfDay(java.time.ZoneId.systemDefault()).toInstant()));
data.getCell(4).setCellStyle(dateStyle);
}
data.createCell(5).setCellValue("示例企业");
data.createCell(6).setCellValue("智联招聘");
data.createCell(7).setCellValue("https://www.zhaopin.com/job/42");
data.createCell(8).setCellValue(Date.from(LocalDate.of(2026, 7, 20).atStartOfDay(java.time.ZoneId.systemDefault()).toInstant()));
data.getCell(8).setCellStyle(dateStyle);
if (useStringDateTimes) {
data.createCell(8).setCellValue("2026-07-20 00:00:00");
} else {
data.createCell(8).setCellValue(Date.from(LocalDate.of(2026, 7, 20).atStartOfDay(java.time.ZoneId.systemDefault()).toInstant()));
data.getCell(8).setCellStyle(dateStyle);
}
data.createCell(9).setCellValue("石河子市");
workbook.write(output);
return output.toByteArray();