我正在尝试从 java 编写一个 csv 文件。完美运行,但是当我将例如 19,4 写入 csv 文件时,在 excel 中它显示为 19. April。所以我在19,4前面放了一个\'。值现在显示为数字,但我前面有 '。当我单击 excel 中的列并输入 => 时,' 消失了。我该怎么办,'从一开始就没有显示?
这是一些示例源(来自 servlet):
公共无效doWork(HttpServletRequest请求,HttpServletResponse响应){
byte[] fileData = null;
String buffer = "";
buffer += "First";
buffer += ";";
buffer += "\'";
buffer += "19,4";
buffer += ";";
buffer += "\n";
buffer += "Second";
buffer += ";";
buffer += "\'";
buffer += "15,1";
buffer += ";";
buffer += "\n";
buffer += "Third";
buffer += ";";
buffer += "\'";
buffer += "1,7";
buffer += ";";
buffer += "\n";
fileData = buffer.getBytes("UTF-8");
byte[] fileDataWithBOM = new byte[fileData.length+10];
fileDataWithBOM[0] = (byte)0xef;
fileDataWithBOM[1] = (byte)0xbb;
fileDataWithBOM[2] = (byte)0xbf;
System.arraycopy(fileData, 0, fileDataWithBOM, 3, fileData.length);
ServletOutputStream out = null;
String fileName = "Accounting.csv";
try {
response.setContentType("text/csv");
String disposition = "attachment; fileName="+fileName;
response.setHeader("Content-Disposition", disposition);
response.setCharacterEncoding("UTF-8");
out = response.getOutputStream();
int length = fileData.length;
response.setContentLength(length);
out.write(fileData);
out.flush();
out.close();
} catch (Exception e) {
throw e;
} finally {
try {
if (out != null)
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}