简体   繁体   English

在 MS Excel 中打开时,日文字符在使用 apache commons csv 生成的 csv 文件中显示为乱码

[英]When opened in MS Excel Japanese character shows as garbled character in csv file generated using apache commons csv

We are creating a CSV file, then writing to it using following piece of code (apache commons csv).我们正在创建一个 CSV 文件,然后使用以下代码 (apache commons csv) 写入该文件。 We are using UTF-8 encoding while creating the file.我们在创建文件时使用 UTF-8 编码。

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter; 

File file = new File(fileName);
Writer fileWriter = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
CSVPrinter printer = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader(columnHeadersTranslatedArray));

Then we are using following code to write the file to servlet response output stream然后我们使用以下代码将文件写入 servlet 响应 output stream

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.FileCopyUtils;

HttpServletResponse response // received as function argument
response.setContentType("text/csv; charset=utf-8");
response.setHeader("Content-Disposition","attachment; filename=" + fileName);
response.setCharacterEncoding("utf-8");
response.setContentLength((int) file.length());

File file = new File(fileName);                 
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
FileCopyUtils.copy(inputStream, response.getOutputStream());

At the end we are downloading the file in the browser after attaching it to an anchor tag as a href using following code最后,我们使用以下代码将文件作为 href 附加到锚标记后,在浏览器中下载该文件

a = document.createElement('a');
a.href = window.URL.createObjectURL(xmlhttp.response);
a.download = filename;
a.style.display = 'none';
document.body.appendChild(a);
a.click();

Now after downloading when we are opening the file using Notepad or Notepad++ it shows the Japanese character properly.现在,当我们使用记事本或 Notepad++ 打开文件时,下载后它会正确显示日文字符。 But when we are double clicking.csv file it opens in MS Excel, then it shows garbled character in place of Japanese character.但是当我们双击.csv文件时,它在MS Excel中打开,然后显示乱码而不是日文字符。 Most probably this time it uses default ANSI encoding in place of UTF-8 encoding.很可能这次它使用默认的 ANSI 编码代替 UTF-8 编码。 Please help me how that can be shown as Japanese character like Notepad.请帮助我如何将其显示为记事本之类的日语字符。 We are using the methods described in the following link to visualize the file with Japanese character in MS Excel我们正在使用以下链接中描述的方法在 MS Excel 中可视化带有日语字符的文件

https://support.tibco.com/s/article/Excel-failed-to-display-Japanese-characters-when-opening-csv-file-saved-from-Statistica-13-2-with-UTF-8-encoding https://support.tibco.com/s/article/Excel-failed-to-display-Japanese-characters-when-opening-csv-file-saved-from-Statistica-13-2-with-UTF-8-编码

Currently my understanding is above problem resolved after adding a BOM while writing to the file.目前我的理解是在写入文件时添加 BOM 后解决了上述问题。 I added the BOM (ie byte order mark) in following fashion after following statement.在下面的语句之后,我按照以下方式添加了 BOM(即字节顺序标记)。

Writer fileWriter = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
fileWriter.write('\ufeff'); //wrote BOM character
CSVPrinter printer = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader(columnHeadersTranslatedArray));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM