首页 文章

Spring Batch CSV转换

提问于
浏览
1

我在spring批处理txt文件到csv转换,我正在使用管道分离的文本文件,我的问题是当我尝试写入csv如果字段遇到逗号在csv中创建一个新字段,即如果我有地址像|印度孟买|在文本文件中它创建了两个字段,即孟买和印度的CSV文件,而不是它应该写印度孟买作为单个字段 .

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>

2 回答

  • 1

    您将要扩展 DelimitedLineAggregator 并将包含 , 的每个字段包装在双引号 " 中 . 此外,对于包含逗号和双qoute的任何字段,您将希望使用另一个双引号来转义每个双引号,如下所示:

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

    仅供参考:这是Excel处理CSV文档的方式

  • 4

    它发生在我之前,试着用Double Quotes包围它
    示例:

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

    希望这可以帮助

相关问题