Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Creating Excel file but remained in used
Message
De
22/03/2007 11:09:42
Alexandre Palma
Harms Software, Inc.
Alverca, Portugal
 
 
À
22/03/2007 10:33:58
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 8.0
OS:
Windows XP SP2
Database:
Visual FoxPro
Divers
Thread ID:
01207065
Message ID:
01207094
Vues:
14
Michel your missing loFileStream.Close once u finish dealing with it.


>I used the following code to create an Excel file from a dataset. However, the only way I can forward that file to the user is with a Response.Redirect(). If I try a Response.TransmitFile(), it will say that the file is in used by another process. If I try to directly open that file from the disk, it tells me the same thing.
>
>Anyone can take a look at that code and see what kind of operation I should do in order to release that lock on the file?
>
>
>        ' Export a dataset into an Excel spreadsheet
>        ' expO1 DataSet
>        ' expC1 Full path of the file to save
>        Public Function ExportToExcel(ByVal toDataSet As DataSet, ByVal tcFile As String) As Boolean
>            Dim lcFile As String = oApp.GenerateFileName()
>            Dim lcXml As String = ""
>            Dim lcXsl As String = ""
>            Dim loFileStream As FileStream
>            Dim loXml As Framework.XML
>            Dim loXslCompiledTransform As Xsl.XslCompiledTransform = New Xsl.XslCompiledTransform
>
>            ' Get the proper definition as per the current scope
>            If oProcess Is Nothing Then
>                loXml = New Framework.XML(oApp)
>            Else
>                loXml = New Framework.XML(oProcess)
>            End If
>
>            ' Load the Xml
>            If Not loXml.LoadXml(oApp.DataSetToXml(toDataSet)) Then
>                Return False
>            End If
>
>            ' Normalize the Xml
>            If Not loXml.Normalize() Then
>                Return False
>            End If
>
>            ' Get the Xml
>            lcXml = loXml.cXml
>
>            ' Add the French header because by default there isn't so we cover it all
>            lcXml = loXml.AddHeaderForFrenchCharacter() + lcXml
>
>            ' Save in a Xml file
>
>            ' Get the proper definition as per the current scope
>            If oProcess Is Nothing Then
>                If Not oApp.CreateFile(lcXml, oApp.cHttpFat + "Temp\" + lcFile + ".xml") Then
>                    Return False
>                End If
>            Else
>                If Not oProcess.CreateFile(lcXml, oApp.cHttpFat + "Temp\" + lcFile + ".xml") Then
>                    Return False
>                End If
>            End If
>
>            ' Generate a Xsl string based on the dataset
>            lcXsl = GetXsl()
>
>            ' Save the Xsl file
>
>            ' Get the proper definition as per the current scope
>            If oProcess Is Nothing Then
>                If Not oApp.CreateFile(lcXsl, oApp.cHttpFat + "Temp\" + lcFile + ".xsl") Then
>                    Return False
>                End If
>            Else
>                If Not oProcess.CreateFile(lcXsl, oApp.cHttpFat + "Temp\" + lcFile + ".xsl") Then
>                    Return False
>                End If
>            End If
>
>            ' Load the Xsl in memory
>            loXslCompiledTransform.Load(oApp.cHttpFat + "Temp\" + lcFile + ".xsl")
>
>            ' Load the target file in memory
>            loFileStream = New FileStream(tcFile, FileMode.Create)
>
>            ' Do the transformation
>            Try
>                loXslCompiledTransform.Transform(New XPath.XPathDocument(oApp.cHttpFat + "Temp\" + lcFile + ".xml"), Nothing, loFileStream)
>            Catch loError As Exception
>
>                ' Get the proper definition as per the current scope
>                If oProcess Is Nothing Then
>                    oApp.ErrorSetup(loError)
>                Else
>                    oProcess.ErrorSetup(loError)
>                End If
>
>                Return False
>            End Try
>
>            ' We can now remove the temporary files
>
>            ' Get the proper definition as per the current scope
>            If oProcess Is Nothing Then
>                oApp.DeleteFile(oApp.cHttpFat + "Temp\" + lcFile + ".xml")
>                oApp.DeleteFile(oApp.cHttpFat + "Temp\" + lcFile + ".xsl")
>            Else
>                oProcess.DeleteFile(oApp.cHttpFat + "Temp\" + lcFile + ".xml")
>                oProcess.DeleteFile(oApp.cHttpFat + "Temp\" + lcFile + ".xsl")
>            End If
>
>            Return True
>        End Function
>
Alexandre Palma
Senior Application Architect
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform