提速快的车C#使⽤流处理写⼊WORD⽂件出现乱码
要实现从数据库中读取数据并将数据拼接成字符串,从⽽写⼊流,但⽣成的word中却是乱码。
代码如下:
word打开是乱码string upLoadPath = GetUpLoadPath(); //上传⽬录相对路径
string CNumber = "LBSC" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
建怎么建?string newFileName = CNumber + ".doc"; //随机⽣成新的⽂件名
string fullUpLoadPath = DotNet.FrameWork.Common.API.SerializeHelper.GetMapPath(upLoadPath); //上传⽬录的物理路径立春说说
string newFilePath = upLoadPath + newFileName; //上传后的路径
加班工资怎么算string ReportFileName = fullUpLoadPath + newFileName;//上传后完整路径
string html = lab360WebUI.OrderHelper.GenerateAgreement(oid, CNumber);//拼接的字符串
FileStream Fs = new FileStream(ReportFileName, FileMode.Create);
BinaryWriter binWrite = new BinaryWriter(Fs, System.Text.Encoding.GetEncoding("UTF-8"));
binWrite.Write(html);
binWrite.Close();
Fs.Close();
byte[] byteArray = System.Text.Encoding.Default.GetBytes(html);
类FileStream是以⼆进制形式将基元类型写⼊流,并⽀持⽤特定的编码写⼊字符串。
所以出现乱码的原因是:虽然是⽤字符串写⼊⽅式,但是编码并不可控,既然FileStream可以⼆进制形式将基元类型写⼊流,那就将字符串转换成type[]类型之后再写⼊流。代码如下:
谭维维简介string upLoadPath = GetUpLoadPath(); //上传⽬录相对路径
string CNumber = "LBSC" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
string newFileName = CNumber + ".doc"; //随机⽣成新的⽂件名
string fullUpLoadPath = DotNet.FrameWork.Common.API.SerializeHelper.GetMapPath(upLoadPath); //上传⽬录的物理路径
string newFilePath = upLoadPath + newFileName; //上传后的路径
string ReportFileName = fullUpLoadPath + newFileName;//上传后完整路径
string html = lab360WebUI.OrderHelper.GenerateAgreement(oid, CNumber);拼接的字符串
byte[] byteArray = System.Text.Encoding.Default.GetBytes(html);
FileStream Fs = new FileStream(ReportFileName, FileMode.Create);
BinaryWriter binWrite = new BinaryWriter(Fs, System.Text.Encoding.GetEncoding("UTF-8"));
binWrite.Write(byteArray);
binWrite.Close();
Fs.Close();
这样写⼊之后导出的word就正常不出乱码了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论