[bug-69583] when copying cells with dates - prefer using the numeric data directly

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1923780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2025-02-13 10:53:16 +00:00
parent d6d403371a
commit 3a3effca65
2 changed files with 28 additions and 0 deletions

View File

@ -727,6 +727,7 @@ public class DateUtil {
case 0x0f:
case 0x10:
case 0x11:
// the 0x12 to 0x15 formats are time (only) formats
case 0x12:
case 0x13:
case 0x14:

View File

@ -146,6 +146,33 @@ public abstract class BaseTestCellUtilCopy {
wb.close();
}
@Test
public final void testCopyCellTime() throws IOException {
try(
Workbook srcWb = createNewWorkbook();
Workbook destWb = createNewWorkbook()
) {
final Row rowS = srcWb.createSheet("Sheet1").createRow(0);
final Row rowD = destWb.createSheet("Sheet1").createRow(0);
srcCell = rowS.createCell(0);
destCell = rowD.createCell(0);
srcCell.setCellValue(22.0/24.0);
final CellStyle style = srcWb.createCellStyle();
style.setDataFormat((short) 0x12); // time format
srcCell.setCellStyle(style);
final CreationHelper createHelper = srcWb.getCreationHelper();
final CellCopyPolicy policy = new CellCopyPolicy.Builder().build();
CellUtil.copyCell(srcCell, destCell, policy, new CellCopyContext());
assertEquals(srcCell.getNumericCellValue(), destCell.getNumericCellValue());
assertEquals(srcCell.getCellStyle().getDataFormatString(), destCell.getCellStyle().getDataFormatString());
}
}
private void setUp_testCopyCellFrom_CellCopyPolicy() {
@SuppressWarnings("resource")
final Workbook wb = createNewWorkbook();