CSV jsp生成文件,如何插入换行符

发布于 2024-09-18 13:26:43 字数 543 浏览 3 评论 0原文

我正在尝试使用 jsp 文件生成 csv 文件,其代码如下:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- Set the content type --%>
<%@ page contentType="text/csv"%>

<c:forEach items="${chartData}" var="chartDataEntry" varStatus="status">
 ${chartDataEntry.date}, ${chartDataEntry.open}, ${chartDataEntry.high}, ${chartDataEntry.low}, ${chartDataEntry.close}, ${chartDataEntry.volume}
</c:forEach>

它非常简单,并且工作正常。问题是我需要在每行后面插入一条断线,但我无法让它工作。我尝试过通常的做法: \n \r\n 等等。

有什么想法吗?

I am trying to generate a csv file using a jsp file this its code:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%-- Set the content type --%>
<%@ page contentType="text/csv"%>

<c:forEach items="${chartData}" var="chartDataEntry" varStatus="status">
 ${chartDataEntry.date}, ${chartDataEntry.open}, ${chartDataEntry.high}, ${chartDataEntry.low}, ${chartDataEntry.close}, ${chartDataEntry.volume}
</c:forEach>

Its quite simple, and it works fine. The problem is that I need to insert a breakline after each row, but I can't get it working. I have tried with usuals: \n \r\n and so on.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2024-09-25 13:26:44

在 JSP 中,有太多的环境因素可能会导致这种情况的发生。 JSP 本身也会发出一些“无意的”空格和其他字符。此外,trimSpaces 设置可能会占用一些空格/换行符。通常认为(滥用)将 JSP 用于纯 HTML 以外的其他用途是一种不好的做法。

而是使用 servlet 来获得健壮且一致的 CSV 输出:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType("text/csv");
    PrintWriter writer = response.getWriter();
    for (ChartDataEntry chartDataEntry : chartData) {
        writer.append(chartDataEntry.getDate()).append(',');
        writer.append(chartDataEntry.getOpen()).append(',');
        writer.append(chartDataEntry.getHigh()).append(',');
        writer.append(chartDataEntry.getLow()).append(',');
        writer.append(chartDataEntry.getClose()).append(',');
        writer.append(chartDataEntry.getVolume()).println();
    }
}

In a JSP there are too much environmental factors which can malform this. The JSP itself also emits some "unintentional" whitespace and other characters. Also, the trimSpaces setting may eat some whitespace/newlines. It's generally considered a poor practice to (ab)use JSP for something else than plain HTML.

Rather use a servlet instead for a robust and consistent CSV output:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType("text/csv");
    PrintWriter writer = response.getWriter();
    for (ChartDataEntry chartDataEntry : chartData) {
        writer.append(chartDataEntry.getDate()).append(',');
        writer.append(chartDataEntry.getOpen()).append(',');
        writer.append(chartDataEntry.getHigh()).append(',');
        writer.append(chartDataEntry.getLow()).append(',');
        writer.append(chartDataEntry.getClose()).append(',');
        writer.append(chartDataEntry.getVolume()).println();
    }
}
恰似旧人归 2024-09-25 13:26:44

您提供的代码应该生成新行(可能是其中几行)。确定不是渲染的问题? (即您在浏览器中查看它,并且由于没有
标记,因此输出呈现在一行上?

The code you supplied should generate new lines (probably several of them). Are you sure it isn't a matter of rendering? (i.e. you're looking at it in a browser, and since there is no
tag, the output is rendered on a single line?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文