CSV 文件可能包含多语言字符。
下面是我的阅读代码DataView
List<BulkUser> _lstBulkUser = new List<BulkUser>();
DataView BlukUserView;
try
{
using (SqlDataSource _SqlDataSource = new SqlDataSource())
{
//_SqlDataSource.ConnectionString = "Dsn=outlook;dbq=D:\\selvam\\projectlist\\lates\\trunk\\SLVconsoleApp\\SLVconsoleApp.Web\\Uploads\\CSVFiles;defaultdir=D:\\selvam\\projectlist\\lates\\trunk\\SLVconsoleApp\\SLVconsoleApp.Web\\Uploads\\CSVFiles;driverid=27;fil=text;maxbuffersize=2048;pagetimeout=5";
_SqlDataSource.ConnectionString = "Dsn=" + ConfigurationManager.AppSettings["OutlookDns"].ToString() + ";dbq=" + ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + ";defaultdir=" + ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + ";driverid=27;fil=text;maxbuffersize=2048;pagetimeout=5";
_SqlDataSource.SelectCommand = "SELECT [First Name] As First_Name, [Last Name] AS LAST_NAME, Company, [E-mail Address] AS EMAILID FROM [" + Filename + "]";
_SqlDataSource.ProviderName = "System.Data.Odbc";
BlukUserView = (DataView)_SqlDataSource.Select(DataSourceSelectArguments.Empty);
int i = 1;
_lstBulkUser = (from value in BlukUserView.Table.Select() select new BulkUser { ID = Convert.ToString(i++), FirstName = Convert.ToString(Encoding.UTF8.GetString((byte[])value["First_Name"])), LastName = value["LAST_NAME"].ToString(), EmailId = value["EMAILID"].ToString(), Company = value["Company"].ToString() }).ToList();
if (File.Exists(ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + "\\" + Filename))
{
File.Delete(ConfigurationManager.AppSettings["OutlookFileFolder"].ToString() + "\\" + Filename);
}
}
}
catch (Exception ex)
{
}
它抛出你nable to cast object of type 'System.String' to type 'System.Byte[]'.
即使我尝试了 Unicode 字符,它也不是!我该如何克服这个问题?
下面是csv文件的截图