-
Notifications
You must be signed in to change notification settings - Fork 373
[Feature] add support for a custom CSVFormat #353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d7d760f
feat(read): add support for custom CSVFormat
delei 5e981b3
test: add unit tests for CSVFormat parameter
delei 493223f
Merge branch 'main' into feature/custom-CSVFormat
delei ec3578b
fix: resolve ReadWorkbook conflict
delei e753410
refactor: simplify csvFormat initialization in CsvReadWorkbookHolder
delei 5ca54be
Merge branch 'fast-excel:main' into feature/custom-CSVFormat
delei aa60a02
feat(csv): add CSV constant definitions
delei 1b929d6
feat(read): add CSV support and refactor ExcelReaderBuilder
delei cccaac8
feat(write): add CSV support and refactor ExcelWriterBuilder
delei 75c5234
test(csv): refactor CSV read/write tests
delei 16513fb
test(csv): refactor testHolder() delimiter with CsvConstant
delei 85a0609
refactor: add quote method to set quote character and default QuoteMode
delei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
fastexcel-test/src/test/java/cn/idev/excel/test/temp/csv/CsvFormatTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cn.idev.excel.test.temp.csv; | ||
|
||
import cn.idev.excel.ExcelReader; | ||
import cn.idev.excel.FastExcel; | ||
import cn.idev.excel.read.metadata.holder.ReadWorkbookHolder; | ||
import cn.idev.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; | ||
import cn.idev.excel.test.util.TestFileUtil; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.QuoteMode; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
public class CsvFormatTest { | ||
|
||
private static File csvSimpleFile; | ||
private static File csvSimpleDelimiterFile; | ||
private static File csvSimpleQuoteFile; | ||
|
||
@BeforeAll | ||
public static void init() { | ||
csvSimpleFile = TestFileUtil.readFile("csv" + File.separator + "simple.csv"); | ||
csvSimpleDelimiterFile = TestFileUtil.readFile("csv" + File.separator + "simple-delimiter.csv"); | ||
csvSimpleQuoteFile = TestFileUtil.readFile("csv" + File.separator + "simple-quote.csv"); | ||
} | ||
|
||
@Test | ||
public void testReadSimple() { | ||
List<CsvData> dataList = FastExcel.read(csvSimpleFile, CsvData.class, new CsvDataListener()) | ||
.csvFormat(null) | ||
.doReadAllSync(); | ||
Assertions.assertEquals(10, dataList.size()); | ||
} | ||
|
||
@Test | ||
public void testReadDelimiter() { | ||
char delimiter = '#'; | ||
// setting the CsvFormat of ExcelReader | ||
try (ExcelReader excelReader = FastExcel.read(csvSimpleDelimiterFile, CsvData.class, new CsvDataListener()).build()) { | ||
ReadWorkbookHolder readWorkbookHolder = excelReader.analysisContext().readWorkbookHolder(); | ||
if (readWorkbookHolder instanceof CsvReadWorkbookHolder) { | ||
CsvReadWorkbookHolder csvReadWorkbookHolder = (CsvReadWorkbookHolder) readWorkbookHolder; | ||
csvReadWorkbookHolder.setCsvFormat(CSVFormat.DEFAULT.builder().setDelimiter(delimiter).build()); | ||
} | ||
excelReader.readAll(); | ||
} | ||
|
||
// use parameter | ||
List<CsvData> dataList = FastExcel.read(csvSimpleDelimiterFile, CsvData.class, new CsvDataListener()) | ||
.csvFormat(CSVFormat.DEFAULT.builder().setDelimiter(delimiter).build()) | ||
.doReadAllSync(); | ||
Assertions.assertEquals(10, dataList.size()); | ||
} | ||
|
||
@Test | ||
public void testReadQuote() { | ||
List<CsvData> dataList = FastExcel.read(csvSimpleQuoteFile, CsvData.class, new CsvDataListener()) | ||
.csvFormat(CSVFormat.DEFAULT.builder().setQuoteMode(QuoteMode.MINIMAL).build()) | ||
.doReadAllSync(); | ||
Assertions.assertEquals(10, dataList.size()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
fastexcel-test/src/test/resources/csv/simple-delimiter.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
字符串标题#日期标题#数字标题 | ||
String0#2020-01-01 01:01:00#1 | ||
String1#2020-01-02 01:01:00#2 | ||
String2#2020-01-03 01:01:00#3 | ||
String3#2020-01-04 01:01:00#4 | ||
String4#2020-01-05 01:01:00#5 | ||
String5#2020-01-06 01:01:00#6 | ||
String6#2020-01-07 01:01:00#7 | ||
String7#2020-01-08 01:01:00#8 | ||
String8#2020-01-09 01:01:00#9 | ||
String9#2020-01-10 01:01:00#10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"字符串标题","日期标题","数字标题" | ||
"String0","2020-01-01 01:01:00",1 | ||
"String1","2020-01-02 01:01:00",2 | ||
"String2""quote""","2020-01-03 01:01:00",3 | ||
"String3,,","2020-01-04 01:01:00",4 | ||
"String4","2020-01-05 01:01:00",5 | ||
"String5","2020-01-06 01:01:00",6 | ||
"String6","2020-01-07 01:01:00",7 | ||
"String7","2020-01-08 01:01:00",8 | ||
"String8","2020-01-09 01:01:00",9 | ||
"String9","2020-01-10 01:01:00",10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
字符串标题,日期标题,数字标题 | ||
String0,2020-01-01 01:01:00,1 | ||
String1,2020-01-02 01:01:00,2 | ||
String2,2020-01-03 01:01:00,3 | ||
String3,2020-01-04 01:01:00,4 | ||
String4,2020-01-05 01:01:00,5 | ||
String5,2020-01-06 01:01:00,6 | ||
String6,2020-01-07 01:01:00,7 | ||
String7,2020-01-08 01:01:00,8 | ||
String8,2020-01-09 01:01:00,9 | ||
String9,2020-01-10 01:01:00,10 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.