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;
    }
    
  • 相关阅读:
    RWIGS and LORBIT (1)
    时间档案:飞秒、皮秒、纳秒、微秒、毫秒、秒 (转自新浪)
    Linux Shell 文本处理工具集锦(转载)
    awk——getline
    PERL 正则表达式简介
    算法的性能
    排序算法与稳定性的理解
    实现双端队列
    实现栈
    实现队列
  • 原文地址:https://www.cnblogs.com/volnet/p/2127186.html
Copyright © 2011-2022 走看看