阅读CSV文件的编码错误 [英] Read Csv file encoding error
问题描述
我使用下面的方法读取CSV文件的内容:
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;
}
}
此方法的工作,如果我有一个的 UTF-8 的EN codeD CSV文件用的的Schema.ini 的,看起来是这样的:
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
如果我有一个CSV德国字符的文件用的的Uni code 的编码,该方法不能正确读取数据。
If I have German characters in a Csv file with Unicode encoding, the method cannot read the data correctly.
我可以对上面的方法有什么修改,读取的的Uni code 的CSV文件?如果没有办法做到这一点这样一来,什么的CSV阅读code可以建议?
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?
推荐答案
尝试在你的schema.ini文件使用字符集= UNI code
。虽然这不是记录在MSDN上该本<一个作品href="http://social.microsoft.com/Forums/en-US/vblanguage/thread/0ab1db1a-bfc4-48b6-b31e-33242abf18b2"相对=nofollow>微软论坛线程的。
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屋!