Browse Source

跑通csv 写

developing
Jiaju Zhuang 3 years ago
parent
commit
fbb79df74c
  1. 12
      src/main/java/com/alibaba/excel/csv/CsvCell.java
  2. 11
      src/main/java/com/alibaba/excel/csv/CsvRow.java
  3. 5
      src/main/java/com/alibaba/excel/csv/CsvSheet.java
  4. 1
      src/main/java/com/alibaba/excel/csv/CsvWorkbook.java
  5. 2
      src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

12
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 { public class CsvCell implements Cell {
private String value;
@Override @Override
public int getColumnIndex() { public int getColumnIndex() {
return 0; return 0;
@ -75,11 +77,12 @@ public class CsvCell implements Cell {
@Override @Override
public void setCellValue(double value) { public void setCellValue(double value) {
this.value = value + "";
} }
@Override @Override
public void setCellValue(Date value) { public void setCellValue(Date value) {
this.value = value.toString();
} }
@ -100,7 +103,7 @@ public class CsvCell implements Cell {
@Override @Override
public void setCellValue(String value) { public void setCellValue(String value) {
this.value = value;
} }
@Override @Override
@ -140,7 +143,7 @@ public class CsvCell implements Cell {
@Override @Override
public String getStringCellValue() { public String getStringCellValue() {
return null; return value;
} }
@Override @Override
@ -222,4 +225,7 @@ public class CsvCell implements Cell {
public boolean isPartOfArrayFormulaGroup() { public boolean isPartOfArrayFormulaGroup() {
return false; return false;
} }
} }

11
src/main/java/com/alibaba/excel/csv/CsvRow.java

@ -1,6 +1,8 @@
package com.alibaba.excel.csv; package com.alibaba.excel.csv;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -15,10 +17,13 @@ import org.apache.poi.ss.usermodel.Sheet;
*/ */
public class CsvRow implements Row { public class CsvRow implements Row {
public List<CsvCell> list = new ArrayList<>();
@Override @Override
public Cell createCell(int column) { public Cell createCell(int column) {
return new CsvCell(); CsvCell cell = new CsvCell();
list.add(cell);
return cell;
} }
@Override @Override
@ -43,7 +48,7 @@ public class CsvRow implements Row {
@Override @Override
public Cell getCell(int cellnum) { public Cell getCell(int cellnum) {
return null; return list.get(cellnum);
} }
@Override @Override
@ -58,7 +63,7 @@ public class CsvRow implements Row {
@Override @Override
public short getLastCellNum() { public short getLastCellNum() {
return 0; return (short)list.size();
} }
@Override @Override

5
src/main/java/com/alibaba/excel/csv/CsvSheet.java

@ -9,6 +9,7 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
@ -668,7 +669,9 @@ public class CsvSheet implements Sheet {
public void flushData() { public void flushData() {
try { try {
csvPrinter.printRecord(); for (CsvRow row : rowCache) {
csvPrinter.printRecord(row.list.stream().map(CsvCell::getStringCellValue).collect(Collectors.toList()));
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

1
src/main/java/com/alibaba/excel/csv/CsvWorkbook.java

@ -177,6 +177,7 @@ public class CsvWorkbook implements Workbook {
@Override @Override
public void write(OutputStream stream) throws IOException { public void write(OutputStream stream) throws IOException {
csvSheet.flushData();
csvSheet.csvPrinter.flush(); csvSheet.csvPrinter.flush();
csvSheet.csvPrinter.close(); csvSheet.csvPrinter.close();
} }

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

@ -23,7 +23,7 @@ public class CsvReadTest {
@Test @Test
public void write() throws Exception { 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") CSVPrinter printer = CSVFormat.DEFAULT.withHeader("userId", "userName")
.print(out); .print(out);
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {

Loading…
Cancel
Save