asp.net 输出html表格中文乱码,asp.net 解决文本中文乱码   各类文本编码转UTF-8 文本...

///

/// 解决文本中文乱码   各类文本编码转UTF-8 文本

///

/// 原路径

/// 临时保存路径

/// 新的文件路径

/// 新文件路径

public static string  GetUtf8(string fullPath,string savePath,string strnew)

{

Encoding e =GetFileEncodeType(fullPath);

System.IO.File.Create(savePath).Close();

using (StreamReader sr = new StreamReader(fullPath, e, false))

{

string utf8info = string.Empty;

Encoding utf8 = Encoding.UTF8;

// Convert the string into a byte[].

byte[] unicodeBytes = e.GetBytes(sr.ReadToEnd());

// Perform. the conversion from one encoding to the other.

byte[] asciiBytes = Encoding.Convert(e, utf8, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.

// This is a slightly different approach to converting tillustrate

// the use of GetCharCount/GetChars.

char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];

utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);

utf8info = new string(asciiChars);

using (StreamWriter sw = new StreamWriter(savePath, false, Encoding.UTF8))

{

sw.Write(utf8info);

}

}

System.IO.File.Delete(fullPath);

System.IO.File.Copy(savePath,strnew,true);

System.IO.File.Delete(savePath);

return strnew;

}

方法2

using (FileStream fs = new FileStream(fullPath, FileMode.Open))

{

using (StreamReader sr = new StreamReader(fs,Encoding.Default))

{

while (!sr.EndOfStream)

{

string sLine = sr.ReadLine();

//StringBuilder b = new StringBuilder();

//b.AppendLine(sLine);

//另存到其他txt文件中

using (StreamWritersw=System.IO.File.AppendText(savepath))

{

//string n = b.ToString();

sw.WriteLine(sLine.ToString());

}

}

}

}

System.IO.File.Delete(fullPath);

string strNewFile = fullPath;

System.IO.File.Copy(savepath, strNewFile, true);

System.IO.File.Delete(savepath);

//using (StreamReader sr = new StreamReader(strNewFile,Encoding.Default, false))

//{

//    using (StreamWriter sw = new StreamWriter(fullPath, falsEncoding.UTF8))

//    {

//        sw.Write(Server.HtmlEncode(sr.ReadToEnd()));

//    }

//}

//System.IO.File.Delete(strNewFile);

FileInfo fileNew = new FileInfo(strNewFile);

sourcesFile.File_Size = fileNew.Length;

this.sourcesFileService.Save(sourcesFile);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值