diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java index 6128ebd4..a434758e 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java @@ -6,6 +6,7 @@ import java.time.ZoneId; import java.util.Calendar; import java.util.Date; +import com.alibaba.excel.enums.NumericCellTypeEnum; import com.alibaba.excel.metadata.data.FormulaData; import lombok.AccessLevel; @@ -17,6 +18,7 @@ import org.apache.poi.ss.usermodel.CellBase; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Comment; +import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; @@ -44,6 +46,17 @@ public class CsvCell extends CellBase { @Getter(value = AccessLevel.NONE) @Setter(value = AccessLevel.NONE) private CellType cellType; + + /** + * workbook + */ + private final CsvWorkbook csvWorkbook; + + /** + * sheet + */ + private final CsvSheet csvSheet; + /** * row */ @@ -52,7 +65,7 @@ public class CsvCell extends CellBase { /** * {@link CellType#NUMERIC} */ - private BigDecimal numberValue; + private Double numberValue; /** * {@link CellType#STRING} and {@link CellType#ERROR} {@link CellType#FORMULA} */ @@ -62,11 +75,6 @@ public class CsvCell extends CellBase { */ private Boolean booleanValue; - /** - * {@link CellType#NUMERIC} - */ - private LocalDateTime dateValue; - /** * formula */ @@ -82,7 +90,9 @@ public class CsvCell extends CellBase { */ private CellStyle cellStyle; - public CsvCell(CsvRow csvRow, Integer columnIndex, CellType cellType) { + public CsvCell(CsvWorkbook csvWorkbook, CsvSheet csvSheet, CsvRow csvRow, Integer columnIndex, CellType cellType) { + this.csvWorkbook = csvWorkbook; + this.csvSheet = csvSheet; this.csvRow = csvRow; this.columnIndex = columnIndex; this.cellType = cellType; @@ -111,31 +121,25 @@ public class CsvCell extends CellBase { @Override protected void setCellValueImpl(double value) { - numberValue = BigDecimal.valueOf(value); + this.numberValue = value; this.cellType = CellType.NUMERIC; } @Override protected void setCellValueImpl(Date value) { - if (value == null) { - return; - } - this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()); + this.numberValue = DateUtil.getExcelDate(value, csvWorkbook.getUse1904windowing()); this.cellType = CellType.NUMERIC; } @Override protected void setCellValueImpl(LocalDateTime value) { - this.dateValue = value; + this.numberValue = DateUtil.getExcelDate(value, csvWorkbook.getUse1904windowing()); this.cellType = CellType.NUMERIC; } @Override protected void setCellValueImpl(Calendar value) { - if (value == null) { - return; - } - this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()); + this.numberValue = DateUtil.getExcelDate(value, csvWorkbook.getUse1904windowing()); this.cellType = CellType.NUMERIC; } @@ -227,15 +231,18 @@ public class CsvCell extends CellBase { @Override public Date getDateCellValue() { - if (dateValue == null) { + if (numberValue == null) { return null; } - return Date.from(dateValue.atZone(ZoneId.systemDefault()).toInstant()); + return DateUtil.getJavaDate(numberValue, csvWorkbook.getUse1904windowing()); } @Override public LocalDateTime getLocalDateTimeCellValue() { - return dateValue; + if (numberValue == null) { + return null; + } + return DateUtil.getLocalDateTime(numberValue, csvWorkbook.getUse1904windowing()); } @Override @@ -256,12 +263,15 @@ public class CsvCell extends CellBase { @Override public void setCellErrorValue(byte value) { - this.numberValue = BigDecimal.valueOf(value); + this.numberValue = value; this.cellType = CellType.ERROR; } @Override public boolean getBooleanCellValue() { + if (booleanValue == null) { + return false; + } return booleanValue; } diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java index 0550e611..24acd9d9 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java @@ -25,10 +25,16 @@ public class CsvRow implements Row { */ private final List cellList; + /** + * workbook + */ + private final CsvWorkbook csvWorkbook; + /** * sheet */ private final CsvSheet csvSheet; + /** * row index */ @@ -39,22 +45,23 @@ public class CsvRow implements Row { */ private CellStyle cellStyle; - public CsvRow(CsvSheet csvSheet, Integer rowIndex) { + public CsvRow(CsvWorkbook csvWorkbook, CsvSheet csvSheet, Integer rowIndex) { cellList = Lists.newArrayList(); + this.csvWorkbook = csvWorkbook; this.csvSheet = csvSheet; this.rowIndex = rowIndex; } @Override public Cell createCell(int column) { - CsvCell cell = new CsvCell(this, column, null); + CsvCell cell = new CsvCell(csvWorkbook, csvSheet, this, column, null); cellList.add(cell); return cell; } @Override public Cell createCell(int column, CellType type) { - CsvCell cell = new CsvCell(this, column, type); + CsvCell cell = new CsvCell(csvWorkbook, csvSheet, this, column, type); cellList.add(cell); return cell; } diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java index fa83fd47..0b203804 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java @@ -7,8 +7,13 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import com.alibaba.excel.enums.NumericCellTypeEnum; import com.alibaba.excel.exception.ExcelGenerateException; +import com.alibaba.excel.metadata.data.WriteCellData; +import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.ListUtils; +import com.alibaba.excel.util.NumberDataFormatterUtils; +import com.alibaba.excel.util.StringUtils; import lombok.Data; import org.apache.commons.csv.CSVFormat; @@ -61,7 +66,8 @@ public class CsvSheet implements Sheet, Closeable { /** * last row index */ - private int lastRowIndex; + private Integer lastRowIndex; + /** * row cache */ @@ -74,7 +80,7 @@ public class CsvSheet implements Sheet, Closeable { public CsvSheet(CsvWorkbook csvWorkbook, Appendable out) { this.csvWorkbook = csvWorkbook; this.out = out; - this.rowCacheCount = 500; + this.rowCacheCount = 3000; this.csvFormat = CSVFormat.DEFAULT; this.lastRowIndex = -1; } @@ -87,7 +93,7 @@ public class CsvSheet implements Sheet, Closeable { lastRowIndex++; assert rownum == lastRowIndex : "csv create row must be in order."; printData(); - CsvRow csvRow = new CsvRow(this, rownum); + CsvRow csvRow = new CsvRow(csvWorkbook, this, rownum); rowCache.add(csvRow); return csvRow; } @@ -724,7 +730,7 @@ public class CsvSheet implements Sheet, Closeable { Iterator cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { CsvCell csvCell = (CsvCell)cellIterator.next(); - csvPrinter.print(csvCell.getStringCellValue()); + csvPrinter.print(buildCellValue(csvCell)); } csvPrinter.println(); } @@ -734,4 +740,38 @@ public class CsvSheet implements Sheet, Closeable { } } + private String buildCellValue(CsvCell csvCell) { + switch (csvCell.getCellType()) { + case STRING: + case ERROR: + case FORMULA: + return csvCell.getStringCellValue(); + case NUMERIC: + + NumberDataFormatterUtils.format() + + if (csvCell.getNumericCellType() == NumericCellTypeEnum.DATE) { + // date + if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) { + return new WriteCellData<>(DateUtils.format(value, null)); + } else { + return new WriteCellData<>( + DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat())); + } + + } else { + //number + + } + case BOOLEAN: + return csvCell.getBooleanValue().toString(); + case BLANK: + return StringUtils.EMPTY; + default: + return null; + } + } + + private String + } diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java index ae8a3e50..ba55e01f 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java @@ -36,6 +36,14 @@ public class CsvWorkbook implements Workbook { * locale */ private Locale locale; + /** + * true if date uses 1904 windowing, or false if using 1900 date windowing. + * + * default is false + * + * @return + */ + private Boolean use1904windowing; /** * data format @@ -50,9 +58,10 @@ public class CsvWorkbook implements Workbook { */ private List csvCellStyleList; - public CsvWorkbook(Appendable out, Locale locale) { + public CsvWorkbook(Appendable out, Locale locale, Boolean use1904windowing) { this.out = out; this.locale = locale; + this.use1904windowing = use1904windowing; } @Override diff --git a/src/main/java/com/alibaba/excel/util/StyleUtil.java b/src/main/java/com/alibaba/excel/util/StyleUtil.java index 0b70cf9b..70c7ac7a 100644 --- a/src/main/java/com/alibaba/excel/util/StyleUtil.java +++ b/src/main/java/com/alibaba/excel/util/StyleUtil.java @@ -123,7 +123,7 @@ public class StyleUtil { public static Font buildFont(Workbook workbook, WriteFont writeFont) { Font font = workbook.createFont(); - if (writeFont == null) { + if (writeFont == null || font == null) { return font; } if (writeFont.getFontName() != null) { diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java index 5071709d..a7a3d74a 100644 --- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java +++ b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java @@ -69,7 +69,7 @@ public class WorkBookUtil { case CSV: CsvWorkbook csvWorkbook = new CsvWorkbook( new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset()), - writeWorkbookHolder.getGlobalConfiguration().getLocale()); + writeWorkbookHolder.getGlobalConfiguration().getLocale(), writeWorkbookHolder.getGlobalConfiguration().getUse1904windowing()); writeWorkbookHolder.setCachedWorkbook(csvWorkbook); writeWorkbookHolder.setWorkbook(csvWorkbook); return; diff --git a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java index 653eafc6..d6f8fb2a 100644 --- a/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java @@ -84,7 +84,7 @@ public class CsvReadTest { List list = new ArrayList<>(); for (int i = 0; i < 10; i++) { CsvData data = new CsvData(); - data.setString("字符串" + i); + data.setString("字符,串" + i); data.setDate(new Date()); data.setDoubleData(0.56); data.setIgnore("忽略" + i);