输出到 CSV 时更改 java 中的分隔符

发布于 2024-10-19 10:30:43 字数 153 浏览 1 评论 0原文

我在网上或我的书中找不到任何关于此的信息,而且这是一本大书! Java 有没有办法在输出到 CSV 文件时更改默认分隔符。我想将其从逗号更改为 \t?我想这样做是因为当我放入包含逗号的字符串时,它们会转到下一个单元格,这很痛苦。

任何帮助将是最伟大的!

詹姆斯

I can't find any info on this on the net or in my book and it's a big book! Is there a way in Java to change the default delimiter when out putting to CSV files. I want to change it from a comma to \t? I want to do this because when I'm putting in string that contain a comma they go to the next cell which is a pain.

Any help would be most greatful!

James

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

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

发布评论

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

评论(3

回眸一笑 2024-10-26 10:30:43

您用什么来创建 CSV 文件?如果您只是使用基本的 Java 写入文件方法,您应该能够选择您想要的分隔符。如果您使用的是 openCSV 等第三方 API,应该有一个命令来设置分隔符。

What are you using to create CSV files? If you are just using the basic Java write-to-file method, you should be able to choose what you want for delimiter. If you are using a third party API like openCSV, there should be a command to set the delimiter.

魄砕の薆 2024-10-26 10:30:43

CSV 绝不是一个标准,Java 也没有对其的标准支持。难怪你的大书没有谈到它。

我建议使用 OpenCSV 编写 CSV。 CSVWriter 的构造函数 有一个分隔符参数。

编辑:

您在一小时前刚刚提出的问题中得到了相同的答案: 写包含逗号的 CSV

CSV is in no way a standard, and Java has no standard support for it. No wonder your big book doesn't talk about it.

I'd suggest using OpenCSV to write CSV. The CSVWriter's constructor has a separator argument.

EDIT :

You had this same answer in the question you just asked one hour ago : Write to CSV that contains comma

绝不放开 2024-10-26 10:30:43

虽然这是很晚的答案,但它可能会对某人有所帮助

只需在 CSV 字符串前附加“sep=\t”即可。

ae:在 Java Servlet 中

public class ExportCSV extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      response.setContentType("text/CSV");
      response.setHeader("Content-Disposition", "attachment; fileName=Test.csv");
      OutputStream lOutputStream = response.getOutputStream();                                              lOutputStream.write("sep=\t".getBytes());
      lOutputStream.write(System.getProperty(LINE_SEPERATOR).getBytes());
      lOutputStream.write(<CSV Byte Array>);
   }
}

注意:此代码未经测试,仅用于说明目的。

Though this is very late answer, It may help someone

Just append "sep=\t" Before CSV string.

a.e : In Java Servlet

public class ExportCSV extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      response.setContentType("text/CSV");
      response.setHeader("Content-Disposition", "attachment; fileName=Test.csv");
      OutputStream lOutputStream = response.getOutputStream();                                              lOutputStream.write("sep=\t".getBytes());
      lOutputStream.write(System.getProperty(LINE_SEPERATOR).getBytes());
      lOutputStream.write(<CSV Byte Array>);
   }
}

N.B : This code is not tested,for illustration purposes only.

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