Browse Source

修改日期格式

developing
Jiaju Zhuang 3 years ago
parent
commit
d970854f6f
  1. 52
      src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java
  2. 13
      src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java
  3. 48
      src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java
  4. 11
      src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java
  5. 2
      src/main/java/com/alibaba/excel/util/StyleUtil.java
  6. 2
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  7. 2
      src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

52
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.Calendar;
import java.util.Date; import java.util.Date;
import com.alibaba.excel.enums.NumericCellTypeEnum;
import com.alibaba.excel.metadata.data.FormulaData; import com.alibaba.excel.metadata.data.FormulaData;
import lombok.AccessLevel; 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.CellStyle;
import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Comment; 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.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
@ -44,6 +46,17 @@ public class CsvCell extends CellBase {
@Getter(value = AccessLevel.NONE) @Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE) @Setter(value = AccessLevel.NONE)
private CellType cellType; private CellType cellType;
/**
* workbook
*/
private final CsvWorkbook csvWorkbook;
/**
* sheet
*/
private final CsvSheet csvSheet;
/** /**
* row * row
*/ */
@ -52,7 +65,7 @@ public class CsvCell extends CellBase {
/** /**
* {@link CellType#NUMERIC} * {@link CellType#NUMERIC}
*/ */
private BigDecimal numberValue; private Double numberValue;
/** /**
* {@link CellType#STRING} and {@link CellType#ERROR} {@link CellType#FORMULA} * {@link CellType#STRING} and {@link CellType#ERROR} {@link CellType#FORMULA}
*/ */
@ -62,11 +75,6 @@ public class CsvCell extends CellBase {
*/ */
private Boolean booleanValue; private Boolean booleanValue;
/**
* {@link CellType#NUMERIC}
*/
private LocalDateTime dateValue;
/** /**
* formula * formula
*/ */
@ -82,7 +90,9 @@ public class CsvCell extends CellBase {
*/ */
private CellStyle cellStyle; 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.csvRow = csvRow;
this.columnIndex = columnIndex; this.columnIndex = columnIndex;
this.cellType = cellType; this.cellType = cellType;
@ -111,31 +121,25 @@ public class CsvCell extends CellBase {
@Override @Override
protected void setCellValueImpl(double value) { protected void setCellValueImpl(double value) {
numberValue = BigDecimal.valueOf(value); this.numberValue = value;
this.cellType = CellType.NUMERIC; this.cellType = CellType.NUMERIC;
} }
@Override @Override
protected void setCellValueImpl(Date value) { protected void setCellValueImpl(Date value) {
if (value == null) { this.numberValue = DateUtil.getExcelDate(value, csvWorkbook.getUse1904windowing());
return;
}
this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault());
this.cellType = CellType.NUMERIC; this.cellType = CellType.NUMERIC;
} }
@Override @Override
protected void setCellValueImpl(LocalDateTime value) { protected void setCellValueImpl(LocalDateTime value) {
this.dateValue = value; this.numberValue = DateUtil.getExcelDate(value, csvWorkbook.getUse1904windowing());
this.cellType = CellType.NUMERIC; this.cellType = CellType.NUMERIC;
} }
@Override @Override
protected void setCellValueImpl(Calendar value) { protected void setCellValueImpl(Calendar value) {
if (value == null) { this.numberValue = DateUtil.getExcelDate(value, csvWorkbook.getUse1904windowing());
return;
}
this.dateValue = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault());
this.cellType = CellType.NUMERIC; this.cellType = CellType.NUMERIC;
} }
@ -227,15 +231,18 @@ public class CsvCell extends CellBase {
@Override @Override
public Date getDateCellValue() { public Date getDateCellValue() {
if (dateValue == null) { if (numberValue == null) {
return null; return null;
} }
return Date.from(dateValue.atZone(ZoneId.systemDefault()).toInstant()); return DateUtil.getJavaDate(numberValue, csvWorkbook.getUse1904windowing());
} }
@Override @Override
public LocalDateTime getLocalDateTimeCellValue() { public LocalDateTime getLocalDateTimeCellValue() {
return dateValue; if (numberValue == null) {
return null;
}
return DateUtil.getLocalDateTime(numberValue, csvWorkbook.getUse1904windowing());
} }
@Override @Override
@ -256,12 +263,15 @@ public class CsvCell extends CellBase {
@Override @Override
public void setCellErrorValue(byte value) { public void setCellErrorValue(byte value) {
this.numberValue = BigDecimal.valueOf(value); this.numberValue = value;
this.cellType = CellType.ERROR; this.cellType = CellType.ERROR;
} }
@Override @Override
public boolean getBooleanCellValue() { public boolean getBooleanCellValue() {
if (booleanValue == null) {
return false;
}
return booleanValue; return booleanValue;
} }

13
src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java

@ -25,10 +25,16 @@ public class CsvRow implements Row {
*/ */
private final List<CsvCell> cellList; private final List<CsvCell> cellList;
/**
* workbook
*/
private final CsvWorkbook csvWorkbook;
/** /**
* sheet * sheet
*/ */
private final CsvSheet csvSheet; private final CsvSheet csvSheet;
/** /**
* row index * row index
*/ */
@ -39,22 +45,23 @@ public class CsvRow implements Row {
*/ */
private CellStyle cellStyle; private CellStyle cellStyle;
public CsvRow(CsvSheet csvSheet, Integer rowIndex) { public CsvRow(CsvWorkbook csvWorkbook, CsvSheet csvSheet, Integer rowIndex) {
cellList = Lists.newArrayList(); cellList = Lists.newArrayList();
this.csvWorkbook = csvWorkbook;
this.csvSheet = csvSheet; this.csvSheet = csvSheet;
this.rowIndex = rowIndex; this.rowIndex = rowIndex;
} }
@Override @Override
public Cell createCell(int column) { public Cell createCell(int column) {
CsvCell cell = new CsvCell(this, column, null); CsvCell cell = new CsvCell(csvWorkbook, csvSheet, this, column, null);
cellList.add(cell); cellList.add(cell);
return cell; return cell;
} }
@Override @Override
public Cell createCell(int column, CellType type) { 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); cellList.add(cell);
return cell; return cell;
} }

48
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.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.enums.NumericCellTypeEnum;
import com.alibaba.excel.exception.ExcelGenerateException; 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.ListUtils;
import com.alibaba.excel.util.NumberDataFormatterUtils;
import com.alibaba.excel.util.StringUtils;
import lombok.Data; import lombok.Data;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
@ -61,7 +66,8 @@ public class CsvSheet implements Sheet, Closeable {
/** /**
* last row index * last row index
*/ */
private int lastRowIndex; private Integer lastRowIndex;
/** /**
* row cache * row cache
*/ */
@ -74,7 +80,7 @@ public class CsvSheet implements Sheet, Closeable {
public CsvSheet(CsvWorkbook csvWorkbook, Appendable out) { public CsvSheet(CsvWorkbook csvWorkbook, Appendable out) {
this.csvWorkbook = csvWorkbook; this.csvWorkbook = csvWorkbook;
this.out = out; this.out = out;
this.rowCacheCount = 500; this.rowCacheCount = 3000;
this.csvFormat = CSVFormat.DEFAULT; this.csvFormat = CSVFormat.DEFAULT;
this.lastRowIndex = -1; this.lastRowIndex = -1;
} }
@ -87,7 +93,7 @@ public class CsvSheet implements Sheet, Closeable {
lastRowIndex++; lastRowIndex++;
assert rownum == lastRowIndex : "csv create row must be in order."; assert rownum == lastRowIndex : "csv create row must be in order.";
printData(); printData();
CsvRow csvRow = new CsvRow(this, rownum); CsvRow csvRow = new CsvRow(csvWorkbook, this, rownum);
rowCache.add(csvRow); rowCache.add(csvRow);
return csvRow; return csvRow;
} }
@ -724,7 +730,7 @@ public class CsvSheet implements Sheet, Closeable {
Iterator<Cell> cellIterator = row.cellIterator(); Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) { while (cellIterator.hasNext()) {
CsvCell csvCell = (CsvCell)cellIterator.next(); CsvCell csvCell = (CsvCell)cellIterator.next();
csvPrinter.print(csvCell.getStringCellValue()); csvPrinter.print(buildCellValue(csvCell));
} }
csvPrinter.println(); 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
} }

11
src/main/java/com/alibaba/excel/metadata/csv/CsvWorkbook.java

@ -36,6 +36,14 @@ public class CsvWorkbook implements Workbook {
* locale * locale
*/ */
private Locale 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 * data format
@ -50,9 +58,10 @@ public class CsvWorkbook implements Workbook {
*/ */
private List<CsvCellStyle> csvCellStyleList; private List<CsvCellStyle> csvCellStyleList;
public CsvWorkbook(Appendable out, Locale locale) { public CsvWorkbook(Appendable out, Locale locale, Boolean use1904windowing) {
this.out = out; this.out = out;
this.locale = locale; this.locale = locale;
this.use1904windowing = use1904windowing;
} }
@Override @Override

2
src/main/java/com/alibaba/excel/util/StyleUtil.java

@ -123,7 +123,7 @@ public class StyleUtil {
public static Font buildFont(Workbook workbook, WriteFont writeFont) { public static Font buildFont(Workbook workbook, WriteFont writeFont) {
Font font = workbook.createFont(); Font font = workbook.createFont();
if (writeFont == null) { if (writeFont == null || font == null) {
return font; return font;
} }
if (writeFont.getFontName() != null) { if (writeFont.getFontName() != null) {

2
src/main/java/com/alibaba/excel/util/WorkBookUtil.java

@ -69,7 +69,7 @@ public class WorkBookUtil {
case CSV: case CSV:
CsvWorkbook csvWorkbook = new CsvWorkbook( CsvWorkbook csvWorkbook = new CsvWorkbook(
new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset()), new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset()),
writeWorkbookHolder.getGlobalConfiguration().getLocale()); writeWorkbookHolder.getGlobalConfiguration().getLocale(), writeWorkbookHolder.getGlobalConfiguration().getUse1904windowing());
writeWorkbookHolder.setCachedWorkbook(csvWorkbook); writeWorkbookHolder.setCachedWorkbook(csvWorkbook);
writeWorkbookHolder.setWorkbook(csvWorkbook); writeWorkbookHolder.setWorkbook(csvWorkbook);
return; return;

2
src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

@ -84,7 +84,7 @@ public class CsvReadTest {
List<CsvData> list = new ArrayList<>(); List<CsvData> list = new ArrayList<>();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
CsvData data = new CsvData(); CsvData data = new CsvData();
data.setString("字符串" + i); data.setString("字符,串" + i);
data.setDate(new Date()); data.setDate(new Date());
data.setDoubleData(0.56); data.setDoubleData(0.56);
data.setIgnore("忽略" + i); data.setIgnore("忽略" + i);

Loading…
Cancel
Save