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 {
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;
}
}

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

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.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();
}

1
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();
}

2
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++) {

Loading…
Cancel
Save