读取Csv文件编码错误 [英] Read Csv file encoding error

查看:107
本文介绍了读取Csv文件编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法读取Csv文件内容:

  ///< summary> 
///从CSV文件读取数据到数据表
///< / summary>
///< param name =filePath> CSV文件的路径< / param>
///< return>数据表填充有从CSV文件读取的数据< / returns>
public DataTable ReadCsv(string filePath)
{
if(string.IsNullOrEmpty(filePath))
{
log.Error(CSV文件名无效。 ;
return null;
}

try
{
DataTable dt = new DataTable();

string folder = FileMngr.Instance.ExtractFileDir(filePath);
string fileName = FileMngr.Instance.ExtractFileName(filePath);
string connectionString =
string.Concat(@Driver = {Microsoft Text Driver(* .txt; * .csv)}; Dbq =,
folder,;);

使用(OdbcConnection conn =
new System.Data.Odbc.OdbcConnection(connectionString))
{
string selectCommand = string.Concat(select * from [ , 文件名, ]);
using(OdbcDataAdapter da = new OdbcDataAdapter(selectCommand,conn))
{
da.Fill(dt);
}
}

return dt;
}
catch(Exception ex)
{
log.Error(Error loading CSV content,ex);
return null;
}
}

如果我有一个UTF -8 编码的具有 schema.ini 的Csv文件:

  [Example.csv] 
Format = Delimited(,)
ColNameHeader = True
MaxScanRows = 2
CharacterSet = ANSI
pre>

如果我在使用Unicode编码的CSV文件中使用德语字符,则该方法无法正确读取数据。



我可以对上述方法进行哪些修改以读取Unicode的 Csv文件?如果没有办法这样做,你可以建议什么Csv阅读代码?

解决方案

尝试使用 CharacterSet = UNICODE 。虽然这不是在MSDN上记录,它根据此 Microsoft论坛上的主题。 / p>

I am using the following method for reading Csv file content:

    /// <summary>
    /// Reads data from a CSV file to a datatable
    /// </summary>
    /// <param name="filePath">Path to the CSV file</param>
    /// <returns>Datatable filled with data read from the CSV file</returns>
    public DataTable ReadCsv(string filePath)
    {
        if (string.IsNullOrEmpty(filePath))
        {
            log.Error("Invalid CSV file name.");
            return null;
        }

        try
        {
            DataTable dt = new DataTable();

            string folder = FileMngr.Instance.ExtractFileDir(filePath);
            string fileName = FileMngr.Instance.ExtractFileName(filePath);
            string connectionString = 
            string.Concat(@"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=",
            folder, ";");

            using (OdbcConnection conn = 
                   new System.Data.Odbc.OdbcConnection(connectionString))
            {
                string selectCommand = string.Concat("select * from [", fileName, "]");
                using (OdbcDataAdapter da = new OdbcDataAdapter(selectCommand, conn))
                {
                    da.Fill(dt);
                }
            }

            return dt;
        }
        catch (Exception ex)
        {
            log.Error("Error loading CSV content", ex);
            return null;
        }
    }

This method works if I have a UTF-8 encoded Csv file with a schema.ini that looks something like this:

[Example.csv]
Format=Delimited(,)
ColNameHeader=True
MaxScanRows=2
CharacterSet=ANSI

If I have German characters in a Csv file with Unicode encoding, the method cannot read the data correctly.

What modifications can I make to the above method to read Unicode Csv files? If there is no way to do it this way, what Csv-reading code can you suggest?

解决方案

Try using CharacterSet=UNICODE in your schema.ini file. Although this is not documented on MSDN it works according to this thread on Microsoft Forums.

这篇关于读取Csv文件编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆