user2153847
user2153847

Reputation: 33

Spring Batch CSV conversion

I am working on txt file to csv conversion in spring Batch, a am using pipe seperated text file and my problem is when i try to write into csv if a field encounters comma a new field is created in csv, i.e if i have address like |Mumbai,India| in text file it creates two fields i.e Mumbai and India in CSV file instead it should write Mumbai,India as single field.

CSV ItemWriter

<bean id="ABCWriters" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource" value="file:output/ABC.csv"></property>
    <property name="shouldDeleteIfExists" value="true"></property>

    <property name="lineAggregator">    
        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
            <property name="delimiter" value=","></property>

            <property name="fieldExtractor">
                  <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <property name="names" value="address, city" />
                   </bean>
            </property>         
        </bean>          
    </property>             
</bean>

Upvotes: 1

Views: 2557

Answers (2)

Dean Clark
Dean Clark

Reputation: 3868

You'll want to extend the DelimitedLineAggregator and wrap each field that contains a , to be surrounded in double-quotes ". Additionally, for any field that contains a comma AND a double-qoute, you'll want to escape each double-quote with another double-quote like so:

some value          -> some value
some, value         -> "some, value"
some, "other" value -> "some, ""other"" value"

FYI: this is how Excel handles CSV documents

Upvotes: 5

Rahal Danthanarayana
Rahal Danthanarayana

Reputation: 748

It happens to me before, Try to surround it with Double Quotes
Example :

new_content = "\"" + review_content.text() + "\"";

Hope this helps

Upvotes: 1

Related Questions