From fbb79df74c0dbe972939aa2ac3c98464256086e9 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Tue, 7 Sep 2021 19:03:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=91=E9=80=9Acsv=20=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/alibaba/excel/csv/CsvCell.java | 12 +++++++++--- src/main/java/com/alibaba/excel/csv/CsvRow.java | 11 ++++++++--- src/main/java/com/alibaba/excel/csv/CsvSheet.java | 5 ++++- src/main/java/com/alibaba/excel/csv/CsvWorkbook.java | 1 + .../alibaba/easyexcel/test/temp/csv/CsvReadTest.java | 2 +- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/alibaba/excel/csv/CsvCell.java b/src/main/java/com/alibaba/excel/csv/CsvCell.java index 9c3a5d22..84808bf0 100644 --- a/src/main/java/com/alibaba/excel/csv/CsvCell.java +++ b/src/main/java/com/alibaba/excel/csv/CsvCell.java @@ -23,6 +23,8 @@ import org.apache.poi.ss.util.CellRangeAddress; */ public class CsvCell implements Cell { + private String value; + @Override public int getColumnIndex() { return 0; @@ -75,11 +77,12 @@ public class CsvCell implements Cell { @Override public void setCellValue(double value) { - + this.value = value + ""; } @Override public void setCellValue(Date value) { + this.value = value.toString(); } @@ -100,7 +103,7 @@ public class CsvCell implements Cell { @Override public void setCellValue(String value) { - + this.value = value; } @Override @@ -140,7 +143,7 @@ public class CsvCell implements Cell { @Override public String getStringCellValue() { - return null; + return value; } @Override @@ -222,4 +225,7 @@ public class CsvCell implements Cell { public boolean isPartOfArrayFormulaGroup() { return false; } + + + } diff --git a/src/main/java/com/alibaba/excel/csv/CsvRow.java b/src/main/java/com/alibaba/excel/csv/CsvRow.java index 6a979417..16129263 100644 --- a/src/main/java/com/alibaba/excel/csv/CsvRow.java +++ b/src/main/java/com/alibaba/excel/csv/CsvRow.java @@ -1,6 +1,8 @@ package com.alibaba.excel.csv; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; @@ -15,10 +17,13 @@ import org.apache.poi.ss.usermodel.Sheet; */ public class CsvRow implements Row { + public List list = new ArrayList<>(); @Override public Cell createCell(int column) { - return new CsvCell(); + CsvCell cell = new CsvCell(); + list.add(cell); + return cell; } @Override @@ -43,7 +48,7 @@ public class CsvRow implements Row { @Override public Cell getCell(int cellnum) { - return null; + return list.get(cellnum); } @Override @@ -58,7 +63,7 @@ public class CsvRow implements Row { @Override public short getLastCellNum() { - return 0; + return (short)list.size(); } @Override diff --git a/src/main/java/com/alibaba/excel/csv/CsvSheet.java b/src/main/java/com/alibaba/excel/csv/CsvSheet.java index eda6484a..db21787b 100644 --- a/src/main/java/com/alibaba/excel/csv/CsvSheet.java +++ b/src/main/java/com/alibaba/excel/csv/CsvSheet.java @@ -9,6 +9,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import com.alibaba.excel.util.ListUtils; @@ -668,7 +669,9 @@ public class CsvSheet implements Sheet { public void flushData() { try { - csvPrinter.printRecord(); + for (CsvRow row : rowCache) { + csvPrinter.printRecord(row.list.stream().map(CsvCell::getStringCellValue).collect(Collectors.toList())); + } } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/com/alibaba/excel/csv/CsvWorkbook.java b/src/main/java/com/alibaba/excel/csv/CsvWorkbook.java index 20002d83..082e770d 100644 --- a/src/main/java/com/alibaba/excel/csv/CsvWorkbook.java +++ b/src/main/java/com/alibaba/excel/csv/CsvWorkbook.java @@ -177,6 +177,7 @@ public class CsvWorkbook implements Workbook { @Override public void write(OutputStream stream) throws IOException { + csvSheet.flushData(); csvSheet.csvPrinter.flush(); csvSheet.csvPrinter.close(); } 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 b9af89e4..6fe42448 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 @@ -23,7 +23,7 @@ public class CsvReadTest { @Test public void write() throws Exception { - Appendable out = new PrintWriter(TestFileUtil.createNewFile("csvWrite.csv")); + Appendable out = new PrintWriter(TestFileUtil.createNewFile("csvWrite1.csv")); CSVPrinter printer = CSVFormat.DEFAULT.withHeader("userId", "userName") .print(out); for (int i = 0; i < 10; i++) {