Commit e99cdcfa authored by pengxin's avatar pengxin

导出CSV特殊符号处理。

parent 07ce48de
......@@ -26,6 +26,7 @@ import com.yice.webadmin.app.service.DatasetDataService;
import com.yice.webadmin.app.service.DatasetOutputService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
......@@ -391,16 +392,7 @@ public class DatasetOutputServiceImpl extends BaseService<DatasetOutput, Long> i
JsonNode rootNode = objectMapper.readTree(datasetData.getData());
String instruction = rootNode.get(DatasetConstant.INSTRUCTION).textValue();
String output = rootNode.get(DatasetConstant.OUTPUT).textValue();
// 对包含换行符的字符串进行处理
instruction = instruction.replaceAll("\n", " ");
output = output.replaceAll("\n", " ");
// 将包含逗号的字符串用双引号包围,并转义其中的双引号
instruction = "\"" + instruction.replaceAll("\"", "\"\"") + "\"";
output = "\"" + output.replaceAll("\"", "\"\"") + "\"";
pw.println(instruction + "," + output);
pw.println(escapeCsvValue(instruction) + "," + escapeCsvValue(output));
}
pw.flush();
// 转换为InputStream
......@@ -421,6 +413,21 @@ public class DatasetOutputServiceImpl extends BaseService<DatasetOutput, Long> i
return fileName;
}
/**
* 特殊符号处理
* @param value 处理值
* @return 返回处理
*/
private String escapeCsvValue(String value) {
// 使用Apache Commons CSV库来转义CSV值中的特殊字符
String escaped = StringEscapeUtils.escapeCsv(value);
// 如果值中包含换行符,确保整个值被双引号包围
if (value.contains("\n")) {
escaped = "\"" + escaped + "\"";
}
return escaped;
}
/**
* 获取最大数据
* @param dataList 数据列表
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment