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 ef627392..4898ff2c 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java @@ -8,6 +8,10 @@ import java.util.Date; import com.alibaba.excel.metadata.data.FormulaData; +import lombok.AccessLevel; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.CellBase; import org.apache.poi.ss.usermodel.CellStyle; @@ -24,16 +28,21 @@ import org.apache.poi.ss.util.CellRangeAddress; * * @author Jiaju Zhuang */ +@Data public class CsvCell extends CellBase { /** * column index */ + @Getter(value = AccessLevel.NONE) + @Setter(value = AccessLevel.NONE) private Integer columnIndex; /** * cell type */ + @Getter(value = AccessLevel.NONE) + @Setter(value = AccessLevel.NONE) private CellType cellType; /** * row @@ -132,6 +141,24 @@ public class CsvCell extends CellBase { richTextString = value; } + @Override + public void setCellValue(String value) { + if (value == null) { + setBlank(); + return; + } + setCellValueImpl(value); + } + + @Override + public void setCellValue(RichTextString value) { + if (value == null || value.getString() == null) { + setBlank(); + return; + } + setCellValueImpl(value); + } + @Override protected SpreadsheetVersion getSpreadsheetVersion() { return null; diff --git a/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java b/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java index 8c8fe981..ca153e45 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java @@ -2,6 +2,7 @@ package com.alibaba.excel.metadata.csv; import com.alibaba.excel.metadata.data.DataFormatData; +import lombok.Data; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Color; @@ -15,6 +16,7 @@ import org.apache.poi.ss.usermodel.VerticalAlignment; * * @author Jiaju Zhuang */ +@Data public class CsvCellStyle implements CellStyle { /** @@ -33,22 +35,31 @@ public class CsvCellStyle implements CellStyle { @Override public short getIndex() { - return 0; + return index; } @Override public void setDataFormat(short fmt) { + initDataFormatData(); + dataFormatData.setIndex(fmt); + } + private void initDataFormatData() { + if (dataFormatData == null) { + dataFormatData = new DataFormatData(); + } } @Override public short getDataFormat() { - return 0; + initDataFormatData(); + return dataFormatData.getIndex(); } @Override public String getDataFormatString() { - return null; + initDataFormatData(); + return dataFormatData.getFormat(); } @Override 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 d7248313..0550e611 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java @@ -3,6 +3,7 @@ package com.alibaba.excel.metadata.csv; import java.util.Iterator; import java.util.List; +import lombok.Data; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.compress.utils.Lists; import org.apache.poi.ss.usermodel.Cell; @@ -16,6 +17,7 @@ import org.apache.poi.ss.usermodel.Sheet; * * @author Jiaju Zhuang */ +@Data public class CsvRow implements Row { /** 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 07f09e73..fa83fd47 100644 --- a/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java +++ b/src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java @@ -1,13 +1,7 @@ package com.alibaba.excel.metadata.csv; import java.io.Closeable; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.nio.charset.Charset; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -77,22 +71,6 @@ public class CsvSheet implements Sheet, Closeable { */ private CSVPrinter csvPrinter; - public CsvSheet(CsvWorkbook csvWorkbook, File file, Charset charset) throws FileNotFoundException { - this.csvWorkbook = csvWorkbook; - this.out = new OutputStreamWriter(new FileOutputStream(file), charset); - this.rowCacheCount = 500; - this.csvFormat = CSVFormat.DEFAULT; - this.lastRowIndex = -1; - } - - public CsvSheet(CsvWorkbook csvWorkbook, OutputStream outputStream) { - this.csvWorkbook = csvWorkbook; - this.out = new OutputStreamWriter(outputStream); - this.rowCacheCount = 500; - this.csvFormat = CSVFormat.DEFAULT; - this.lastRowIndex = -1; - } - public CsvSheet(CsvWorkbook csvWorkbook, Appendable out) { this.csvWorkbook = csvWorkbook; this.out = out; diff --git a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java index 7f1a14c8..2cbcb42c 100644 --- a/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java +++ b/src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java @@ -19,6 +19,10 @@ public class DataFormatData { */ private String format; + public DataFormatData() { + this.index = 0; + } + @Override public DataFormatData clone() { DataFormatData dataFormatData = new DataFormatData(); diff --git a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java index 3fb6575e..5071709d 100644 --- a/src/main/java/com/alibaba/excel/util/WorkBookUtil.java +++ b/src/main/java/com/alibaba/excel/util/WorkBookUtil.java @@ -1,6 +1,5 @@ package com.alibaba.excel.util; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; @@ -68,16 +67,9 @@ public class WorkBookUtil { } return; case CSV: - CsvWorkbook csvWorkbook; - if (writeWorkbookHolder.getFile() != null) { - csvWorkbook = new CsvWorkbook( - new OutputStreamWriter(new FileOutputStream(writeWorkbookHolder.getFile()), - writeWorkbookHolder.getFileCharset()), - writeWorkbookHolder.getGlobalConfiguration().getLocale()); - } else { - csvWorkbook = new CsvWorkbook(null, writeWorkbookHolder.getGlobalConfiguration().getLocale()); - } - CsvWorkbook csvWorkbook = new CsvWorkbook(writeWorkbookHolder); + CsvWorkbook csvWorkbook = new CsvWorkbook( + new OutputStreamWriter(writeWorkbookHolder.getOutputStream(), writeWorkbookHolder.getCharset()), + writeWorkbookHolder.getGlobalConfiguration().getLocale()); writeWorkbookHolder.setCachedWorkbook(csvWorkbook); writeWorkbookHolder.setWorkbook(csvWorkbook); return; diff --git a/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java index 899e4ca1..9f4bea5c 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java +++ b/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java @@ -3,6 +3,7 @@ package com.alibaba.excel.write.metadata; import java.io.File; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.Charset; import com.alibaba.excel.support.ExcelTypeEnum; @@ -31,6 +32,10 @@ public class WriteWorkbook extends WriteBasicParameter { * If 'outputStream' and 'file' all not empty,file first */ private OutputStream outputStream; + /** + * output charset + */ + private Charset charset; /** * Template input stream *

diff --git a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java index cb29ec22..a7871ba7 100644 --- a/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java +++ b/src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java @@ -65,14 +65,14 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { * If 'outputStream' and 'file' all not empty,file first */ private File file; - /** - * charset of final output file - */ - private Charset fileCharset; /** * Final output stream */ private OutputStream outputStream; + /** + * output charset + */ + private Charset charset; /** * Template input stream *

@@ -152,6 +152,13 @@ public class WriteWorkbookHolder extends AbstractWriteHolder { } else { this.outputStream = writeWorkbook.getOutputStream(); } + + if (writeWorkbook.getCharset() == null) { + this.charset = Charset.defaultCharset(); + } else { + this.charset = writeWorkbook.getCharset(); + } + if (writeWorkbook.getAutoCloseStream() == null) { this.autoCloseStream = Boolean.TRUE; } else {