Browse Source

完成csv测试

developing
Jiaju Zhuang 3 years ago
parent
commit
fd000d51ce
  1. 27
      src/main/java/com/alibaba/excel/metadata/csv/CsvCell.java
  2. 17
      src/main/java/com/alibaba/excel/metadata/csv/CsvCellStyle.java
  3. 2
      src/main/java/com/alibaba/excel/metadata/csv/CsvRow.java
  4. 22
      src/main/java/com/alibaba/excel/metadata/csv/CsvSheet.java
  5. 4
      src/main/java/com/alibaba/excel/metadata/data/DataFormatData.java
  6. 14
      src/main/java/com/alibaba/excel/util/WorkBookUtil.java
  7. 5
      src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java
  8. 15
      src/main/java/com/alibaba/excel/write/metadata/holder/WriteWorkbookHolder.java

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

17
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

2
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 {
/**

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

4
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();

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

5
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
* <p>

15
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
* <p>
@ -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 {

Loading…
Cancel
Save