Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV generation possible with Apache POI?

Tags:

apache-poi

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

like image 905
reddyvaribabu Avatar asked Feb 22 '12 19:02

reddyvaribabu


People also ask

Can we use Apache POI for CSV?

Apache POI was never designed to call on CSV files. While a CSV File may be opened in Excel, Excel has its own reader that does an auto import.

How create CSV file from Excel in Java?

You may try Aspose. Cells for Java. You can use this component to open a CSV file and save it as XLS file using Java. It also helps you work with different Excel file versions.

What is difference between HSSFWorkbook and XSSFWorkbook?

HSSFWorkbook − This class has methods to read and write Microsoft Excel files in . xls format. It is compatible with MS-Office versions 97-2003. XSSFWorkbook − This class has methods to read and write Microsoft Excel and OpenOffice xml files in .

How do I view a CSV file?

If you already have Microsoft Excel installed, just double-click a CSV file to open it in Excel. After double-clicking the file, you may see a prompt asking which program you want to open it with. Select Microsoft Excel. If you are already in Microsoft Excel, you can choose File > Open and select the CSV file.


1 Answers

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

like image 168
gregshap Avatar answered Nov 13 '22 01:11

gregshap