File open and Locked Exception


#1

Hi
I am trying to develop a web application, which has reports in Excel format. I am getting the following error message when 2 users are trying to generate the same report.

Error message: System.Exception: Access denied because another user has the file open and locked…
at Aspose.Excel.F1.Read(String fileName)
at Aspose.Excel.Excel.Open(String designerSpreadsheet)




Since it is a web application this will be a common scenario. Does anyone have a solution to this?

Thanks
Geeta


#2

Dear Geeta,

Thanks for your consideration.

You can use the Excel.Open(MemoryStream stream) method.


Excel excel1 = new Excel();
Excel excel2 = new Excel();

FileStream fs1 = new FileStream(“d:\book1.xls”, FileMode.Open, FileAccess.Read);
byte[] data1 = new byte[fs1.Length];
fs1.Read(data1, 0, data1.Length);
MemoryStream ms1 = new MemoryStream(data1);

FileStream fs2 = new FileStream(“d:\book1.xls”, FileMode.Open, FileAccess.Read);
byte[] data2 = new byte[fs2.Length];
fs2.Read(data2, 0, data2.Length);
MemoryStream ms2 = new MemoryStream(data2);

excel1.Open(ms1);
excel2.Open(ms2);




#3

Laurence -

I had a client that just received the same error message. However, it was when 10 users were testing a new report and only 2 out of the 10 received the error message. Any ideas on how to correct this?

Thanks

Steven Tidwell


#4

A follow up message: From my post above is there a limit to the number of excel designersheets that can be opened at once. My site could have anywhere from 1 to 100 users at a time and possibly more. This is the first time that they got this message and they haven’t seen it since then.

Thanks

Steven


#5

When you use the Excel.Open(string) method, the excel designer spreadsheet cannot be opened concurrently.
If in some cases, the excel designer spreadsheet have to be opened concurrently, please use FileStream open the file in read only mode and use Excel.Open(MemoryStream) method to open. There isn’t any limit on this approach except the hardware limit.
I also will shape the Excel.Open(string) in the future release to remove the limit.


#6

Hello Laurence. I’m trying to Open() a designer file with a MemoryStream from an Embedded Resource. I keep getting “InvalidCastException”.

I believe you only support opening with a FileStream currently. We need to embed designer files as resource files and get at them with Assy.GetManifestResource() which allows us to manipulate a Stream object. Any chance you can make Open() work directly with Stream or MemoryStream objects ?

thanks
Marty


#7

Hi Marty,

Actually Open method supports FileStream and MemoryStream. Which version are you using?

If the problem still occurs on the latest version, could you please send your file to me? Thank you.


#8

Thanks Laurence, on furhter inspection, I was not using a MemoryStream. I was using a StreamReader.

Here’s what I did, let me know if you think this is a good approach - I can post a blog article if you think it will help (I do).

- I created a Stream from the assembly’s manifest resource for the designer file.
- I copied the Stream into a MemoryStream and called Excel.Open(ms).

Marty

Here is the code:

--------------------


Dim f As String = Me.DesignerTemplateFilename()

’ Create Memory Stream from Manifest Resource
Dim ms As MemoryStream = IOHelper.GetManifestResourceAsMemoryStream(Me.ReportAssembly(), f)

’ Open the MemoryStream
x._xls.Open(ms)



-----------------------
My IOHelper Function


Public Shared Function GetManifestResourceAsMemoryStream(ByRef Assy As System.Reflection.Assembly, ByVal ResourceName As String) As System.IO.MemoryStream

Dim sr As Stream = Assy.GetManifestResourceStream(ResourceName)

If sr Is Nothing Then

Return Nothing

End If

Dim ms As MemoryStream = New MemoryStream

Dim count As Integer = -1

Dim buffer(4096) As Byte

Do Until (count = 0)

count = sr.Read(buffer, 0, buffer.Length)

ms.Write(buffer, 0, count)

Loop

sr = Nothing

ms.Position = 0

Return ms

End Function





#9

Hi Marty,

I think that your approach is fine. Aspose.Excel can open any stream which supports seeking.


#10

Hi Laurence thanks for the info - quick question for you to continue the thread since you brought up “seeking”.

When I initially tried to use the assembly.GetManifestResourceStream API, which returns a Stream, I got an Invalid Cast exception - which is why I first thought that you did not support the base stream object; so I put it into a memory stream and it worked.

So, what is the actual case ? Is this a bug or is this required ? I tend to think it is a bug because the resource stream method works fine on the Set License API - I pass it a streamreader.baseStream object (which is the unmanaged_stream).

For now, I have a work around - but let me know what you think - I would rather not have to copy the resource needlessly into a memory stream only to pass it to the Open() method. I would want to remove that code after you say its supported or not… note: I have Version 3.1.0.0

thanks again!


Dim sr As Stream = Me.ReportAssembly.GetManifestResourceStream(f)

x._xls.Open(sr)

Invalid Cast Exception- in Excel.Open()


#11

At first Aspose.Excel just support FileStream and MemoryStream but now it can open stream which support seeking. Please try the attached fix.


#12

Hello Laurence, this fix 3.2.0.3 worked. Maybe I can blog about how developers can open a stream now since it works nicely.

thanks again for the fix and your help!
Marty