Visual Studio 2005 and Aspose.Cells issue


#1

Hi,

I have a VB.NET application in VS2005 that is using Aspose.Cells 4.0.1, using the following code to read from a datareader into an Excel file upon the clicking of a buttonlink:

'********************************Start Aspose Code

Dim license As License = New License() 'Create a License object

license.SetLicense(Server.MapPath("\WebApp1\Aspose.Cells.lic")) 'Set the license of Aspose.Cells to avoid the evaluation limitations

Dim workbook As Workbook = New Workbook() 'Instantiate an Workbook object that represents an Excel file

Dim fstream As IO.FileStream = New IO.FileStream(Server.MapPath("\WebApp1\book1.xls"), IO.FileMode.Open) 'Creating a file stream containing the Excel file to be opened

workbook.Open(fstream, FileFormatType.Default) 'Opening the Excel file through the file stream

dReader = cmd.ExecuteReader()

While dReader.Read

For k = 0 To 3

Dim cell As Cell = workbook.Worksheets(0).Cells(i, k) 'Get the reference of "A1" cell from the cells collection of a worksheet

cell.PutValue(dReader.GetValue(k)) 'Put the dReader value into the "A1" cell

Next

i = i + 1

End While

workbook.Save("File.xls", FileFormatType.Default, SaveType.OpenInExcel, Response) 'Save the Excel file

fstream.Close()

'********************************End Aspose Code

The issue I'm running into is that, if I click the buttonlink to create the Excel file a second time in that same session (after clicking it and getting the Excel file successfully a first time), I get the following error from .NET:

Server Error in '/WebApp1' Application.

The process cannot access the file 'S:\server\WebApp1\book1.xls' because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: The process cannot access the file 'S:\server\WebApp1\book1.xls' because it is being used by another process.

Source Error:

Line 349:        license.SetLicense(Server.MapPath("\WebApp1\Aspose.Cells.lic"))             'Set the license of Aspose.Cells to avoid the evaluation limitations
Line 350:        Dim workbook As Workbook = New Workbook()                                   'Instantiate an Workbook object that represents an Excel file
Line 351:        Dim fstream As IO.FileStream = New IO.FileStream(Server.MapPath("\WebApp1\book1.xls"), IO.FileMode.Open) 'Creating a file stream containing the Excel file to be opened
Line 352:        workbook.Open(fstream, FileFormatType.Default)                              'Opening the Excel file through the file stream
Line 353:        dReader = cmd.ExecuteReader()

Is there a way around this error, or is there a snippet of code I need to use that I'm missing in my sub? Any help would be appreciated. I'll post the stack trace if it is needed.


#2

Please change your code from:

Dim fstream As IO.FileStream = New IO.FileStream(Server.MapPath("\WebApp1\book1.xls"), IO.FileMode.Open) 'Creating a file stream containing the Excel file to be opened

to

Dim fstream As IO.FileStream = IO.File.OpenRead(Server.MapPath("\WebApp1\book1.xls"))