Skip to content

Commit 3a3effc

Browse files
committed
[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
1 parent d6d4033 commit 3a3effc

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ public static boolean isInternalDateFormat(int format) {
727727
case 0x0f:
728728
case 0x10:
729729
case 0x11:
730+
// the 0x12 to 0x15 formats are time (only) formats
730731
case 0x12:
731732
case 0x13:
732733
case 0x14:

poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtilCopy.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,33 @@ public final void testCopyCellFrom_CellCopyPolicy_copyHyperlink() throws IOExcep
146146
wb.close();
147147
}
148148

149+
@Test
150+
public final void testCopyCellTime() throws IOException {
151+
try(
152+
Workbook srcWb = createNewWorkbook();
153+
Workbook destWb = createNewWorkbook()
154+
) {
155+
final Row rowS = srcWb.createSheet("Sheet1").createRow(0);
156+
final Row rowD = destWb.createSheet("Sheet1").createRow(0);
157+
srcCell = rowS.createCell(0);
158+
destCell = rowD.createCell(0);
159+
160+
srcCell.setCellValue(22.0/24.0);
161+
162+
final CellStyle style = srcWb.createCellStyle();
163+
style.setDataFormat((short) 0x12); // time format
164+
srcCell.setCellStyle(style);
165+
166+
final CreationHelper createHelper = srcWb.getCreationHelper();
167+
168+
final CellCopyPolicy policy = new CellCopyPolicy.Builder().build();
169+
CellUtil.copyCell(srcCell, destCell, policy, new CellCopyContext());
170+
171+
assertEquals(srcCell.getNumericCellValue(), destCell.getNumericCellValue());
172+
assertEquals(srcCell.getCellStyle().getDataFormatString(), destCell.getCellStyle().getDataFormatString());
173+
}
174+
}
175+
149176
private void setUp_testCopyCellFrom_CellCopyPolicy() {
150177
@SuppressWarnings("resource")
151178
final Workbook wb = createNewWorkbook();

0 commit comments

Comments
 (0)