RT,
不能使用扩展名判断,太不靠谱,EXCEL好像没什么公开格式,好像也不能判断MIME,不知道有没有什么其它的方法,WINFORM的。
做为数据库去连接可能是个方法,不过似乎太慢,请各大大指教。

解决方案 »

  1.   

    通过文件头:
    http://www.dnbcw.com/biancheng/c/hqai113394.html
      

  2.   

    通过文件头信息判断
    MS Word/Excel (xls.or.doc),文件头:D0CF11E0  
      

  3.   

     public static string GetContentTypeByExtName(string extName)
            {
                switch (extName.ToLower())
                {
                    case ".001":
                        return "application/x-001";                case ".301":
                        return "application/x-301";                case ".323":
                        return "text/h323";                case ".906":
                        return "application/x-906";                case ".907":
                        return "drawing/907";
    .........      ....   
     return "application/x-wrk";                case ".ws":
                        return "application/x-ws";                case ".ws2":
                        return "application/x-ws";                case ".wsc":
                        return "text/scriptlet";                case ".wsdl":
                        return "text/xml";                case ".wvx":
                        return "video/x-ms-wvx";                case ".xdp":
                        return "application/vnd.adobe.xdp";                case ".xdr":
                        return "text/xml";                case ".xfd":
                        return "application/vnd.adobe.xfd";                case ".xfdf":
                        return "application/vnd.adobe.xfdf";                case ".xhtml":
                        return "text/html";                case ".xls":
                        return "application/x-xls";                case ".xlw":
                        return "application/x-xlw";                case ".xml":
                        return "text/xml";                case ".xpl":
                        return "audio/scpls";                case ".xq":
                        return "text/xml";                case ".xql":
                        return "text/xml";                case ".xquery":
                        return "text/xml";                case ".xsd":
                        return "text/xml";                case ".xsl":
                        return "text/xml";                case ".xslt":
                        return "text/xml";                case ".xwd":
                        return "application/x-xwd";                case ".x_b":
                        return "application/x-x_b";                case ".x_t":
                        return "application/x-x_t";
                }
                return "application/octet-stream";
            }
      

  4.   

    已经搞定,结贴代码:/// <summary>
            /// 是否是EXCEL文件
            /// </summary>
            /// <param name="filePath"></param>
            /// <returns></returns>
            private bool isExcelFile(string filePath)
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                byte[] b = new byte[4];
                string temstr = "";
                //将文件流读取的文件写入到字节数组
                if (Convert.ToInt32(fs.Length) > 0)
                {
                    fs.Read(b, 0, 4);                fs.Close();
                    
                    for (int i = 0; i < b.Length; i++)
                    {
                        temstr += Convert.ToString(b[i], 16);
                    }                
                }
             
                if (temstr.ToUpper() == "D0CF11E0")
                { return true; }
                else
                { return false; }
            }