开发者

how Import /read CSV File with Different - different encoding

开发者 https://www.devze.com 2023-04-05 03:22 出处:网络
I am trying to read / import CSV into data set with Following code if (!File.Exists(file)) { File.Create(file).Close();

I am trying to read / import CSV into data set with Following code

if (!File.Exists(file))
{
    File.Create(file).Close();
}

OleDbConnection connection = new System.Data.OleDb.OleDbConnection(
    "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + Path.GetDirectoryName(file) + 
    "; Extended Properties = \"Text;Excel 12.0;HDR=" + _AllowHeader + ";FMT=Delimited\"");

connection.Open();

string Query = "Select " + Count + " from [" + Path.GetFileName(_filename) + "]";

OleDbDataAdapter Adaptar = new System.Data.OleDb.OleDbDataAdapter(Query, connection);
Adaptar.Fill(DataSet);

It is fine With ANSI Encoded csv file a开发者_运维百科s I can Import, but when I try to import UTF-8 or Unicode (Encoded) CSV File I got Byte Order Mark (BOM) in column Name.

My question is how can I prevent this and import any encoded CSV File?


You are probably better of using something other than Jet / Ace drivers to read or parse CSV data. At work we use this free library: http://www.filehelpers.com/

Looking at the documentation it appears to support different encoding types.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号