Browse Source

修改日期格式

pull/2077/head
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.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;
}

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;
/**
* 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;
}

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.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<Cell> 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
}

11
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<CsvCellStyle> 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

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) {
Font font = workbook.createFont();
if (writeFont == null) {
if (writeFont == null || font == null) {
return font;
}
if (writeFont.getFontName() != null) {

2
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;

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

@ -84,7 +84,7 @@ public class CsvReadTest {
List<CsvData> 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);

Loading…
Cancel
Save