Escaping CSV comma in Java(Spring Boot) - spring-boot

I am looking for a way to escape the comma within a cell of a .csv file. I tried to achieve this with 'Opencsv', but could not get it to work.
I have a custom spreadsheet/grid in my Springboot application and a user can view the .csv file they upload(as a MultipartFile) in the grid. If the cell contains a comma, it messes the grid.
What I need to do is to escape the comma within the cell of a (comma separated) .csv file. What is the best way to achieve this requirement? Is there a way to escape characters from a MultipartFile with a minimum number of conversions/transactions?
Thanks in advance!

Related

Does a single column CSV file have commas?

When i open my csv file in an excel it looks like this -
Header
Value1
Value2
Value3
Value4
Value5
I want to know whether this file actually has commas in it? I am aware that if i have multiple columns i will see the commas
You can easily test that by opening the file in a text editor (e.g. Notepad on Windows). It will show the file as it is in text format, i.e., with commas present (if they are in the file). I would say that if it is single column, it won't have commas (but rather line breaks between the rows), but if you need to be sure just open it with a text editor.
https://www.ietf.org/rfc/rfc4180.txt
Given there is only one value in each record it would not have a comma given the spec.
Within the header and each record, there may be one or more
fields, separated by commas. Each line should contain the same
number of fields throughout the file. Spaces are considered part
of a field and should not be ignored. The last field in the
record must not be followed by a comma. For example:

How To Process A Sentence In Special Format In PLSQL

I would like to decode the content of csv in PLSQL.
The content of the csv as following.
"HEADER_ROW1_COL1,WITH_COMMA",HEADER_ROW1_COL2,my darling's ccc
DATA_ROW2_COL1,DATA_ROW2_COL2
DATA_ROW3_COL1,DATA_ROW3_COL2
CSV will split the column with a comma. If the cell contains a comma, CSV will enclosed the content of the cell with double quotes.
Threfore, if I want to read the content of the Excel file, I need to follow the steps as following
Read the content of the CSV by line
Split the text with comma
If the comma is enclosed by two double quotes, ignore the comma
Please advise.
Thanks

Reading Text file with fixed length columns using spring batch

I need to read a text file using spring batch process and bellow is a sample file
000115989 AB0001 BC00012 030114 010100 WITHDRAWL FROM SAVING 100.00
It doesn't have any column header and each column has a fixed length and delimited by two blank spaces.
Here I can't use DelimitedLineTokenizer for two blank spaces as columns can also have leading or trailing blank spaces .
Is there any work around so that I read each column with its specific length after that I can trim that.
Take a look at the FixedLengthTokenizer (http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/transform/FixedLengthTokenizer.html). This allows you to set how lines are parsed by column instead of by delimiter.

How do I export a spreadsheet (csv) in excel using ascii control characters as the delimiters?

I have this csv file that I would like to parse with Ruby. The file's data is a cluster with commas and new lines in the fields but Excel still reads it properly. If the file could be exported from excel using the unit and record separators as the delimiters for the columns and rows, I'd be golden.
Anybody know how to specify those characters in excel? Thanks!
Use Ruby CSV with this option:
:col_sep
The String placed between each field. This String will be transcoded
into the data’s Encoding before parsing.
See more here: http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html
I ended up having Google Sheets export the file as json. Steps I followed here There were 10,000 records and the browser tab crashed when it tried to do all of them. So I had to piece meal it. I'm sure there's a better way to do it.

csv parsing issue when data include comma

I am retrieving data from DB and make each field merged with Comma between them to generate CSV.
But the problem is one the field is Company Name and the data includes comma which leads to malformed CSV file.
Example: Name, Telephone, Email
AAA, 12345, aaa#mail.com
BBB Co,.Ltd, 43466, bbb#gmail.com
For the record BBB the generated CSV becomes problem as it includes , in the data.
How should I make the correct CSV for such records of including , ?
Most of the developers handle this situation by using different characters instead of "comma". But i would suggest you to look into an old post here
Dealing with commas in a CSV file
Is your question related to Salesforce APEX?
When the CSV was generated there ought to be an option to enclose the fields in Double quotes so that commas can appear inside the field content. For example "Company, Name","1234","etc."
The CSV generator will also "escape" any double quotes inside a field like this "Some field with \"double\" quotes","123","etc"
This all means you need a CSV parser that can handle these situations.
If your question is related to Salesforce APEX then it is quite difficult to build such a CSV parser because of the limitations Salesforce imposes on the number of statements that can run in any given action.

Resources