StreamReader不从csv文件中读取日文字符

 cfn7831325 发布于 2022-12-25 21:34

使用带有日文字符双引号字段的流阅读器读取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;
} 

在此输入图像描述

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有