StreamReader defaults to UTF-8 encoding unless specified otherwise, instead
of defaulting to the ANSI code page for the current system.
(Hint: Convert xls/xlsx file to csv file. the encoding is ANSI.)
http://msdn.microsoft.com/zh-tw/library/6aetdk20(v=vs.100).aspx
StreamReader Constructor (Stream, Encoding)
http://msdn.microsoft.com/zh-tw/library/ms143456(v=vs.100).aspx
------Example---------
#Two methods to access excel file
#How to adjust the encoding in StreamReader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb; // another method to access excel.
using Excel = Microsoft.Office.Interop.Excel; // the latest method to access excel.
namespace ConsoleReadStreamFile
{
class Program
{
static void Main(string[] args)
{
// undefined Encoding,the Encodeing is Unicode.
System.IO.StreamReader srNoEncode = new System.IO.StreamReader("Alam.txt");
// define Encoding is ANSI(or use System.Text.Encoding.Default)
System.IO.StreamReader srDefault =
new System.IO.StreamReader("Yaya.csv", System.Text.Encoding.Default);
}
}
}文章標籤
全站熱搜
