简体   繁体   English

如何在Java中解析CSV(excel,不用逗号分隔)文件?

[英]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. 我有一个CSV文件(excel),其中包含数据,我需要使用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. 这些文件中的数据未使用逗号分隔,CSV文件具有写入所有数据的列数和行数(每个单元格都有数据)。 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. 我需要遍历所有文件,直到到达每个文件的EOF(文件结尾)并解析数据。 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. 文件中还包含空行,因此空行不是停止解析的条件,我认为只有EOF会表明我已经到达特定文件的末尾。

many thanks. 非常感谢。

You can use opencsv to parse the excel CSV. 您可以使用opencsv解析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/ 您可以在这里找到信息: http : //opencsv.sourceforge.net/

And to parse the excelCSV you can do: 要解析excelCSV,您可以执行以下操作:

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

Aside from other suggestions, I would offer Jackson CSV module . 除了其他建议,我还将提供Jackson CSV模块 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). Jackson具有非常强大的数据绑定功能,并且CSV模块允许以CSV格式进行读/写,以替代JSON(或XML,YAML和其他受支持的格式)。 So you can also do conversions between other data formats, in addition to powerful CSV-to/from-POJO binding. 因此,除了强大的CSV-to / from-POJO绑定之外,您还可以在其他数据格式之间进行转换。

Please have a Stream Object to read the CSV file. 请具有一个流对象以读取CSV文件。

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

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

Read an inputstream Object and store the file in String object. 读取inputstream对象并将文件存储在String对象中。

Then using StringTokenizer with ,[comma] as delimeter -->you will get the tokens Please manipulate the token to get the value. 然后,使用StringTokenizer,以[[逗号]作为分隔符->,您将获得令牌。请操纵令牌以获取值。

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

StringTokenizer st = new StringTokenizer(str); StringTokenizer st =新的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 帕万

Suppose you have a csv fileContent in form of string: 假设您有一个csv fileContent字符串形式:

String fileContent;

Generally, the CSV fileContent are parsed into List>. 通常,将CSV fileContent解析为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 然后使用OpenCSV的CSVParser并将逗号分隔的行解析为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");
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM