即使我也遇到过相同的情况,即一个包含逗号的文本滑入一个新单元格。我更改了我的 XML 并添加了一个 CustomDelimitedLineAggregator。
eg:) 我的 XML 将具有以下代码:-
<property name="lineAggregator">
<bean
class="com.CustomDelimitedLineAggregator">
<property name="delimiter" value="|" />
<property name="fieldExtractor">
<bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names"
value="entityHashKey,entityLocationClli,entityLocationName,entityLocationType,entitySCISCode,entityStateCode,entityCountryCode,entityRegion" />
</bean>
</property>
</bean>
</property>
</bean>
还创建了自定义类(即 CustomDelimitedLineAggregator.java),如下所示:-
包 com;
导入 org.springframework.batch.item.file.transform.ExtractorLineAggregator;
导入 org.springframework.util.StringUtils;
公共类 CustomDelimitedLineAggregator 扩展 ExtractorLineAggregator {
私有字符串分隔符;
public CustomDelimitedLineAggregator() {
this.delimiter = ",";
}
public void setDelimiter(String delimiter) {
this.delimiter = delimiter;
}
@Override
public String doAggregate(Object[] fields) {
String arrregateVal = StringUtils.arrayToDelimitedString(fields, this.delimiter);
if(null != arrregateVal && arrregateVal.contains(","))
{
arrregateVal = "\""+arrregateVal+"\"";
}
return arrregateVal;
}
}
在doAggregate 方法中,每一行的完整数据都来了,如果任何行包含逗号,它将把整行放在里面(即双引号)“我的包含逗号的完整行数据”因此整行即使在其中包含逗号,也会出现在一行中。