From 4c975e848542c2680f0c550c698a4fecd47ef46c Mon Sep 17 00:00:00 2001 From: yixian Date: Tue, 24 Dec 2019 15:30:13 +0800 Subject: [PATCH] csv charset fix --- .../core/impls/TradeLogServiceImpl.java | 3 ++ .../payment/manage/valid/CSVTest.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java index 0654b9941..15bdf7b21 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java @@ -43,6 +43,7 @@ import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; import net.sf.jasperreports.export.*; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.csv.QuoteMode; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; @@ -1542,11 +1543,13 @@ public class TradeLogServiceImpl implements TradeLogService { String[] title = {"order Id", "Client Order Id", "Transaction Time", "Channel", "Gateway", "Exchange Rate", "Transaction Type", "Currency", "Input Amount", "Total Amount", "Clearing Amount", "Surcharge Rate", "Settle Amount", "Surcharge", "GST", "Remark", "Dev No", "Company Name", "Short Name", "Settlement Date"}; CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withHeader(title); + response.setCharacterEncoding("UTF-8"); response.setContentType("application/octet-stream;"); response.addHeader("Content-Disposition", "attachment; filename=" + "Merchant_Settlement_Info_" + begin + "_" + end + ".csv"); try (PrintWriter writer = response.getWriter(); CSVPrinter printer = new CSVPrinter(writer, format)) { + printer.printRecord((Object[]) title); JSONObject clearDetailTotal = transactionMapper.getClearDetailTotal(params); diff --git a/src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java b/src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java new file mode 100644 index 000000000..4c1ad5366 --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/valid/CSVTest.java @@ -0,0 +1,29 @@ +package au.com.royalpay.payment.manage.valid; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; +import org.apache.commons.csv.QuoteMode; +import org.junit.Test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; + +/** + * Create by davep at 2019-12-24 15:15 + */ +public class CSVTest { + + @Test + public void test() throws IOException { + File file = File.createTempFile("csvtest",".csv"); + FileWriter writer = new FileWriter(file); + CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withHeader("title", "标题1", "abc");//.withQuoteMode(QuoteMode.ALL_NON_NULL); + CSVPrinter printer = new CSVPrinter(writer, format); + printer.printRecord("test", "订单1234", "hello"); + writer.flush(); + System.out.println(file.getAbsolutePath()); + } +}