Merge pull request #5331 from dataease/pr@dev@fixdataset

fix: 精简模式导入csv处理特殊字符
This commit is contained in:
taojinlong 2023-05-26 11:47:13 +08:00 committed by GitHub
commit 6c45cd0567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 7 deletions

View File

@ -2454,7 +2454,19 @@ public class DataSetTableService {
if (num > 1000) {
break;
}
data.add(Arrays.asList(line.split(",")));
String str;
line += ",";
Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
Matcher mCells = pCells.matcher(line);
List<String> cells = new ArrayList();//每行记录一个list
//读取每个单元格
while (mCells.find()) {
str = mCells.group();
str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
data.add(cells);
num++;
}
ExcelSheetData excelSheetData = new ExcelSheetData();

View File

@ -72,6 +72,8 @@ import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Service
@ -758,13 +760,20 @@ public class ExtractDataService {
List<List<String>> csvData = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
if(line.endsWith(",")){
List<String> list = new ArrayList<>(Arrays.asList(line.split(",")));
list.add("");
csvData.add(list);
}else {
csvData.add(Arrays.asList(line.split(",")));
String str;
line += ",";
Pattern pCells = Pattern.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,");
Matcher mCells = pCells.matcher(line);
List<String> cells = new ArrayList();//每行记录一个list
//读取每个单元格
while (mCells.find()) {
str = mCells.group();
str = str.replaceAll("(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
csvData.add(cells);
}
ExcelSheetData csvSheetData = new ExcelSheetData();
String[] fieldArray = fields.stream().map(TableField::getFieldName).toArray(String[]::new);