gpt4 book ai didi

java - CSV 生成、日期和长数字格式

转载 作者:行者123 更新时间:2023-12-02 07:35:17 25 4
gpt4 key购买 nike

我正在使用 JSF 动态/运行时生成 CSV 文件,代码如下

FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = HttpServletResponse)context.getExternalContext().getResponse();

int read = 0;
byte[] bytes = new byte[1024];

response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");

ServletOutputStream os = null;

StringBuffer stringBuffer1 = new StringBuffer("");

stringBuffer1.append("Disconnect Time");
stringBuffer1.append(',');
stringBuffer1.append("Calling Number");

stringBuffer1.append("01/06/2010 01:00:35 AM");
stringBuffer1.append(", ");
stringBuffer1.append("447744369900");


ByteArrayInputStream bis1;
try {
bis1 = new ByteArrayInputStream(stringBuffer1.toString().getBytes("UTF-8"));

os = response.getOutputStream();

while ((read = bis1.read(bytes)) != -1) {
os.write(bytes, 0, read);
}

os.flush();
os.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

FacesContext.getCurrentInstance().responseComplete();

下面是打开文件时的内容

Disconnect Time Calling Number
1/6/2010 1:00 4.47744E+11

实际预期结果将是包含 AM/PM 的完整日期格式和完整长度的数字。

我已经尝试过给定的双引号示例 Excel CSV - Number cell format以及其他一些添加空格的方法,但没有成功。用户还将对数字列执行算术运算。

提前致谢。

最佳答案

我不是 JSF 专家,但这似乎完全与 Excel 如何读取此信息有关。

我尝试对数据进行一些操作,但无法使其按照您希望在 CSV 中执行的操作进行操作。

我创建了一个包含以下内容的示例 CSV 文件(使用 Nopepad++):

Disconnect Time, Calling Number
01/06/2010 01:00:35 AM,447744369900

当我使用 Excel 查看它时,它给了我与您得到的相同的输出 - 所以您面临的问题与在 Excel 中显示 CSV 有关 - 而不是 JSF。

关于java - CSV 生成、日期和长数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com