Skip to content

Commit

Permalink
feat: add UTF-8 support for CSV exported files
Browse files Browse the repository at this point in the history
Close #89
  • Loading branch information
flang committed Jan 24, 2024
1 parent e5a3f22 commit be89f0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -72,7 +73,14 @@ public InputStream createInputStream() {
PipedOutputStream out = new PipedOutputStream(in);
new Thread(
() -> {
try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(out))) {
try (
OutputStreamWriter os = new OutputStreamWriter(out, exporter.getCsvCharset());
CSVWriter writer = new CSVWriter(os)) {
if (StandardCharsets.UTF_8.equals(exporter.getCsvCharset())) {
// write BOM
os.write(0xfeff);
}

writer.writeNext(headers);
writer.writeAll(data);
if (footers.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Comparator;
Expand Down Expand Up @@ -102,6 +103,8 @@ public class GridExporter<T> implements Serializable {

private List<FooterToolbarItem> footerToolbarItems;

private String csvCharsetName;

private GridExporter(Grid<T> grid) {
this.grid = grid;
}
Expand Down Expand Up @@ -554,4 +557,17 @@ public void setFooterToolbarItems(List<FooterToolbarItem> footerToolbarItems) {
this.footerToolbarItems = footerToolbarItems;
}

/**
* Charset to use when exporting the CSV file.
*
* @return CSV file charset or default one.
*/
public Charset getCsvCharset() {
return csvCharsetName == null ? Charset.defaultCharset() : Charset.forName(csvCharsetName);
}

public void setCsvCharset(Charset charset) {
this.csvCharsetName = charset.name();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.vaadin.flow.router.Route;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
Expand Down Expand Up @@ -81,6 +82,7 @@ public GridExporterDemo() throws EncryptedDocumentException, IOException {
exporter.setTitle("People information");
exporter.setFileName(
"GridExport" + new SimpleDateFormat("yyyyddMM").format(Calendar.getInstance().getTime()));
exporter.setCsvCharset(StandardCharsets.UTF_8);

TextField filterField = new TextField();
filterField.setPlaceholder("Filter by");
Expand Down

0 comments on commit be89f0e

Please sign in to comment.