3

I have a CSV file(excel) in which each data is available in cell like excel. I'm using Apache CSV to parse excel type csv. While parsing the data my each character is getting separated by null character in between. This CSV file I'm getting from different source but when I'm copying the same excel csv file data and making a excel csv file manually, my below code able to get the desired output.

My CSV Excel:
MyCSVExcel.jpg

My Code:

InputStream is =null;
Reader in =null;
is = new ByteArrayInputStream(excelCSVFile);

in = new InputStreamReader(is);

CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader("EMPLOYEE_ID","NAME","DOJ",
                    "MOBILE_NO").withSkipHeaderRecord(true).withTrim(true));

List<CSVRecord> records = parser.getRecords();

for (CSVRecord record : records) {

System.out.println("Employee Id::"+record.get("EMPLOYEE_ID"));

System.out.println("Employee Name::"+record.get("NAME"));

}

I'm getting output here by above code is:
MyOutput

When I checked the ASCII value for blank character I got '0' as value means Null character.

I was looking to get output like this:
ActuallOutput

Supriya
  • 290
  • 4
  • 15
Pat's
  • 43
  • 6
  • 1
    Is this ASCII or Unicode ? This looks like a encoding problem – Marged Mar 28 '18 at 14:36
  • @Marged is right. In addition to the CVS metadata (header, column types, …), to read any type of text file, you need to know the character encoding used by the writer. The sender should communicate that to you and you should retain and use it or it's a failed communication. – Tom Blodget Mar 28 '18 at 16:22
  • Hey Guys, How did CSVFormt.EXCEL worked for you, It's not working for me at all. Can you please checkout this question I posted https://stackoverflow.com/questions/67964627/how-to-read-excel-xlsx-multipartfile-using-apache-csv-parser-current-approac – Deepanshu Rathi Jun 14 '21 at 07:19

1 Answers1

0
is = new FileInputStream(excelCSVFile);

instead of

is = new ByteArrayInputStream(excelCSVFile);

Note also that the fields are case-sensitive.

Alan Deep
  • 2,037
  • 1
  • 14
  • 22