forked from tabulapdf/tabula-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVWriter.java
More file actions
45 lines (35 loc) · 1.06 KB
/
CSVWriter.java
File metadata and controls
45 lines (35 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package technology.tabula.writers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVFormat;
import technology.tabula.RectangularTextContainer;
import technology.tabula.Table;
public class CSVWriter implements Writer {
public CSVWriter() {
this(CSVFormat.EXCEL);
}
protected CSVWriter(CSVFormat format) {
this.format = format;
}
private final CSVFormat format;
@Override
public void write(Appendable out, Table table) throws IOException {
write(out, Collections.singletonList(table));
}
@Override
public void write(Appendable out, List<Table> tables) throws IOException {
try (CSVPrinter printer = new CSVPrinter(out, format)) {
for (Table table : tables) {
for (List<RectangularTextContainer> row : table.getRows()) {
List<String> cells = new ArrayList<>(row.size());
for (RectangularTextContainer<?> tc : row) cells.add(tc.getText());
printer.printRecord(cells);
}
}
printer.flush();
}
}
}