Skip to content
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

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
92a2ea6
Enhanced to read and write formatted dates and formulas.
Harsha0211 Oct 2, 2023
9327ba4
Issue-27 fix
Harsha0211 Oct 3, 2023
bfdb650
minor variable name change
Harsha0211 Oct 3, 2023
1a44805
Enhanced to read and write formatted dates and formulas-modified acc.…
Harsha0211 Oct 3, 2023
41ff52e
minor code refactor
Harsha0211 Oct 4, 2023
00efbc6
executed DateTest.java->sample-files changed
Harsha0211 Oct 4, 2023
78995e4
Enhanced to write new sheet to existing file without clearing prev. s…
Harsha0211 Oct 6, 2023
8944d87
Revert "Enhanced to write new sheet to existing file without clearing…
Harsha0211 Oct 6, 2023
ee9b2bf
[TestCase Update] updated test cases with Date format checks
Harsha0211 Oct 10, 2023
114501c
[SheetColumn] removed 'isFormated' field from sheet column
Harsha0211 Oct 10, 2023
1d48dce
[DateFormat] added support for localDate and Date
Harsha0211 Oct 10, 2023
6cd9cbb
[RMVD-Formula part] Removed all the formula related code
Harsha0211 Oct 10, 2023
b45ad03
[Final keyword] Added final keyword at necessary lines
Harsha0211 Oct 10, 2023
fdfb8a5
[LocalDate read] Modified code to support LocalDate
Harsha0211 Oct 11, 2023
0819f0b
[Test Cases] Added and modified files for Dates TestCases
Harsha0211 Oct 11, 2023
6037107
[Finla keyword]Added final keyword at necessary lines
Harsha0211 Oct 11, 2023
fba7611
[Unused imports] Removed unused imports
Harsha0211 Oct 11, 2023
851936e
[Revert] reverted sheet and rowListeners file from develop branch
Harsha0211 Oct 11, 2023
2e44848
[Code refactor] Replaced SheetColumn,SSReader,Beans,XlsxReader files …
Harsha0211 Oct 11, 2023
5f44494
[Code Refactor] Added minor changes to replaced files in prev commit
Harsha0211 Oct 11, 2023
d59ac27
[Final] Added final keyword
Harsha0211 Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
* @return column name/header.
*/
String value() default "";
boolean isFormatted() default false;
String format() default "dd/MM/yyyy";

boolean isFromula() default false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct formula spelling


/**
* Setting this to <code>false</code> will enable the null check on the Column values, to ensure
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/io/github/millij/poi/ss/reader/XlsReader.java
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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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>();
Expand All @@ -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);

Choose a reason for hiding this comment

The 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");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

store default format as const..

Also, Can be written as
final String format = cellFormat != null ? cellFormat : defaultFormat; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);

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;
Expand Down
140 changes: 0 additions & 140 deletions src/main/java/io/github/millij/poi/ss/reader/XlsxReader.java

This file was deleted.

Loading