使用 Apache POI 可以生成 CSV 吗? [英] CSV generation possible with Apache POI?

查看:40
本文介绍了使用 Apache POI 可以生成 CSV 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成 csv 文件,我偶然发现了我们项目中的一个模块,该模块使用 Apache POI 提前生成 Excel 表格.所以我想我可以用它来生成 csv.所以我问了谷歌兄弟,但他找不到任何肯定说 Apache POI 可以用于 CSV 文件生成的内容.我也在检查以下 api,它只在任何地方谈论 xls 表而不是 csv.有什么想法吗?

http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Workbook.html

解决方案

Apache Poi 不会为您输出到 CSV.但是,您有几个不错的选择,具体取决于您要写入 csv 的数据类型.

如果您知道没有一个单元格将包含 csv 标记,例如逗号、引号、行尾,那么您可以遍历数据行并将文本复制到 StringBuffer 中,然后将其发送到常规的 Java IO.这是一个沿着这些行向 csv 写入 sql 查询的示例:Poi 邮件列表:编写 CSV

否则,与其自己弄清楚如何转义特殊字符,不如查看 opencsv 项目

I need to generate csv files and I stumbled on a module in our project itself which uses Apache POI to generate excel sheets aleady. So I thought I could use the same to generate csv. So I asked google brother, but he couldnt find anything for sure that says Apache POI can be used for CSV file generation. I was checking on the following api too and it only talks about xls sheets and not csv anywhere. Any ideas?

http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Workbook.html

解决方案

Apache Poi will not output to CSV for you. However, you have a couple good options, depending on what kind of data you are writing into the csv.

If you know that none of your cells will contain csv markers such as commas, quotes, line endings, then you can loop through your data rows and copy the text into a StringBuffer and send that to regular java IO. Here is an example of writing an sql query to csv along those lines: Poi Mailing List: writing CSV

Otherwise, rather than figure out how to escape the special characters yourself, you should check out the opencsv project

这篇关于使用 Apache POI 可以生成 CSV 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆