关于Excel文件打开的问题-乱码
C# 操作Excel下载 ,下载的时候弹出IE的保存-打开的那个小窗口。
如果选择保存后再打开没有问题,
但是直接打开的话会出现一个弹出出窗口 -
您尝试打开的文件“%e5%99%a8%e5%85%b7%e5%9f%ba%e7%.xls 的格式
与指定的格式不一致 。” 我的文件名叫 “成绩列表.xls” 好像是把这个中午名给解析了。
现在打开的话,那Excel的工作表(sheel)的题目就都是 “%e5%99%a8%e5%85%b7%e5%9f%ba%e7%
这样的乱码。我的代码在下面。。请教高手解答。谢谢。多谢。
public static bool ResponseFile(HttpRequest oHttpRequest, HttpResponse oHttpResponse, string sFileName, long lSpeed, byte[] bFile)
{
MemoryStream oFileStream = new MemoryStream(bFile);
BinaryReader oBinaryReader = new BinaryReader(oFileStream);
try
{
long lFileLength = bFile.Length;
long lStartBytes = 0;
int nPack = 10240;
int nSleep = (int)Math.Floor((float)(1000 * nPack / lSpeed)) + 1;
oHttpResponse.AddHeader("Accept-Ranges", "bytes");
oHttpResponse.Buffer = false;
if (oHttpRequest.Headers["Range"] != null)
{
oHttpResponse.StatusCode = 200;
string[] arrRange = oHttpRequest.Headers["Range"].Split(new char[] { '=', '-' });
lStartBytes = Convert.ToInt64(arrRange[1]);
}
oHttpResponse.AddHeader("Content-Length", (lFileLength - lStartBytes).ToString());
if (lStartBytes != 0)
{
oHttpResponse.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", lStartBytes, lFileLength - 1, lFileLength));
}
oHttpResponse.AddHeader("Connection", "Keep-Alive");
oHttpResponse.ContentType = "application/octet-stream";
//oHttpResponse.HeaderEncoding = Encoding.GetEncoding("GB2312");
//oHttpResponse.AddHeader("Content-Disposition", "attachment;filename=" + sFileName);
//oHttpResponse.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sFileName, System.Text.Encoding.GetEncoding("gb2312")));
oHttpResponse.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sFileName, System.Text.Encoding.UTF8).Replace("+", "%20"));
oBinaryReader.BaseStream.Seek(lStartBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((float)((lFileLength - lStartBytes) / nPack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (oHttpResponse.IsClientConnected)
{
oHttpResponse.BinaryWrite(oBinaryReader.ReadBytes(nPack));
Thread.Sleep(nSleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
oBinaryReader.Close();
oFileStream.Close();
}
return true;
}