user1864229
user1864229

Reputation: 53

how can I parse CSV(excel,not separated by comma) file in Java ?

I have a CSV files (excel) which has data in it and i need to parse the data using java. the data in those files doesn't separated using comma,the CSV files has number of columns and number of rows(each cell has data) where all the data is written. i need to go through on all the files until i get to the EOF(end of file)of each file and parse the data. the files contains also empty rows in it so empty row is not a criteria to stop parsing,i think only EOF will indicate that i've reached to the end of the specific file.

many thanks.

Upvotes: 1

Views: 12845

Answers (4)

harshit modani
harshit modani

Reputation: 141

Suppose you have a csv fileContent in form of string:

String fileContent;

Generally, the CSV fileContent are parsed into List>.

final List<String> rows = new ArrayList<String>(Lists.newArraysList(fileContent.split("[\\r\\n]+")));

Split the file into List of rows. Then use CSVParser of OpenCSV and parse the comma separated line into List

final CSVParser parser = new CSVParser();
final List<List<String>> csvDetails = new ArrayList<List<String>>();
    rows.forEach(t -> {
        try {
            csvDetails.add(Lists.newArrayList(parser.parseLine(t)));
            } catch (Exception e) {
                throw new RunTimeException("Exception occurred while parsing the data");
            }
        });

Upvotes: 0

StaxMan
StaxMan

Reputation: 116522

Aside from other suggestions, I would offer Jackson CSV module. Jackson has very powerful data-binding functionality, and CSV module allows reading/writing as CSV as an alternative to JSON (or XML, YAML, and other supported formats). So you can also do conversions between other data formats, in addition to powerful CSV-to/from-POJO binding.

Upvotes: 1

Pavan
Pavan

Reputation: 1289

Please have a Stream Object to read the CSV file.

FileInputStream fis = new FileInputStream("FileName.CSV");

BufferedInputStream bis = new BufferedInputStream(fis); InputStreamReader isr = new InputStreamReader(bis);

Read an inputstream Object and store the file in String object.

Then using StringTokenizer with ,[comma] as delimeter -->you will get the tokens Please manipulate the token to get the value.

String str = "This is String , split by StringTokenizer, created by mkyong";

StringTokenizer st = new StringTokenizer(str);

    System.out.println("---- Split by space ------");
    while (st.hasMoreElements()) {
        System.out.println(st.nextElement());
    }

    System.out.println("---- Split by comma ',' ------");
    StringTokenizer st2 = new StringTokenizer(str, ",");

    while (st2.hasMoreElements()) {
        System.out.println(st2.nextElement());
    }

Thanks,

Pavan

Upvotes: 0

Lyrion
Lyrion

Reputation: 426

You can use opencsv to parse the excel CSV. I've used this myself, all you need to do is split on the ';'. Empty cells will be parsed aswell.

You can find info here : http://opencsv.sourceforge.net/

And to parse the excelCSV you can do:

 CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), ';');

Upvotes: 2

Related Questions