简体   繁体   English

如何以不同的方式导入/读取CSV文件-不同的编码

[英]how Import /read CSV File with Different - different encoding

I am trying to read / import CSV into data set with Following code 我正在尝试使用以下代码将CSV读取/导入到数据集中

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 as 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. 可以使用ANSI编码的csv文件很好地导入,但是当我尝试导入UTF-8或Unicode(编码)CSV文件时,我在名称列中得到了字节顺序标记(BOM)。

My question is how can I prevent this and import any encoded CSV File? 我的问题是如何防止这种情况并导入任何编码的CSV文件?

You are probably better of using something other than Jet / Ace drivers to read or parse CSV data. 您最好使用Jet / Ace驱动程序以外的其他方法来读取或解析CSV数据。 At work we use this free library: http://www.filehelpers.com/ 在工作中,我们使用以下免费库: http : //www.filehelpers.com/

Looking at the documentation it appears to support different encoding types. 查看文档,它似乎支持不同的编码类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM