zoukankan      html  css  js  c++  java
  • 关于EXCEL下载后无法打开的问题

    image

    通过流的下载的方式下载文件的需求很普遍,也基本不太会错,但最近用VPN(这套系统用的是网页反代的方式来实现),下载的Excel却无法打开,仔细检查后通过增加ContentType,修正了这个问题。代码分享如下:

    byte[] bytes = System.IO.File.ReadAllBytes(path);
                        ms.Write(bytes, 0, bytes.Length);
                        Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(displayFileName));
                        string contentType = GetContentType(path);
                        if (!string.IsNullOrEmpty(contentType))
                        {
                            Response.ContentType = contentType;
                        }
                        Response.BinaryWrite(ms.ToArray());
                        Response.End();
    

    其中ContentType部分如下:

    public const string ExcelContentType_97Or2003 = "application/vnd.ms-excel";
    public const string ExcelContentType_2007 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    
    public string GetContentType(string path)
    {
        string extension = Path.GetExtension(path).ToLower();
        if (extension.EndsWith("xlsx") || extension.EndsWith("xlsm"))
        {
            return ExcelContentType_2007;
        }
        else if (extension.EndsWith("xls"))
        {
            return ExcelContentType_97Or2003;
        }
        return string.Empty;
    }
    
  • 相关阅读:
    PSP编程
    题库软件1.0发布
    ubuntu上安装netgear wg511v2驱动
    boost的编译
    Plot3D 0.3发布
    立体画板Plot3D
    求教团队内的朋友,在directx中,如何画虚线?
    OpenGL如何显示文本?
    JZ028数组中出现次数超过一半的数字
    JZ027字符串的排列
  • 原文地址:https://www.cnblogs.com/volnet/p/2127186.html
Copyright © 2011-2022 走看看