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;
}
如果选择保存后再打开没有问题,但是直接打开的话会出现一个弹出出窗口 -
您尝试打开的文件“%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;
}
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
参考
直接打开的时候由于进行了编码,但是在客户端没进行解码,也没有办法进行解码,所以文件名和sheet名看起来会乱码,这个应该没办法解决,我以前查阅了好多资料
在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗?
测试:string file="文件上(传)篇.doc";
string Server_UrlEncode=Server.UrlEncode(file);
string Server_UrlDecode=Server.UrlDecode(Server_UrlEncode);
string HttpUtility_UrlEncode=System.Web.HttpUtility.UrlEncode(file);
string HttpUtility_UrlDecode=System.Web.HttpUtility.UrlDecode(HttpUtility_UrlEncode);
Response.Write("原数据:"+file);
SFun.WriteLine("Server.UrlEncode:"+Server_UrlEncode);
SFun.WriteLine("Server.UrlDecode:"+Server_UrlDecode);
SFun.WriteLine("HttpUtility.UrlEncode:"+HttpUtility_UrlEncode);
SFun.WriteLine("HttpUtility.UrlDecode:"+HttpUtility_UrlDecode);
输出:
原数据:文件上(传)篇.doc
Server.UrlEncode:%ce%c4%bc%fe%c9%cf%a3%a8%b4%ab%a3%a9%c6%aa.doc
Server.UrlDecode:文件上(传)篇.doc
HttpUtility.UrlEncode:%e6%96%87%e4%bb%b6%e4%b8%8a%ef%bc%88%e4%bc%a0%ef%bc%89%e7%af%87.doc
HttpUtility.UrlDecode:文件上(传)篇.doc 区别在于:HttpUtility.UrlEncode()默认是以UTF8对URL进行编码,而Server.UrlEncode()则以默认的编码对URL进行编码。 在用 ASP.Net 开发页面的时候, 我们常常通过 System.Web.HttpUtility.UrlEncode 和 UrlDecode 在页面间通过 URL 传递参数. 成对的使用 Encode 和 Decode 是没有问题的. 但是, 我们在编写文件下载的页面的时候, 常常用如下方法来指定下载的文件的名称:Response.AddHeader("Content-Disposition","attachment; filename="
+ HttpUtility.UrlEncode(fileName, Encoding.UTF8));
之所以转换成 UTF8 是为了支持中文文件名. 这时候问题就来了, 因为 HttpUtility.UrlEncode 在 Encode 的时候, 将空格转换成加号('+'), 在 Decode 的时候将加号转为空格, 但是浏览器是不能理解加号为空格的, 所以如果文件名包含了空格, 在浏览器下载得到的文件, 空格就变成了加号.一个解决办法是, 在 HttpUtility 的 UrlEncode 之后, 将 "+" 替换成 "%20"( 如果原来是 "+" 则被转换成 "%20" ) , 如:fileName = HttpUtility.UrlEncode(fileName, Encoding.UTF8);
fileName = fileName.Replace("+", "%20");
不明白微软为什么要把空格转换成加号而不是"%20". 记得 JDK 的 UrlEncoder 是将空格转换成 "%20"的.
经检查, 在 .Net 2.0 也是这样. 上面是从别的地方拷贝的,写得很好,我自己的一个程序中也遇到同样的问题,默认aspx是以utf-8为编码的,在我这个程序中必须用gb2312为默认编码
(<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>),
问题出现了,以前没有问题的HttpUtility.UrlDecode在Page.Request回的值是乱码这就是上面说的HttpUtility.UrlDecode默认以UTF8对URL进行编码,这种情况下面只需将HttpUtility.UrlDecode改成Server.UrlEncode即可。已投稿到: 排行榜 圈子 阅读(58)|评论(0)|收藏(0)|打印|举报