我的文本文件包含 ö、ä、ü 等字符,并且文件以 ANSI 编码。我想阅读它并将所有数据放入数据表中。我的代码是:
private DataTable GetTextToTable(string path)
{
try
{
DataTable dataTable = new DataTable();
int rowNo = 0;
using (StreamReader sr = new StreamReader(path,Encoding.Default,true))
{
DataRow dr = null;
String line;
while ((line = sr.ReadLine()) != null)
{
dr=dataTable.NewRow();
if (rowNo == 0)
{
string[] Columns = line.Split(new string[] { "\t" }, StringSplitOptions.None);
for (int i = 0; i != Columns.Length; i++)
{
DataColumn dc = new DataColumn();
dc.ColumnName = Convert.ToString(Columns[i]);
dataTable.Columns.Add(dc);
}
rowNo = rowNo + 1;
}
else
{
string[] ColumnsValue = line.Split(new string[] { "\t" }, StringSplitOptions.None);
for (int i = 0; i != ColumnsValue.Length; i++)
{
dr[i] = ColumnsValue[i];
}
dataTable.Rows.Add(dr);
}
}
}
return dataTable;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
我从这段代码中得到的输出是:¿½ 表示 ö 并以同样的方式为其余 ä 获得任意字符组合,ü 类型字符也是。所以帮助我!提前致谢!