我用ASp.net2.0下载excel07文件时出错:1、文件是在服务器的文件系统上(磁盘上)2、下载后打开出错,错误提示:Excel在“12.xlsx”中发现不可读取内容。是否恢复工作簿的内容?如果信任此工作簿的来源,请单击“是”。单击“是”后:Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。异常信息:ex.Message 由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。这时,excel中的数据是正确的,而且另存为另一个文件后,这个问题就不存在了,打开正常。用excel03的文件试验,就不存在了。代码如下:中间注释了一些,是因为想了很多办法来解决这个问题。没解决,问问各位大牛们。/// <summary>
    /// 文件下载并删除
    /// </summary>
    /// <param name="result"></param>
    private void FileDownLoadDel(string result)
    {
        try
        {
            //byte[] data = File.ReadAllBytes(result);            //FileStream fs = File.Open(result, FileMode.Open);            //byte[] data = new byte[(int)fs.Length];
            //fs.Read(data, 0, (int)fs.Length);
            Response.ClearHeaders();
            Response.Clear();            //Response.ContentType = "application/vnd.ms-excel";//excel格式            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.Default;
            //   添加头信息,为"文件下载/另存为"对话框指定默认文件名   
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + Server.UrlEncode("2.xlsx"));
            //   添加头信息,指定文件大小,让浏览器能够显示下载进度   
            //Response.AddHeader("Content-Length", data.Length.ToString());
            Response.ContentType = "Application/octet-stream";            //   把文件流发送到客户端   
            Response.BinaryWrite((byte[])File.ReadAllBytes(result));            //fs.Close();            //File.Delete(result);            //   停止页面的执行 
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('文件导出后,下载失败!');</script>");
            //ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "Error", "alert('获取模板文件失败!');", true);
        }
    }http://forums.microsoft.com/china/ShowPost.aspx?postid=3559357&isthread=true&siteid=15&authhash=67e452d848f011e1fcb7b3eddbcb9c10b3335f68&ticks=633504987153582477

解决方案 »

  1.   

    我们公司一个大牛,给了我段代码,瞬间就解决了,可怜我昨天在公司搞到12点。 代码如下:     /// <summary> 
        /// 文件下载并删除 
        /// </summary> 
        /// <param name="result"> </param> 
        public void FileDownLoadDel(string fullFilename) 
        { 
            System.IO.Stream iStream = null; 
            // Buffer to read 10K bytes in chunk: 
            byte[] buffer = new Byte[10000];         // Length of the file: 
            int length;         // Total bytes to read: 
            long dataToRead;         // Identify the file to download including its path. 
            string filepath = fullFilename;         // Identify the file name. 
            string filename = System.IO.Path.GetFileName(filepath);         try 
            { 
                // Open the file. 
                iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 
                            System.IO.FileAccess.Read, System.IO.FileShare.Read); 
                // Total bytes to read: 
                dataToRead = iStream.Length;             Response.ContentType = "application/octet-stream"; 
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);             // Read the bytes. 
                while (dataToRead > 0) 
                { 
                    // Verify that the client is connected. 
                    if (Response.IsClientConnected) 
                    { 
                        // Read the data in buffer. 
                        length = iStream.Read(buffer, 0, 10000);                     // Write the data to the current output stream. 
                        Response.OutputStream.Write(buffer, 0, length);                     // Flush the data to the HTML output. 
                        Response.Flush();                     buffer = new Byte[10000]; 
                        dataToRead = dataToRead - length; 
                    } 
                    else 
                    { 
                        //prevent infinite loop if user disconnects 
                        dataToRead = -1; 
                        Response.Clear(); 
                    } 
                } 
                Response.End(); //没有这句会将该页面刷新后的内容追加写入文件中。 
            } 
            catch (Exception ex) 
            { 
                // Trap the error, if any. 
                //Response.Write("Error : " + ex.Message); 
                //base.WriteLog("资料", "下载资料:" + ex.Message + "!", LogType.Error, this.GetType().ToString());         } 
            finally 
            { 
                if (iStream != null) 
                { 
                    //Close the file. 
                    iStream.Close(); 
                } 
                File.Delete(fullFilename); 
            } 
        }