-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhanced to read and write formatted dates and formulas. #48
base: develop
Are you sure you want to change the base?
Changes from 2 commits
92a2ea6
9327ba4
bfdb650
1a44805
41ff52e
00efbc6
78995e4
8944d87
ee9b2bf
114501c
1d48dce
6cd9cbb
b45ad03
fdfb8a5
0819f0b
6037107
fba7611
851936e
2e44848
5f44494
d59ac27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
package io.github.millij.poi.ss.reader; | ||
|
||
import static io.github.millij.poi.util.Beans.isInstantiableType; | ||
|
||
import io.github.millij.poi.SpreadsheetReadException; | ||
import io.github.millij.poi.ss.handler.RowListener; | ||
import io.github.millij.poi.ss.writer.SpreadsheetWriter; | ||
import io.github.millij.poi.util.Spreadsheet; | ||
|
||
import java.io.InputStream; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import org.apache.poi.ss.usermodel.DateUtil; | ||
import org.apache.poi.hssf.usermodel.HSSFCell; | ||
import org.apache.poi.hssf.usermodel.HSSFRow; | ||
import org.apache.poi.hssf.usermodel.HSSFSheet; | ||
|
@@ -117,7 +124,7 @@ protected <T> void processSheet(Class<T> beanClz, HSSFSheet sheet, int headerRow | |
continue; // Skip Header row | ||
} | ||
|
||
Map<String, Object> rowDataMap = this.extractRowDataAsMap(row, headerMap); | ||
Map<String, Object> rowDataMap = this.extractRowDataAsMap(beanClz,row, headerMap); | ||
if (rowDataMap == null || rowDataMap.isEmpty()) { | ||
continue; | ||
} | ||
|
@@ -169,7 +176,7 @@ private Map<Integer, String> extractCellHeaderMap(HSSFRow headerRow) { | |
return cellHeaderMap; | ||
} | ||
|
||
private Map<String, Object> extractRowDataAsMap(HSSFRow row, Map<Integer, String> columnHeaderMap) { | ||
private <T> Map<String, Object> extractRowDataAsMap(Class<T> beanClz,HSSFRow row, Map<Integer, String> columnHeaderMap) { | ||
// Sanity checks | ||
if (row == null) { | ||
return new HashMap<String, Object>(); | ||
|
@@ -190,12 +197,35 @@ private Map<String, Object> extractRowDataAsMap(HSSFRow row, Map<Integer, String | |
rowDataMap.put(cellColName, cell.getStringCellValue()); | ||
break; | ||
case NUMERIC: | ||
rowDataMap.put(cellColName, cell.getNumericCellValue()); | ||
break; | ||
if (DateUtil.isCellDateFormatted(cell)) { | ||
|
||
Map<String, String> formats = SpreadsheetWriter.getFormats(beanClz); | ||
String cellFormat = formats.get(cellColName); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final ? |
||
|
||
Date date = cell.getDateCellValue(); | ||
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); | ||
|
||
DateTimeFormatter formatter = null; | ||
if (cellFormat != null) { | ||
formatter = DateTimeFormatter.ofPattern(cellFormat); | ||
} else { | ||
formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. store default format as const.. Also, Can be written as |
||
String formattedDateTime = localDateTime.format(formatter); | ||
rowDataMap.put(cellColName, formattedDateTime); | ||
break; | ||
|
||
} else { | ||
rowDataMap.put(cellColName, cell.getNumericCellValue()); | ||
break; | ||
} | ||
|
||
case BOOLEAN: | ||
rowDataMap.put(cellColName, cell.getBooleanCellValue()); | ||
break; | ||
case FORMULA: | ||
rowDataMap.put(cellColName, cell.getCellFormula()); | ||
case BLANK: | ||
case ERROR: | ||
break; | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct formula spelling