我正在使用Eclipse Kepler在JSF java项目上使用Mac Os。
如何为Windows,Mac Os,Linux环境下载文件,而不会因编码和平台而误解字符?
我们是否需要检查客户端使用哪个操作系统来使用用户代理解析来调整编码?
如果你使用UTF-8,它可以在Unix Based Os上运行,但如何在带有excel的windows中打开它?
我将此代码编写为以UTF-8导出。但它会误解é& è在Excel中。
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("text/csv");
ec.setResponseCharacterEncoding("UTF-8");
String fileName = "CleanOmicsTracer_Tableau_"+type+".csv";
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// Now you can write the InputStream of the file to the above OutputStream the usual way.
BufferedOutputStream csvOut = new BufferedOutputStream(ec.getResponseOutputStream());
BufferedWriter csvWriter = new BufferedWriter(new OutputStreamWriter(csvOut, "UTF-8"));
csvWriter.append("Etude;Identifiant Analyse;Numero d'inclusion;Identifiant Prelevement;Identifiant Extrait;Technologie;Service;Sous-analyses;Categorie;Date Enregistrement\n");
for (AnalyseDispatcher aAnalyse : viewedListAnalyse) { // somethings is your List<Something>
csvWriter
.append(toCsvField(aAnalyse.getSample().getParticipant().getProject().getStudy_name()))
.append(';')
.append(toCsvField("A"+aAnalyse.getId()))
.append(';').append(toCsvField(aAnalyse.getSample().getParticipant().getId_study())) .append('\n');
}
csvWriter.flush();
csvWriter.close();
csvOut.close();
fc.responseComplete();
}
答案 0 :(得分:0)
CSV字符流的来源是什么?
您需要确保正在读取文件的java.io.Reader
使用正确的编码(即初始文件的编码)。
答案 1 :(得分:0)
尝试:
ec.setResponseContentType("text/csv;charset=UTF-8");
或
ec.setResponseContentType("application/csv;charset=UTF-8");