CSV jsp生成文件,如何插入换行符
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 JSP 中,有太多的环境因素可能会导致这种情况的发生。 JSP 本身也会发出一些“无意的”空格和其他字符。此外,
trimSpaces
设置可能会占用一些空格/换行符。通常认为(滥用)将 JSP 用于纯 HTML 以外的其他用途是一种不好的做法。而是使用 servlet 来获得健壮且一致的 CSV 输出:
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:
您提供的代码应该生成新行(可能是其中几行)。确定不是渲染的问题? (即您在浏览器中查看它,并且由于没有
标记,因此输出呈现在一行上?
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?