Skip to content

Commit 0ddd3bd

Browse files
committed
Merge pull request #29 from stjohnb/correctly-detect-date-formatted-cells
Correctly detect date formatted cells
2 parents 8a016a9 + f44c06b commit 0ddd3bd

3 files changed

Lines changed: 31 additions & 16 deletions

File tree

src/main/java/com/monitorjbl/xlsx/StreamingReader.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ private void handleEvent(XMLEvent event) throws SAXException {
129129
} else {
130130
currentCell.setType("n");
131131
}
132+
133+
Attribute style = startElement.getAttributeByName(new QName("s"));
134+
135+
if(style != null){
136+
String indexStr = style.getValue();
137+
try{
138+
int index = Integer.parseInt(indexStr);
139+
currentCell.setCellStyle(stylesTable.getStyleAt(index));
140+
} catch(NumberFormatException nfe) {
141+
log.warn("Ignoring invalid style index {}", indexStr);
142+
}
143+
}
132144
}
133145

134146
// Clear contents cache

src/main/java/com/monitorjbl/xlsx/impl/StreamingCell.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class StreamingCell implements Cell {
2525
private Short numericFormatIndex;
2626
private String type;
2727
private Row row;
28+
private CellStyle cellStyle;
2829

2930
public StreamingCell(int columnIndex, int rowIndex) {
3031
this.columnIndex = columnIndex;
@@ -75,6 +76,11 @@ public void setRow(Row row) {
7576
this.row = row;
7677
}
7778

79+
@Override
80+
public void setCellStyle(CellStyle cellStyle) {
81+
this.cellStyle = cellStyle;
82+
}
83+
7884
/* Supported */
7985

8086
/**
@@ -176,6 +182,17 @@ public Date getDateCellValue() {
176182
return rawContents == null ? null : HSSFDateUtil.getJavaDate(getNumericCellValue());
177183
}
178184

185+
/**
186+
* Returns the style of the cell
187+
*
188+
* @return the style of the cell
189+
*/
190+
@Override
191+
public CellStyle getCellStyle() {
192+
return this.cellStyle;
193+
}
194+
195+
179196
/* Not supported */
180197

181198
/**
@@ -298,22 +315,6 @@ public byte getErrorCellValue() {
298315
throw new NotSupportedException();
299316
}
300317

301-
/**
302-
* Not supported
303-
*/
304-
@Override
305-
public void setCellStyle(CellStyle style) {
306-
throw new NotSupportedException();
307-
}
308-
309-
/**
310-
* Not supported
311-
*/
312-
@Override
313-
public CellStyle getCellStyle() {
314-
throw new NotSupportedException();
315-
}
316-
317318
/**
318319
* Not supported
319320
*/

src/test/java/com/monitorjbl/xlsx/StreamingReaderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.monitorjbl.xlsx.exceptions.MissingSheetException;
44
import org.apache.poi.ss.usermodel.Cell;
5+
import org.apache.poi.ss.usermodel.DateUtil;
56
import org.apache.poi.ss.usermodel.Row;
67
import org.junit.BeforeClass;
78
import org.junit.Test;
@@ -86,6 +87,7 @@ public void testTypes() throws Exception {
8687
assertEquals(Cell.CELL_TYPE_NUMERIC, row.get(1).getCellType());
8788
assertEquals("date", row.get(0).getStringCellValue());
8889
assertEquals(df.parse("1/1/2014"), row.get(1).getDateCellValue());
90+
assertTrue(DateUtil.isCellDateFormatted(row.get(1)));
8991

9092
row = obj.get(5);
9193
assertEquals(7, row.size());

0 commit comments

Comments
 (0)