在ASP.Net中下载Excel文件时出错. [英] Getting error while downloading an excel file in ASP.Net.

查看:104
本文介绍了在ASP.Net中下载Excel文件时出错.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件共享路径上有一个Excel文件,并且Excel工作表已连接到SQL表.我试图以编程方式刷新该Excel工作表,然后尝试下载它.文件正在被另一个进程使用."
该代码是这样的:-

I have a Excel file on a file share path and the excel sheet is connected to a SQL Table.I am trying to refresh that excel sheet programatically and then trying to download it.While downloading i am getting an exception that "The file is being used by another process".
The Code is like that :-

string workbookPath = @"\\172.23.150.78\DTRS\AllocationReport1.xlsx";
Application excelFile = new Application();               
Workbook theWorkbook = excelFile.Workbooks._Open(workbookPath, 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false);    
Sheets sheets = (Sheets)theWorkbook.Worksheets;            
theWorkbook.RefreshAll();
FileInfo myfile = new FileInfo(@"\\172.23.150.78\DTRS\AllocationReport1.xlsx");
if (myfile.Exists)
                {
                    // Clear the content of the response  
                    HttpContext.Current.Response.ClearContent();

                    // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

                    // Add the file size into the response header
                    HttpContext.Current.Response.AddHeader("Content-Length", myfile.Length.ToString());

                    // Set the ContentType
                    //HttpContext.Current.Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

                    // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
                    HttpContext.Current.Response.WriteFile(myfile.FullName);

                    // End the response
                    HttpContext.Current.Response.End();
                }



请给我建议,因为我需要强制执行这两个步骤,所以我该怎么做.
首先刷新Excel,然后下载Excel.
发生异常是因为Excel正在内部刷新连接,因为当我调试它时,我会收到一条弹出消息,提示网络服务正在使用AllocationReport1.xlsx.打开只读?"



Please give me suggestion that what i will do as i need both process compulsorly.
Firstly refreshing the Excel and Secondly Downloading the Excel.
The exception is occuring because the Excel is internally refreshing the connection as when i am debugging it i m getting an pop up message that "AllocationReport1.xlsx is being used by NETWORK SERVICE.Open read only?"

推荐答案

,使用下面的代码,您可以查看是否可以独占打开文件:

with the next code you can see if you can exclusively open the the file:

FileInfo fi = new FileInfo(FolderAndFileName);
               if (fi.Exists)
               {
                   FileStream stream = null;
                   try
                   {
                       // check if the file can be opened exclusive. If that is possible, then the file is found otherwise in transfer
                       using (stream = File.Open(FolderAndFileName, FileMode.Open, FileAccess.Write, FileShare.None))
                       {
                           if (stream.CanWrite)


如果是stream.CanWrite,则可以访问该文件.

其他问题是您检入代码是否存在该文件.如果没有,您将不会写入任何文件!
其次,如果找到该文件,则首先将其删除.您始终可以编写一个新文件,而不要使用相同名称的文件.


If the stream.CanWrite you can access the file.

Other probles is that you check in the code if the file exists. If it does not you will not write any file!
Second if the file is found delete it first. You can always write a new file than with the same name.


这篇关于在ASP.Net中下载Excel文件时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆