使用带有日文字符双引号字段的流阅读器读取csv文件.它不读日文字符并将其视为unicode字符.我尝试了不同的编码类型,但它不适合我.请与我分享一些想法或解决此问题的其他解决方案.或者有更好的方法来做到这一点.
public DataTable ReadDataFromCSV(string path, char delim) { string fulltext; string[] arrColumnNames; string[] arrColumnValues; string[] arrRows; int i, j, n; System.Data.DataTable dt = new System.Data.DataTable(); DataRow row; if (delim.ToString().Length < 1) { delim = ','; } try { //' check that the file exists before opening it if (File.Exists(path)) { using (TextReader sr = new StreamReader(path,Encoding.UTF8)) { fulltext = sr.ReadToEnd(); arrRows = fulltext.Split('\n'); arrColumnNames = arrRows[0].Replace('"', ' ').Trim().Split(delim); //'add columns to a datatable for (n = 0; n < arrColumnNames.Length - 1; n++) { dt.Columns.Add(new DataColumn(arrColumnNames[n], System.Type.GetType("System.String"))); }//next for (i = 1; i < arrRows.Length - 1; i++) { arrColumnValues = arrRows[i].Replace('"', ' ').Trim().Split(delim); row = dt.NewRow(); for (j = 0; j < (arrColumnNames.Length - 1); j++) { try { if (!(arrColumnValues[j] == null)) { row[arrColumnNames[j]] = arrColumnValues[j].Replace('"', ' ').Trim(); } else { row[arrColumnNames[j]] = ""; }//End If } catch (Exception ex) { Console.Write("ERROR: " + ex.Message); } }//next dt.Rows.Add(row); }//next } }//End if } catch (Exception ex) { Console.Write("ERROR: " + ex.Message); } finally { }//End Try return dt; }