我用下面的代码来读取UTF-8格式的CSV,可是中文乱码,请帮忙修改,谢谢 public class ExcelReader
{
private string filePath;
private string fileName;
private OleDbConnection conn;
private DataTable readDataTable;
private string connString;
private FileType fileType = FileType.noset; public ExcelReader()
{ } private void SetFileInfo(string path)
{
filePath = path; fileName = this.filePath.Remove(0, this.filePath.LastIndexOf("\\") + 1);
switch (fileName.Split('.')[1])
{
case "xls": connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; fileType = FileType.xls;
break;
case "xlsx": connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"; fileType = FileType.xlsx;
break;
case "csv": connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath.Remove(filePath.LastIndexOf("\\") + 1) + ";Extended Properties='Text;FMT=Delimited;HDR=YES;IMEX=1;'"; fileType = FileType.csv;
break;
}
}
public DataTable ReadFile(string path)
{
if (System.IO.File.Exists(path))
{
SetFileInfo(path);
OleDbDataAdapter myCommand = null;
DataSet ds = null; using (conn = new OleDbConnection(connString))
{
conn.Open(); DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string tableName = fileType == FileType.csv ? fileName : schemaTable.Rows[0][2].ToString().Trim(); string strExcel = string.Empty; strExcel = "Select * From [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, conn); ds = new DataSet(); myCommand.Fill(ds, tableName); readDataTable =ds.Tables[0]; }
}
return readDataTable;
} private enum FileType
{
noset,
xls,
xlsx,
csv
} }
{
private string filePath;
private string fileName;
private OleDbConnection conn;
private DataTable readDataTable;
private string connString;
private FileType fileType = FileType.noset; public ExcelReader()
{ } private void SetFileInfo(string path)
{
filePath = path; fileName = this.filePath.Remove(0, this.filePath.LastIndexOf("\\") + 1);
switch (fileName.Split('.')[1])
{
case "xls": connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'"; fileType = FileType.xls;
break;
case "xlsx": connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"; fileType = FileType.xlsx;
break;
case "csv": connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath.Remove(filePath.LastIndexOf("\\") + 1) + ";Extended Properties='Text;FMT=Delimited;HDR=YES;IMEX=1;'"; fileType = FileType.csv;
break;
}
}
public DataTable ReadFile(string path)
{
if (System.IO.File.Exists(path))
{
SetFileInfo(path);
OleDbDataAdapter myCommand = null;
DataSet ds = null; using (conn = new OleDbConnection(connString))
{
conn.Open(); DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string tableName = fileType == FileType.csv ? fileName : schemaTable.Rows[0][2].ToString().Trim(); string strExcel = string.Empty; strExcel = "Select * From [" + tableName + "]";
myCommand = new OleDbDataAdapter(strExcel, conn); ds = new DataSet(); myCommand.Fill(ds, tableName); readDataTable =ds.Tables[0]; }
}
return readDataTable;
} private enum FileType
{
noset,
xls,
xlsx,
csv
} }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货