Skip to content

Commit d88f76b

Browse files
authored
docs: update Chapter 3-Write (#460)
* docs: update Chapter 3 * docs: update code example in num-rows.md * docs: review the content of File csv.md
1 parent 77ec09e commit d88f76b

14 files changed

Lines changed: 1011 additions & 993 deletions

File tree

docs/zh-CN/read/csv.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# 读取 CSV 文件
2+
3+
本章节介绍如何使用 FastExcel 来读取自定义 CSV 文件。
4+
5+
## 概述
6+
7+
FastExcel 通过不同的参数设计进行 CSV 的读取。其底层使用了[Apache Commons CSV](https://commons.apache.org/proper/commons-csv),也支持通过直接设置[CSVFormat](https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html)进行设定来达成读取的目标。
8+
9+
主要的参数如下:
10+
11+
| 名称 | 默认值 | 描述 |
12+
| :--- | :--- | :--- |
13+
| `delimiter` | `,` (逗号) | 字段分隔符。推荐使用 `CsvConstant` 中预定义的常量,例如 `CsvConstant.AT` (`@`)、`CsvConstant.TAB` 等。 |
14+
| `quote` | `"` (双引号) | 字段引用符号,推荐使用 `CsvConstant` 中预定义的常量,例如 `CsvConstant.DOUBLE_QUOTE` (`"`)。 |
15+
| `recordSeparator` | `CRLF` | 记录(行)分隔符。根据操作系统不同而变化,例如 `CsvConstant.CRLF` (Windows) 或 `CsvConstant.LF` (Unix/Linux)。 |
16+
| `nullString` | `null` | 用于表示 `null` 值的字符串。注意这与空字符串 `""` 不同。 |
17+
| `escape` | `null` | 转义字符,用于转义引用符号自身。 |
18+
19+
---
20+
21+
## 参数详解与示例
22+
23+
下面将详细介绍每一个参数的用法,并提供代码示例。
24+
25+
### delimiter
26+
27+
`delimiter` 用于指定 CSV 文件中的字段分隔符。默认值为英文逗号 `,`。同时,FastExcel 提供了一些常量`CsvConstant`,用于简化使用
28+
29+
#### 代码示例
30+
如果 CSV 文件使用 `\u0000` 作为分隔符,可以如下设置:
31+
```java
32+
@Test
33+
public void delimiterDemo() {
34+
String csvFile = "path/to/your.csv";
35+
List<DemoData> dataList = FastExcel.read(csvFile, DemoData.class, new DemoDataListener())
36+
.csv()
37+
.delimiter(CsvConstant.UNICODE_EMPTY)
38+
.doReadSync();
39+
}
40+
```
41+
42+
### quote
43+
44+
`quote` 用于指定包裹字段的引用符号。默认值为双引号 `"`。当字段内容本身包含分隔符或换行符时,建议设置。
45+
46+
> 注意不可和 `recordSeparator` 的设置重复,建议结合`QuoteMode`使用
47+
48+
#### 代码示例
49+
```java
50+
@Test
51+
public void quoteDemo() {
52+
String csvFile = "path/to/your.csv";
53+
List<DemoData> dataList = FastExcel.read(csvFile, DemoData.class, new DemoDataListener())
54+
.csv()
55+
.quote(CsvConstant.DOUBLE_QUOTE, QuoteMode.MINIMAL)
56+
.doReadSync();
57+
}
58+
```
59+
60+
### recordSeparator
61+
62+
`recordSeparator` 用于指定文件中的换行符。不同操作系统的换行符可能不同(例如,Windows 使用 `CRLF`,而 Unix/Linux 使用 `LF`)。
63+
64+
#### 代码示例
65+
```java
66+
@Test
67+
public void recordSeparatorDemo() {
68+
String csvFile = "path/to/your.csv";
69+
List<DemoData> dataList = FastExcel.read(csvFile, DemoData.class, new DemoDataListener())
70+
.csv()
71+
.recordSeparator(CsvConstant.LF)
72+
.doReadSync();
73+
}
74+
```
75+
76+
### nullString
77+
78+
`nullString` 用于定义文件中代表 `null` 值的特定字符串。例如,可以将字符串 `"N/A"` 解析为 `null` 对象。
79+
80+
#### 代码示例
81+
```java
82+
@Test
83+
public void nullStringDemo() {
84+
String csvFile = "path/to/your.csv";
85+
List<DemoData> dataList = FastExcel.read(csvFile, DemoData.class, new DemoDataListener())
86+
.csv()
87+
.nullString("N/A")
88+
.doReadSync();
89+
}
90+
```
91+
92+
### escape
93+
94+
`escape` 用于指定转义字符,当引用符号(`quote`)本身出现在字段值中时,可以使用转义字符来处理。
95+
96+
#### 代码示例
97+
```java
98+
@Test
99+
public void escapeDemo() {
100+
String csvFile = "path/to/your.csv";
101+
List<DemoData> dataList = FastExcel.read(csvFile, DemoData.class, new DemoDataListener())
102+
.csv()
103+
.escape(CsvConstant.BACKSLASH)
104+
.doReadSync();
105+
}
106+
```
107+
108+
## CSVFormat设置详解与示例
109+
110+
支持直接构建一个`CSVFormat`对象。
111+
> 目前 FastExcel 仍然支持,但并非最推荐的使用方法。
112+
113+
### 代码示例
114+
115+
```java
116+
@Test
117+
public void escapeDemo() {
118+
119+
CSVFormat csvFormat = CSVFormat.DEFAULT.builder().setDelimiter(CsvConstant.AT).build();
120+
String csvFile = "path/to/your.csv";
121+
122+
try (ExcelReader excelReader = FastExcel.read(csvFile, DemoData.class, new DemoDataListener()).build()) {
123+
ReadWorkbookHolder readWorkbookHolder = excelReader.analysisContext().readWorkbookHolder();
124+
// 判断是否为CsvReadWorkbookHolder实例
125+
if (readWorkbookHolder instanceof CsvReadWorkbookHolder) {
126+
CsvReadWorkbookHolder csvReadWorkbookHolder = (CsvReadWorkbookHolder) readWorkbookHolder;
127+
csvReadWorkbookHolder.setCsvFormat(csvFormat);
128+
}
129+
130+
ReadSheet readSheet = FastExcel.readSheet(0).build();
131+
excelReader.read(readSheet);
132+
}
133+
}
134+
```

docs/zh-CN/read/num-rows.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public void allSheetRead() {
2929
```java
3030
@Test
3131
public void singleSheetRead() {
32-
try (ExcelReader excelReader = EasyExcel.read(fileName, DemoData.class, new DemoDataListener()).build()) {
33-
ReadSheet readSheet = EasyExcel.readSheet(0).build();
32+
try (ExcelReader excelReader = FastExcel.read(fileName, DemoData.class, new DemoDataListener()).build()) {
33+
ReadSheet readSheet = FastExcel.readSheet(0).build();
3434
readSheet.setNumRows(100); // 读取前100行
3535
excelReader.read(readSheet);
3636
}

docs/zh-CN/read/read-csv.md

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)