各位同仁:
我用一下各种流,Encoding下的各种编码读word文档,结果读出来的都是乱码,那怎么才能正常读出word文档呢?谢谢!!!
public void ReadTxtFile(string strPath)
{
//对txt可以,但doc是一堆乱码
StreamReader read=new StreamReader(strPath,Encoding.UTF8);
string str=read.ReadLine();
while(str!=null)
{
Console.WriteLine(str);
str=read.ReadLine();
}
read.Close();
}
public void ReadFile(string strFile)
{
//读doc也是乱码
FileStream fStream=new FileStream(strFile,FileMode.Open,FileAccess.Read);
BinaryReader bRead=new BinaryReader(fStream,Encoding.Default);
byte []bt=new byte[fStream.Length];
bRead.Read(bt,0,bt.Length);
string str=Encoding.Default.GetString(bt,0,bt.Length);
Console.Write(str);
bRead.Close();
fStream.Close();
}
我用一下各种流,Encoding下的各种编码读word文档,结果读出来的都是乱码,那怎么才能正常读出word文档呢?谢谢!!!
public void ReadTxtFile(string strPath)
{
//对txt可以,但doc是一堆乱码
StreamReader read=new StreamReader(strPath,Encoding.UTF8);
string str=read.ReadLine();
while(str!=null)
{
Console.WriteLine(str);
str=read.ReadLine();
}
read.Close();
}
public void ReadFile(string strFile)
{
//读doc也是乱码
FileStream fStream=new FileStream(strFile,FileMode.Open,FileAccess.Read);
BinaryReader bRead=new BinaryReader(fStream,Encoding.Default);
byte []bt=new byte[fStream.Length];
bRead.Read(bt,0,bt.Length);
string str=Encoding.Default.GetString(bt,0,bt.Length);
Console.Write(str);
bRead.Close();
fStream.Close();
}
用word.dll
wordApplication类读取,写入。参考MSDN