Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jul 27, 2020
2 parents e37d04b + af3dc15 commit b4e2215
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ Writer.create()
- `bufferSize(int)`:写入一个 XLSX 格式的文件时缓冲大小,默认为 100,建议不修改
- `withRaw()`:自定义写入行,启用该配置后不会根据集合数据写 Excel
- `createRow(int)``withRaw` 启用后可使用该 API,用于自定义创建 `Row``Cell`
- `isAppend(boolean)`: 默认为false,设置为true文件写入将使用追加而不是覆盖
- `to(File)`:写入 Excel 文档到文件
- `to(OutputStream)`:写入 Excel 文档到 OutputStream

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/io/github/biezhi/excel/plus/Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public class Writer {

private Charset charset = StandardCharsets.UTF_8;

/**
* if <code>true</code>, then bytes will be written to the end of the file rather than the beginning
*/
private boolean isAppend = false;

public static Writer create() {
return new Writer(ExcelType.XLSX);
}
Expand Down Expand Up @@ -256,6 +261,11 @@ public Writer charset(Charset charset) {
return this;
}

public Writer isAppend(boolean isAppend) {
this.isAppend = isAppend;
return this;
}

/**
* Write an Excel document to a file
*
Expand All @@ -264,7 +274,7 @@ public Writer charset(Charset charset) {
*/
public void to(File file) throws WriterException {
try {
this.to(new FileOutputStream(file));
this.to(new FileOutputStream(file, isAppend));
} catch (FileNotFoundException e) {
throw new WriterException(e);
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/io/github/biezhi/excel/plus/WriterTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.biezhi.excel.plus;

import io.github.biezhi.excel.plus.enums.ExcelType;
import io.github.biezhi.excel.plus.exception.WriterException;
import org.junit.Test;

Expand Down Expand Up @@ -161,4 +162,10 @@ public void testWriteCSV() throws WriterException {
deleteTempFile(fileName);
}

@Test
public void testAppendWriteCSV() throws WriterException {
String fileName = "append_write_csv_test.csv";
Writer.create(ExcelType.CSV).withRows(buildData()).isAppend(true).to(new File(fileName));
}

}

0 comments on commit b4e2215

Please sign in to comment.