我利用程序想把Excel文件的后缀名.xls改成.txt,但是里面的内容都变成乱码了,不知道该怎么解决啊?
我的代码如下:
//获得该路径全部文件名
string [] fileNames = Directory.GetFiles(FilePath); string newNm = "";
string FileNmTmp = "";
string strPath = "";
foreach(string fileNm in fileNames)
{
strPath = fileNm.Substring(0,fileNm.LastIndexOf(@"\")+1);
FileNmTmp = fileNm.Substring(fileNm.LastIndexOf('\\')+1);
FileNmTmp = FileNmTmp.Substring(0,4);
switch(FileNmTmp)
{
case "If25":
newNm = m_rep_Prefix + fileNm.Substring(fileNm.LastIndexOf('\\')+5);
newNm = newNm.Substring(0,newNm.LastIndexOf('.')) + ".txt";
break;
case "If28":
newNm = m_Pro_Prefix + fileNm.Substring(fileNm.LastIndexOf('\\')+5);
newNm = newNm.Substring(0,newNm.LastIndexOf('.')) + ".txt";
break;
case "If42":
newNm = m_grp_Prefix + fileNm.Substring(fileNm.LastIndexOf('\\')+5);
newNm = newNm.Substring(0,newNm.LastIndexOf('.')) + ".txt";
break;
default:
newNm = fileNm.Substring(fileNm.LastIndexOf('\\')+1);
newNm = newNm.Substring(0,newNm.LastIndexOf('.')) + ".txt";
break;
}
//File.Move(fileNm,strPath + newNm);
File.Copy(fileNm,strPath + newNm,true);
File.Delete(fileNm);
this.txtNewFile.Text += newNm + "\r\n";
}

解决方案 »

  1.   

    虽然我不太清楚Excel的编码格式,但我想肯定不是简单的文本格式吧。你只是将文件的内容照搬,再将后缀名改了,得到的当然是乱码了
      

  2.   

    你可以用代码打开Excel文件,然后操作Excel对象让它另存为txt文件就行了。
      

  3.   

    to: sunjian_qi(sonne) 
    我知道有专门基于word可以转成很多文件格式的类.
    但是不知道有没有专门基于Excel可以转成很多文件格式的类呢?
      

  4.   

    我们以前是这样做的,找到Excel.exe或者Excel.dll(好像Office2000是Excel.olb),用.NET命令tlbimp导出成一个Excel.dll,再在项目中引用,就可以操作Excel了。