使用OleDb导入csv文件的时候,如果文件里面的某些列包含特殊字符,我们需要对特殊字符进行处理。
比如双引号,单引号,斜杠。。。
有两种方式:
1.使用正则表达式
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false);//读取文件, filePath为文件路径包含文件名。
while (!reader.EndOfStream)//开始遍历文件每一行内容
{
string str = reader.ReadLine();
string[] split = Regex.Split(str, @",(?=(?:[""\]*""[""\]"")[^""\]*$)");//将每行内容中的特殊字符进行转换替换。
for (int i = 0; i < split.Length; i++)
{
if (split[i].Length >= 2)
{
if (split[i].IndexOf(""") == 0) split[i] = split[i].Substring(1, split[i].Length-1);
if (split[i].LastIndexOf(""") == (split[i].Length - 1)) split[i] = split[i].Substring(0, split[i].Length - 1);
}
split[i].Replace("""", “”");
}
}
2. 遍历每个单元格中的字符串,替换特殊字符。
public static String[] GetValue(String inString, String delimiter, Boolean deletequotes)
{
//inString为你要处理的字符串的值,delimiter为分隔符,用什么进行分割,比如逗号
String[] outString = null;
try
{
int tokencount = 0;
string quote = “”";
string delim = delimiter.Substring(0, 1);
String[] tempString = new String[400];
Boolean inquotes = false;
Boolean quoteinquote = false;
StringBuilder sb = new StringBuilder();
for (int x = 0; x < inString.Length; x++) { string cchar = inString.Substring(x, 1); if (cchar == quote) { if (!inquotes) { inquotes = true; quoteinquote = false; } else { if ((x == inString.Length - 1) || inString.Substring(x + 1, 1) == delim) { inquotes = false; quoteinquote = false; } else { quoteinquote = true; } } } if ((cchar == delim) && !inquotes) { if ("null".Equals(sb.ToString())) { tempString[tokencount] = ""; } else { tempString[tokencount] = sb.ToString(); } sb = new StringBuilder(); tokencount++; } else if (cchar != quote) { sb.Append(cchar); } else if (!deletequotes && quoteinquote) { sb.Append(cchar); } } if (inString.Length > 0 && inString.Substring(inString.Length - 1, 1) == delim) { tempString[tokencount] = ""; } tempString[tokencount] = sb.ToString(); tokencount++; outString = new String[tokencount]; for (int z = 0; z < tokencount; z++) { outString[z] = tempString[z]; } } catch (Exception ex) { logger.Error(ex); } return outString; }